Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Game Speed

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
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 Clock and .tick(), check out the following resources from the pygame documentation:

Become a Member to join the conversation.