pygame.time.Clock keeps your game loop at a steady speed. Make one clock before the loop and call tick() once per frame:
clock = pygame.time.Clock()
while running:
...
pygame.display.flip()
clock.tick(60)
What tick() does
clock.tick(60) does two things:
- It caps the frame rate. If the frame finished early, it waits, so the loop runs at most 60 times a second. Without it a simple game runs hundreds of frames a second: everything moves too fast and the processor works flat out.
- It returns the time since the last tick, in milliseconds. At 60 frames a second that is about 16 or 17.
tick() cannot make a slow computer faster. If drawing a frame takes 40 milliseconds, the game runs at 25 frames a second whatever number you pass.
Showing the frame rate
clock.get_fps() returns the average frame rate over the last few ticks:
It shows 0 for the first frames, because there is nothing to average yet.
Delta time
If you move a player 5 pixels every frame, its speed depends on the frame rate: 300 pixels a second at 60 FPS, but only 150 at 30 FPS. Delta time fixes that. Measure how long the last frame took and move by speed × time:
clock.tick(60) / 1000 turns milliseconds into seconds, so speed is in pixels per second. The box crosses the screen in about 2.4 seconds on any computer. Two details:
- Store the position as a float (
x = 0.0), not in apygame.Rect. A Rect keeps whole numbers, so0.5pixels a frame would be dropped every time. Copy the float into the Rect, or use pygame-ce'spygame.FRect, when you draw. - The first frame has
dt = 0, because no frame has been timed yet.
For a small game a fixed tick(60) and per-frame speeds are fine. Delta time is worth it when frame rates vary, for example on old laptops or when you let players choose 30, 60 or 144 FPS.
Clock methods
| Method | Returns |
|---|---|
clock.tick(fps) | Milliseconds since the last tick, after waiting to cap the frame rate |
clock.tick() | The same, without a cap |
clock.get_time() | Milliseconds the last frame took, as returned by the last tick() |
clock.get_fps() | Average frames per second over the last ten ticks |
pygame.time.get_ticks() | Milliseconds since pygame.init() |
For things that happen every few seconds (spawning an enemy, a countdown), see pygame timers.
Frequently Asked Questions
What does clock.tick(60) do?
It waits long enough that the loop runs at most 60 times a second, and returns how many milliseconds passed since the previous call (about 16 or 17 at 60 frames a second).
What is delta time in pygame?
The time the last frame took, usually in seconds: dt = clock.tick(60) / 1000. Moving by speed * dt instead of a fixed number of pixels keeps the speed the same when the frame rate changes.
How do I show the FPS in pygame?
Call clock.get_fps() after tick(). It averages the last few frames, so it reads 0 at the very start. Render it with a font, or put it in the title with pygame.display.set_caption.
What happens if I do not call clock.tick()?
The loop runs as fast as the computer can, often hundreds of frames a second, so the game is too fast and uses a whole processor core. Always tick once per frame.