最近我用该turtle
模块制作了一个 Pong 游戏,我正在使用这种方法来暂停游戏:
# Pause function
game_pause = False
def pause_game():
global game_pause
if game_pause:
game_pause = False
else:
game_pause = True
window.listen()
window.onkeypress(pause_game, "p")
onkeypress()
但是当我暂停游戏时,通过命令,桨仍然移动。这种方法是否适合这种情况?还是我只是用错了?如果您需要更多上下文,这是主游戏循环:
# Main game loop
while True:
if game_pause:
window.update()
else:
# Ball mover
ball.setx(ball.xcor() + ball.dx / 5)
ball.sety(ball.ycor() + ball.dy / 5)
# Setup keybinding
window.listen()
window.onkeypress(paddle_a.move_up, "w")
window.onkeypress(paddle_a.move_down, "s")
window.onkeypress(paddle_b.move_up, "Up")
window.onkeypress(paddle_b.move_down, "Down")
# Border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_a += 1
pen.clear()
pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=("Terminal", 22, "normal"))
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_b += 1
pen.clear()
pen.write(f"Player A: {score_a} Player B: {score_b}", align="center", font=("Terminal", 22, "normal"))
# Paddles collision
if 350 > ball.xcor() > 340 and paddle_b.ycor() + 50 > ball.ycor() > paddle_b.ycor() - 50:
ball.setx(340)
ball.dx *= -1
if -340 > ball.xcor() > -350 and paddle_a.ycor() + 50 > ball.ycor() > paddle_a.ycor() - 50:
ball.setx(-340)
ball.dx *= -1