Python 中的变量一次增加超过 1

问题描述 投票:0回答:1

我目前正在为我的学校做一个有趣的项目,我遇到了一个问题。该项目是一个桨和球游戏,每次球击中桨时,分数应该增加 1,但实际上它会增加 3,有时甚至是 2。我想过用另一个变量和常量检查来手动增加,但我认为还有另一个更好的解决方案。这是代码:

#----- IMPORTS --------
import time
import math

app.score = Label('0', 200, 50, size=25)

ball = Group(
Circle(200, 200, 17, fill=rgb(130, 130, 130), border='black', borderWidth=7)  
)

paddle = Group(
Circle(170, 375, 18, fill=rgb(130, 130, 130), border='black', borderWidth=10),
Circle(240, 375, 18, fill=rgb(130, 130, 130), border='black', borderWidth=10),
Line(170, 362, 240, 362, lineWidth=10),
Line(170, 388, 240, 388, lineWidth=10),
Rect(170, 366.5, 70, 17, fill=rgb(130, 130, 130))
)

app.press = False
app.sec = time.time()
app.xvel = 0
app.yvel = 0
app.paddle_xvel = 0
app.mousex = 0
app.mousey = 0
app.scoreVar = 0

app.ballx = 0
app.bally = 0
app.padx = 0
app.pady = 0

def onMouseMove(mouseX, mouseY):
app.mousex = mouseX
app.mousey = mouseY

def onMousePress(mouseX, mouseY):
app.press = True

def onMouseRelease(mouseX, mouseY):
app.press = False

def _init_():
ball.visible = False
paddle.visible = False
app.background = 'skyblue'
app.score.visible = False

_init_()

playBtn = Group(
Circle(140, 210, 30, fill=rgb(150, 225, 170), border='black', borderWidth=10),
Circle(260, 210, 30, fill=rgb(150, 225, 170), border='black', borderWidth=10),
Rect(140, 190, 120, 40, fill=rgb(150, 225, 170)),
Line(140, 185, 260, 185, lineWidth=10),
Line(140, 235, 260, 235, lineWidth=10),
Label('P l a y', 200, 210, size=30, bold=False, font='montserrat')
)

def onStep():
app.sec = time.time()
#----- Ball physics ------
ball.centerY += app.yvel
ball.centerX += app.xvel
app.yvel += 1
app.ballx = ball.centerX
app.bally = ball.centerY
app.padx = paddle.centerX
app.pady = paddle.centerY - 20

    paddle.rotateAngle = (app.paddle_xvel / 2)
    if app.mousex > paddle.centerX:
        app.paddle_xvel += 3
    if app.mousex < paddle.centerX:
        app.paddle_xvel += -3
    app.paddle_xvel = app.paddle_xvel / 1.2
    paddle.centerX += app.paddle_xvel
    
    if app.yvel < -20:
        app.yvel = -20
    if ball.centerX > 390:
        app.xvel = -5
    if ball.centerX < 10:
        app.xvel = 5
    if ball.centerY >= 400:
        ball.centerY = 200
        app.yvel = 0
        app.xvel = 0
        ball.centerX = 200
    
    #---- HIT CHECK -------
    
    for shape in paddle:
        if ball.hitsShape(shape) == True:
            app.yvel = -20
            app.xvel = app.paddle_xvel
            app.scoreVar += 1
            app.score.value = app.scoreVar
        
    
    #----- Ball physics explanation ------
    
    # First, we set the ball's y velocity (the speed that it goes down by exponentially). 
    # Then, we make it so that the Y and X values are CHANGED by, not set to, the velocities.
    # Next we do a few checks, like if the ball is touching the wall, if it is touching 
    # the ground, and we reset it. If it touches the wall we just bounce it the opposite way.
    # The ball's terminal velocity is -20, which is the max speed the ball can get to.'''
    
    #---- Play btn physics
    playBtn.rotateAngle = math.sin((((app.sec*2)+100) * 2) + 100)
    playBtn.centerX = math.sin(app.sec*2.5) * 3 + 200
    
    if playBtn.contains(app.mousex, app.mousey):
        if app.press == True:
            paddle.visible = True
            ball.visible = True
            app.background = 'white'
            playBtn.visible = False
            app.score.visible = True

lastTime = app.sec
app.sec -= lastTime

注意,这段代码是在CMU CS学院的,所以像while true循环这样的永远循环是不可能的,你不能嵌套循环,你不能两次定义同一个函数。这些文档非常有用:https://academy.cs.cmu.edu/docs。您可能需要单击“文档/颜色”选项卡才能将其打开。我的问题是“app.scoreVar += 1,app.score.value = app.scoreVar” 这些代码行使得乐谱的标签改变“1”,但它根据桨的速度改变 2 或 3。我不能使用 time.sleep() 因为它会冻结整个游戏而不是仅冻结相应的范围。

python variables game-development increment
1个回答
0
投票

罪魁祸首就在这里:

for shape in paddle:
    if ball.hitsShape(shape) == True:
        app.yvel = -20
        app.xvel = app.paddle_xvel
        app.scoreVar += 1
        app.score.value = app.scoreVar

可能,如果内部

if
语句是
True
,您应该直接
break
退出 for 循环。

for shape in paddle:
    if ball.hitsShape(shape) == True:
        app.yvel = -20
        app.xvel = app.paddle_xvel
        app.scoreVar += 1
        app.score.value = app.scoreVar
        break
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.