In this lesson, you’ll adjust the speed of your game using the pygame
module time
. Normally, you want as high a frame rate as possible, but for this game, you may need to slow it down a bit for the game to be playable. Fortunately, the module time
contains a Clock
designed precisely for this purpose.
Using Clock
to establish a playable frame rate requires just two lines of code. The first creates a new Clock
before the game loop begins:
102# Setup the clock for a decent framerate
103clock = pygame.time.Clock()
The second calls .tick()
to inform pygame
that the program has reached the end of the frame:
179# Flip everything to the display
180pygame.display.flip()
181
182# Ensure program maintains a rate of 30 frames per second
183clock.tick(30)
For more information about Clock
and .tick()
, check out the following resources from the pygame
documentation: