Computer Science
Grade 9
20 min
7. Game Loops and Frame Rate
Understand the concept of game loops and how to control the frame rate in Pygame.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Explain the purpose of a game loop and its three main phases: input, update, and render.
Implement a basic, functional game loop in Pygame that can be safely exited.
Define 'frame rate' (FPS) and explain its impact on game smoothness and performance.
Use the `pygame.time.Clock` object to control and cap the frame rate of a game.
Update an object's position within the game loop to create simple animation.
Differentiate between game logic that depends on frame rate and frame rate-independent logic.
Ever wonder why your favorite video game feels so smooth and responsive, while a simple animation might look choppy? 🎮 The secret lies in the game's heartbeat: its game loop!
This lesson explores the fundamental concept of the game loop, the...
2
Key Concepts & Vocabulary
TermDefinitionExample
Game LoopAn infinite loop that runs continuously during gameplay. It processes player input, updates the game's state, and draws the results on the screen.A `while running:` loop in Python that checks for keyboard presses, moves a character, and then redraws the screen.
FrameA single, static image that is drawn to the screen. A rapid sequence of frames creates the illusion of motion.One picture of a character mid-jump. When shown after a picture of the character on the ground, it looks like they are starting to jump.
Frame Rate (FPS)Frames Per Second. It's the measure of how many times the game loop runs and redraws the screen in one second.A game running at 60 FPS updates the screen 60 times every second, resulting in very smooth motion.
Event HandlingThe...
3
Core Syntax & Patterns
The Standard Game Loop Structure
running = True
while running:
# 1. Process Input (Events)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 2. Update Game State
# (e.g., player_x += 5)
# 3. Render (Draw)
screen.fill((0, 0, 0)) # Black background
# (e.g., draw player at new position)
pygame.display.update()
This is the fundamental pattern for any Pygame application. It ensures the game continuously checks for input, updates game logic, and redraws the screen until the `running` variable becomes `False`.
Frame Rate Control with `pygame.time.Clock`
# Before the loop
clock = pygame.time.Clock()
# Inside the loop (usually at the end)
clock.tick(60) # Limit to 60 FPS
This pattern is used to cont...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A programmer correctly writes code to update a player's position and draw it, but forgets to include the `pygame.display.update()` line inside the game loop. What will the user see when they run the program?
A.The initial blank or black screen, with no character ever appearing.
B.The game will crash because the display is a required function.
C.The character will be drawn once at its starting position and will not move.
D.The game logic will run, but the window will be invisible.
Challenging
A moving enemy character appears to 'teleport' in large jumps instead of moving smoothly. The frame rate is stable at 60 FPS. Which of these is the most likely cause of the 'teleporting' issue?
A.The `pygame.display.update()` call is missing from the loop.
B.The enemy's position is being updated by a very large number each frame (e.g., `enemy_x += 100`).
C.The `screen.fill()` command is using the wrong color.
D.The `clock.tick(60)` is placed at the beginning of the loop instead of the end.
Challenging
A student reorders their game loop to process input *after* updating and rendering. What is the most likely negative consequence of this structure?
A.The game will crash because input must always be processed first.
B.Player actions will feel 'laggy' or delayed by one frame.
C.The screen will not update correctly, causing visual glitches.
D.The game will run faster because input processing is less frequent.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free