我目前正在为学校做一个有趣的项目,我遇到了一个问题.该项目是一个球拍和球的游戏,每次球击中球拍,得分应该上升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(),因为它冻结了整个游戏,而不是只冻结各自的作用域.

推荐答案

罪魁祸首在这里:

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,那么在for循环中只需要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.

Python相关问答推荐

如何让 turtle 通过点击和拖动来绘制?

Excel图表-使用openpyxl更改水平轴与Y轴相交的位置(Python)

如何在给定的条件下使numpy数组的计算速度最快?

如何设置视频语言时上传到YouTube与Python API客户端

在含噪声的3D点网格中识别4连通点模式

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

为一个组的每个子组绘制,

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

用SymPy在Python中求解指数函数

当条件满足时停止ODE集成?

GPT python SDK引入了大量开销/错误超时

在电影中向西北方向对齐""

python的文件. truncate()意外地没有截断'

我怎么才能用拉夫分拣呢?

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

对数据帧进行分组,并按组间等概率抽样n行

在一个数据帧中,我如何才能发现每个行号是否出现在一列列表中?

在不中断格式的情况下在文件的特定部分插入XML标签

时间戳上的SOAP头签名无效

解析CSV文件以将详细信息添加到XML文件