I am using pygame's time.Clock
to run my game at a lower FPS and I noticed that my input seemed to take one extra frame to take effect. I did some debugging and realized that it was not a problem with pygame.event.get()
but rather with pygame.display.update()
. I've written a simple program to demonstrate the issue: (Explanation of program is below the code snippet)
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
colour = 1
key_press = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
screen.fill((0,255,0))
key_press = True
print("A key was pressed.")
if not key_press:
screen.fill((colour*255, colour*255, colour*255))
colour = not colour
else:
key_press = False
pygame.display.update()
clock.tick(0.5)
The program flashes the screen between black and white every two seconds, and whenever any key is pressed the screen will change to green and "A key was pressed"
will be printed to the console. However, when I run it, the text is printed at the correct time but the screen does not change to green until the next frame. This leads me to believe that pygame.display.update()
is not updating the screen when I am calling the function.
The problem seems to be with the delay immediately after calling pygame.display.update()
. If you change the FPS of the clock, the problem still persists. This also occurs if you delay with time.wait()
or pygame.time.delay()
instead of Clock.tick()
.
Another interesting thing that I've noticed is that if you reverse the order of the last two lines of code in my example (pygame.display.update()
and clock.tick(0.5)
), you might expect that the screen will change two clock cycles after a key is pressed instead of one. However, if I do this I get exactly the same effect as when they were in the other order.
I'm not sure if this is a bug or if something subtle is going on here that I just missed. I'm running Python 3.6.2 and using Pygame v1.9.3 on a macOS. Any help you can give would be greatly appreciated!