In this lesson, you’ll adjust the speed of your game using the
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.
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 103 clock = 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 180 pygame.display.flip() 181 182 # Ensure program maintains a rate of 30 frames per second 183 clock.tick(30)
For more information about
.tick(), check out the following resources from the
Become a Member to join the conversation.