-2

I got this far and it kind of got all confusing and I can't figure out one simple problem with the code.

while not gameExit:
    while gameOver == True:
        pygame.display.flip()
        gameDisplay.fill(white)
        display_message("OVER. C to continue, Q to quit", red)

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    gameOver = False
                    gameExit = True
                if event.key == pygame.K_c:
                    gameLoop()

    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 = -10
                lead_y_change = 0
            elif event.key == pygame.K_RIGHT:
                lead_x_change = 10
                lead_y_change = 0
            elif event.key == pygame.K_UP:
                lead_y_change = -10
                lead_x_change = 0
            elif event.key == pygame.K_DOWN:
                lead_y_change = 10
                lead_x_change = 0
        if  ( lead_x < 0 ) or (lead_x >= 600) or ( lead_y < 0 ) or ( lead_y >= 800):
            gameOver = True

So when I run this code, and when the snake goes over the boundary, the C for continue and Q for quit thing doesn't come UNTIL I press a button on the keyboard or move my mouse. Why is that?

1
  • Have you already fixed the problem?
    – user5653854
    Aug 3, 2016 at 14:08

1 Answer 1

0

Move:

if  ( lead_x < 0 ) or (lead_x >= 600) or ( lead_y < 0 ) or ( lead_y >= 800):
        gameOver = True

out of the pygame.event.get-loop because it will only run if events have taken place (mouse moved, key pressed). Otherwise the code will not reach the check without your action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.