Computer Science
Grade 9
20 min
3. Setting Up Pygame: Installation and Basic Structure
Guide students through the process of installing Pygame and creating a basic game structure.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Install the Pygame library using a package manager.
Write a Python script that initializes and quits Pygame.
Create and display a game window of a specified size.
Explain the purpose and structure of a basic game loop.
Implement an event handler to close the game window properly.
Structure a Pygame program with initialization, a game loop, and a quit sequence.
Ever wanted to build your own video game from scratch? 🎮 Let's lay the first brick by setting up our game development workshop!
This lesson is your launchpad into game development. We will install Pygame, a powerful set of tools for Python, and learn how to create the fundamental skeleton of any game: a window that opens, runs, and closes.
Real-World Applications
Creating 2D indie games l...
2
Key Concepts & Vocabulary
TermDefinitionExample
PygameA free, open-source library for the Python programming language, designed for writing video games. It provides tools for graphics, sound, and user input.Using `pygame.draw.rect()` to draw a rectangle on the screen.
Library / ModuleA collection of pre-written code that you can use in your own programs. Importing a library gives you access to its functions and variables.`import pygame` at the top of your script makes all of Pygame's tools available to you.
Game LoopThe central part of a game's code that runs over and over again. It's responsible for handling user input, updating game states, and drawing everything on the screen.A `while` loop that continuously checks for player actions and redraws the game characters' positions.
EventAn action...
3
Core Syntax & Patterns
Pygame Initialization and Setup
import pygame
pygame.init()
screen = pygame.display.set_mode((width, height))
This is the required starting sequence for any Pygame program. You must import the library, initialize all its modules with `pygame.init()`, and then create the main display window (surface).
The Basic Game Loop Structure
running = True
while running:
# 1. Event Handling
# 2. Game Logic
# 3. Drawing / Rendering
pygame.quit()
This `while` loop keeps the game running. Inside, you must first handle events (like closing the window), then update game elements, and finally draw everything to the screen. The loop ends when the `running` variable becomes `False`, and `pygame.quit()` cleans up and closes the program.
Event Handling
for event in pygame.event....
1 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 student places `pygame.display.update()` (a function that redraws the screen) at the very beginning of the game loop, before the event handling block. What is a potential negative consequence of this ordering?
A.The program will crash because `update()` cannot be the first call in the loop.
B.The display will always be one frame behind the user's input, causing noticeable input lag.
C.The user's input will be processed twice for every frame.
D.There is no negative consequence; the order does not matter.
Challenging
Instead of a `for` loop, a student checks for the quit event with `if pygame.event.get(pygame.QUIT): running = False`. While this works for closing the window, why is the standard `for event in pygame.event.get():` structure generally better for a complete game?
A.The `for` loop is more memory efficient.
B.The `if` statement can only be used once in a script.
C.The `if` statement can only check for one event type and discards all others, like key presses or mouse clicks.
D.The `for` loop allows you to process multiple events that might happen in the same frame, while the `if` statement might miss some.
Challenging
The `pygame.init()` function returns a tuple of two numbers, for example `(6, 0)`. The first number represents successful module initializations, and the second represents failures. What would a return value of `(5, 1)` most likely indicate?
A.One of Pygame's optional modules (like the font or sound module) failed to initialize correctly.
B.The program is running 5 times faster than intended.
C.The user has 5 lives and has lost 1.
D.Pygame was successfully installed, but one component is missing.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free