-1

In order to code the game Pong, in my main.py I have a value called game_is_on set to True,

As long as it's set, I have a refresh of the screen. When the ball touches one of the sides, I display some "Game over" text, turn game_is_on to False and alert the players to reset the game by pressing enter.

When they do, I want to set game_is_on back to True, but somehow can't reach it

#Before that I have a whole setup of my elements, when all is done I do
game_is_on = True


def game_reset():
    game_is_on = True
    print("we try to reset")
    ball.restart()
    score_B.restart()
    field.screen.update()


while game_is_on:
    while game_is_on:
    field.screen.update()
    ball.move()
    if ball.ycor() < -390 or ball.ycor() > 390:
        ball.hitwall()
    if ball.distance(paddle_A) <= 20 or ball.distance(paddle_B) <= 20:
        ball.hitpaddle()
    if ball.xcor() < - 600 or ball.xcor() > 600:
        if ball.xcor() < - 600:
            score_B.increase()
        else:
            score_A.increase()
        score_B.gameover()
        game_is_on = False
        field.screen.onkey(game_reset, "Return")

How can I set the game_is_on back to true in my game_reset() ?

Ari
  • 460
  • 6
  • 13
Krowar
  • 341
  • 2
  • 7
  • 15
  • can you try to add the `global` keyword infront of game_is_on ? – alexrogo Dec 18 '22 at 14:15
  • Your code appears to have an extra 'while game_is on:', without indentation. This is probably a mistaken copy/paste. – Ari Dec 18 '22 at 14:16

1 Answers1

1
def game_reset():
    game_is_on = True

create a new local variable game_is_on within your game_reset function, it does not relate to the global one.

What you need to do is make it refer to the global one by using the global keyword:

def game_reset():
    global game_is_on
    game_is_on = True
luk2302
  • 55,258
  • 23
  • 97
  • 137