0

I'm beginner programmer and am using pygame for the first time--I'm stuck on a strange behavior with screen updating.

My sample code simply displays a count on the screen, starting at 0 going up one with every iteration. I want it to pause for 2 seconds at "10." Instead it pauses at "9" (or the frame before whatever you set the numbers at). "10," in this case will display (after the pause) as it continues at full speed.

Why is it pausing one "frame" early? The pause if-statement is just after the screen display update--I'm missing something...

#!/usr/bin/python

import pygame

pygame.init()

number = 0
color = (255, 255, 255)
done = False
pygame.mouse.set_visible(False)
info = pygame.display.Info()
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.FULLSCREEN)
screen_rect = screen.get_rect()
font = pygame.font.SysFont('ffmetapro', 96)

while not done:

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    text = str(number)
    pygSurface = font.render(text, True, color)

    screen.fill((0, 0, 0))
    screen.blit(pygSurface, pygSurface.get_rect(center=screen_rect.center))
    pygame.display.flip()

    if number == 10:
            pygame.time.delay(2000)

    number = number +1

pygame.quit()
sys.exit()
Darin Boville
  • 63
  • 1
  • 1
  • 6
  • Further clue (but even more confusing of me)--if I move the pause if-statement *after* the "number = number +1" line the pause occurs at "8"... – Darin Boville Aug 10 '17 at 00:37
  • It pauses at "10" for me and it looks like the code should work correctly. – skrx Aug 10 '17 at 01:15
  • I also don't seem to find an issue with this. However, I can explain the "if I move the pause [...] the pause occurs at '8'" thing. Basically, it's normal behavior : if you increment the number before checking the condition, you'll reach the "== 10" one step earlier, since the first check will be made on number == 1. display 0, check 1, display 1, check 2, display 2, check 3 etc. So you reach "check 10" after displaying "9". (after 8 in your case, but the logic is the same..) – Alceste_ Aug 10 '17 at 02:48
  • @skrx No luck here. Tried a different IDE (Using Coderunner, tried Anaconda/Spyder). Also tried Python 2.7 vs 3.x. Same result. Stepping through my code, the screen is only updating after the for loop, not at the pygame.display.flip line. Variables are showing correct numbers (i.e. pausing when the variable "number" is 10) but the screen is still not updated at that point. (Also, fyi, the initial run through the code puts up a blank gray screen, with no number.) Planning on trying to re-install pygame (but I have to update Homebrew which in turn requires a more modern Xcode...ugh! :) ) – Darin Boville Aug 11 '17 at 00:52
  • That sounds pretty odd. We should try to modify the program first and see if something happens before you attempt to reinstall everything. For example check out if it works in windowed mode instead of fullscreen mode. – skrx Aug 11 '17 at 01:37

0 Answers0