To set a constant framerate in pygame
, use Clock.tick
FRAMERATE = 30
clock = pygame.time.Clock()
while True:
clock.tick(FRAMERATE)
for event in pygame.event.get():
...
From the documentation:
[
tick
] should be called once per frame. It will compute how many milliseconds have passed since the previous call.If you pass the optional
framerate
argument the function will delay to keep the game running slower than the given ticks per second. This can be used to help limit the runtime speed of a game. By callingClock.tick(40)
once per frame, the program will never run at more than 40 frames per second.