我目前正在为学校做一个有趣的项目,我遇到了一个问题.该项目是一个球拍和球的游戏,每次球击中球拍,得分应该上升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循环这样的forever循环是不可能的,你不能嵌套循环,你不能定义同一个函数两次.文档非常有用:https://academy.cs.cmu.edu/docs.您可能需要点击docs/colors选项卡来打开它.我的问题是"app. scoreVar += 1,app. scoreVar. value = app. scoreVar" 这些代码行使它,得分的标签改变了"1",但相反,它改变了2或3基于你的桨的速度.我不能使用time.sleep(),因为它冻结了整个游戏,而不是只冻结各自的作用域.