Recently I made a Pong game with the turtle
module and I'm using this method to pause the game:
# 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")
But the paddles, via onkeypress()
command, still move when I pause the game. Is this method correct for this situation? Or am I just using it wrong? Here's the main game loop if you need more context:
# 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
Your program is structured incorrectly, so I would avoid any quick fix suggestions. Even if they work, you're going to have further problems. Below is my attempt to restructure your code as a proper turtle program. I had to reconstruct missing pieces, so it's not going to look identical:
See if that gives you the pause functionality you desire as well as simplifies your code and leaves room for more features.