Python中变量一次增加超过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(),因为那样会让整个游戏都卡住,而不仅仅是相关的部分。
1 个回答
1
问题出在这里:
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 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
否则,如果球碰到了多个形状,得分就会超过1分。