Computer Science
Grade 9
20 min
2. Introduction to Pygame
Introduce the Pygame library and its features for creating games in Python.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Initialize the Pygame library and create a game window.
Implement a basic game loop to keep the program running.
Handle the `QUIT` event to properly close the game window.
Define and use RGB color tuples for backgrounds and shapes.
Draw basic geometric shapes, like a rectangle, onto the screen.
Use `pygame.display.update()` to render changes to the screen.
Ever wanted to build your own video game instead of just playing one? 🎮 Let's take the first step from player to creator!
Pygame is a set of Python modules designed for writing video games. In this lesson, you'll learn the fundamental building blocks: creating a game window, handling player input, and drawing your first graphics on the screen. This is the foundation for every game you'l...
2
Key Concepts & Vocabulary
TermDefinitionExample
Game LoopThe central part of any game. It's an infinite loop that continuously checks for player input, updates the game's state (like character positions), and draws everything on the screen. It keeps the game running frame by frame.A `while running:` loop that contains all the game's logic. Inside, it processes events, updates positions, and redraws the screen for each frame.
SurfaceA blank rectangular image or 'canvas' in Pygame that you can draw on. The main game window itself is a special Surface, but you can create others for images or text.`screen = pygame.display.set_mode((800, 600))` creates the main display Surface with a width of 800 pixels and a height of 600 pixels.
EventAn action that occurs within the game window, such as a key...
3
Core Syntax & Patterns
The Basic Pygame Structure
1. `import pygame`
2. `pygame.init()`
3. Set up the screen: `screen = pygame.display.set_mode(...)`
4. Game loop: `while running:`
5. Quit: `pygame.quit()`
This is the essential 'boilerplate' or template for every Pygame program. You must initialize the library, create a window, run the main loop, and then un-initialize the library when the loop ends.
The Game Loop Pattern
Inside `while running:`:
1. Event Handling: `for event in pygame.event.get(): ...`
2. Game Logic: Update variables, positions, etc.
3. Drawing: `screen.fill(...)` and `pygame.draw...`
4. Update Display: `pygame.display.update()`
This order is crucial for each frame. First, process all user inputs. Second, update the state of your game world. Third, clear the previous fr...
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
If you remove the `screen.fill(BLACK)` line from the 'Drawing a Green Player' example, what would be the visual result after the program runs for a few seconds?
A.The screen would be black and the green rectangle would flash.
B.The green rectangle would appear to 'smear' or 'paint' across the screen, as each new frame is drawn on top of the last.
C.The program would crash because `screen.fill()` is a required function.
D.Only a single, stationary green rectangle would appear on a gray background.
Challenging
A student's Pygame window appears on the screen but closes instantly. Their code includes a `while running:` loop and a `pygame.QUIT` event handler. Which of the following is a plausible cause for this issue?
A.The screen width and height are too large for their monitor.
B.They called `pygame.quit()` inside the game loop.
C.The `running` variable was initialized to `False` before the loop started.
D.They forgot to call `pygame.display.set_caption()`.
Challenging
If you wanted to create a background that smoothly cycles through different colors, where in the game loop structure should you place the code that calculates the new color and calls `screen.fill()`?
A.Inside the `while` loop, before any `pygame.draw` commands.
B.Before the `while` loop begins.
C.After `pygame.display.update()`.
D.Inside the `for event in pygame.event.get():` loop.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free