0

I want to create a GameOver screen with the option to "Play Again" using KEYDOWN event. The quit option works but my gameloop does not update. When I type gameLoop() the font doesn't turn blue.

My Code:

def gameLoop():
      gameExit=False 
      gameOver=False

      lead_x = 300
      lead_y = 300

      lead_x_change = 0
      lead_y_change = 0

      randAppleX = random.randrange(0, display_width-block_size) 
      randAppleY = random.randrange(0, display_height-block_size)

      gameDisplay = pygame.display.set_mode((display_width,display_height))

while not gameExit:

    while gameOver == True:
        gameDisplay.fill(white)
        message_to_screen("Game Over, Press C to Play Again or Q to Quit",red)
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q: #This works fine!
                    gameExit = True
                    gameOver = False
                if event.key == pygame.K_c:
                    gameLoop() #This won't work

    if lead_x > 789 or lead_x < 1 or lead_y > 589 or lead_y < 1:
                    lead_y_change = 0
                    lead_x_change = 0
                    print "Game Over"
                    gameOver = True


    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                if event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                if event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                if event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0                


    lead_x += lead_x_change
    lead_y += lead_y_change

    gameDisplay.fill(white)
    pygame.draw.rect(gameDisplay, red, [randAppleX,randAppleY,block_size,block_size])
    pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,block_size,block_size])
    pygame.display.update()

    clock.tick(FPS)

message_to_screen("You Lose Turd", red)
pygame.display.update()

pygame.quit()
quit()

gameLoop()
SiHa
  • 7,830
  • 13
  • 34
  • 43
  • 1
    `gameExit` and `gameOver` are unkown in `gameLoop`s scope. They will be declared locally and won't change their global siblings. To fix that just add `global gameExit` and `global gameOver` to the beginning of the function. –  Oct 06 '16 at 05:48
  • for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: global gameExit = True global gameOver = False if event.key == pygame.K_c: gameLoop() – Mary Caroline Oct 06 '16 at 06:23
  • Is this how you mean? Thanks for the comment! – Mary Caroline Oct 06 '16 at 06:23
  • No, I meant the `gameLoop` function. Take a look at [this](http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python#answer-4693385). –  Oct 06 '16 at 06:56
  • Take a look at [this](https://stackoverflow.com/questions/14700889/pygame-level-menu-states/14727074) question to see how you could manage this – sloth Oct 06 '16 at 07:56

0 Answers0