0

My goal for my program is for it to loop over images and display them on the screen. When the user presses space, it is supposed goes to a new screen. For some reason, there is a lag from when the user presses space and when it breaks out of the game1 loop.

def game_loop1():
        game1=True
        winScreen=True
        current=0
        myfont = pygame.font.SysFont("monospace", 25)
        label = myfont.render("Who is this person? (Press Space)", 1, (0,0,0))


    while game1:
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                   pygame.quit()
                if event.type == pygame.KEYDOWN:
                    if event.key==pygame.K_SPACE:
                       game1=False # doesn't break out of loop immediately here
        gameDisplay.fill(white)
        gameDisplay.blit(label,(0,500))
        gameDisplay.blit(obama[current], (0,0))
        current+=1
        pygame.display.flip()
        clock.tick(50)
        pygame.time.delay(2500)


    while winScreen:
        gameDisplay.fill(white)
        winLabel=myfont.render("The person was Obama! ", 1, (0,0,0))
        gameDisplay.blit(winLabel,(0,500))
        gameDisplay.blit(obama[len(obama)-1], (0,0))
        pygame.display.flip()
        pygame.time.delay(2500)
        winScreen=False 

How do I fix this?

  • Why are you using `pygame.time.delay(2500)` – MooingRawr Oct 24 '16 at 13:54
  • The time delay is stoping pygame from giving you new events (quit) until it is over. You should change your loop to switch to a new picture once a button is pressed and the time is a specified amount later. – Adam Van Prooyen Oct 24 '16 at 13:55
  • Why would it break out of the loop immediately when you set `game1=False`? the condition for the loop isn't re-evaluated until the current iteration is done (and you have a delay call added at the end of the loop, so the re-evaluation is obviously delayed) – UnholySheep Oct 24 '16 at 13:56
  • `delay` is not good solution because it delays program not only after it sets `game = False` but it delays `for event` loop. It checks pressed key only every 2.5s. – furas Oct 24 '16 at 14:41
  • 1
    As others have pointed out, delaying is a bad idea. Have a variable that stores the time when you start and check each time through the loop to see if you should move on. – cmd Oct 24 '16 at 21:37

1 Answers1

0

You could try:

game1=False
break

to exit the while loop immediately.

However, in this case you don't really need the game1 variable. You could change the loop condition to True, and replace the line game1=False with break, and your code should work as expected.

penalosa
  • 170
  • 1
  • 13