Before We Begin · Install Pygame Zero
One-timePygame Zero opens a real graphics window, so — just like turtle — it needs a desktop Python install, not an online editor. If you set up Python + VS Code back in PY-L1-01, you are ready. Thonny works too.
Open a terminal (in VS Code: Terminal → New Terminal) and type one line:
pip install pgzero
That installs pgzero and the pygame engine it sits on top of. You only do this once per computer.
1. From the terminal: pgzrun mygame.py — the official way.
2. Press the green Run button in your editor — but only if your file ends with pgzrun.go() (we use this trick all module long, so any Run button works).
Learning Goals
3 minBy the end of this lesson you can:
- Install Pygame Zero with
pip install pgzeroand run a file. - Open an empty game window by setting
WIDTHandHEIGHT. - Write a
draw()function that fills the screen and prints a welcome message.
Warm-Up · You Already Know the Hard Part
5 minGames are built from the Python you already have: variables, if, loops, lists and functions. Before we draw anything, prove your Python brain is warmed up. What does this print?
score = 0 for star in range(3): score = score + 10 print("Score:", score)
Show the answer
Output
Score: 30
The loop runs three times, adding 10 each pass. In a game this same idea becomes "+10 points every time the player catches a star".
A game is just a program that draws a picture many times a second. Pygame Zero handles the window and the timing — you only describe what to draw.
New Concept · A Window With No Boilerplate
12 minThink of Pygame Zero like a helpful stage manager. You don't build the theatre, hang the lights or open the curtains — that is done for you. You just hand over your script, and it plays. In code, that means you skip the long set-up other game libraries demand.
The smallest possible game
Set two special variables — WIDTH and HEIGHT — and Pygame Zero opens a window that size. Add pgzrun.go() at the very bottom so the Run button works.
import pgzrun WIDTH = 600 HEIGHT = 400 pgzrun.go()
Run it and a blank 600×400 window appears. That is your stage. Notice there is no "main loop" to write — Pygame Zero runs one for you behind the scenes.
The magic draw() function
To put something on the stage, write a function called exactly draw. Pygame Zero calls it for you, around 60 times every second. Inside it, screen is your canvas.
def draw(): screen.fill("midnightblue")
screen.fill(colour) paints the whole window one colour. Now the stage is no longer black.
Why it matters
Special names like WIDTH, HEIGHT, draw and screen are the deal Pygame Zero offers: name things its way, and it wires everything up. Get a name wrong (say Draw with a capital D) and nothing happens — the function just never gets called.
Worked Example · Your Welcome Screen
12 minThe story
Aisyah wants a title screen for the game she will build over this module. Let's make a window that fills with colour and greets her by name in the centre. Save it as welcome.py:
# welcome.py — your first Pygame Zero window import pgzrun WIDTH = 600 HEIGHT = 400 def draw(): screen.fill("midnightblue") screen.draw.text( "Selamat datang, Aisyah!", center=(300, 200), fontsize=48, color="white", ) pgzrun.go()
Two new pieces. screen.draw.text(...) writes words on the canvas. center=(300, 200) places the middle of the text at the point (300, 200) — the centre of a 600×400 window. We'll dig into those numbers next lesson.
What you'll see
For two levels your code talked to the user through print(). Today the very same Python paints a window with screen. Same brain, new canvas.
Try It Yourself
13 minChange the window to 800×500 and fill it with a colour you like (try "teal", "darkgreen" or "indigo").
Hint
WIDTH = 800 HEIGHT = 500 def draw(): screen.fill("teal")
Swap the welcome text for your own name, and move it near the top by changing the center point to (300, 80).
Hint
def draw(): screen.fill("midnightblue") screen.draw.text("Wei Jie's Arcade", center=(300, 80), fontsize=40, color="white")
Mini-Challenge · A Personalised Splash Screen
8 minCombine today's window with the f-strings you met in Level 2. Store a player name in a variable, then build the welcome line with an f-string so changing one variable updates the screen.
It works if…
changing player_name at the top changes the words on screen
Show one possible solution
# splash.py — name-driven welcome screen import pgzrun WIDTH = 600 HEIGHT = 400 player_name = "Priya" def draw(): screen.fill("indigo") screen.draw.text(f"Welcome, {player_name}!", center=(300, 200), fontsize=46, color="white") pgzrun.go()
The win is the f-string: f"Welcome, {player_name}!" drops the variable straight into the drawn text. Your name and colours are your own.
Recap
3 minPygame Zero opens a real game window with almost no set-up. You set WIDTH and HEIGHT to size it, write a lowercase draw() function to paint it, and use screen.fill and screen.draw.text to put things on the canvas. End the file with pgzrun.go() so any Run button works.
Vocabulary Card
- pgzero
- The beginner-friendly game library that hides Pygame's boilerplate. Installed with
pip install pgzero. - WIDTH / HEIGHT
- Special capitalised variables that set the window's size in pixels.
- draw()
- A function you write; Pygame Zero calls it ~60 times a second to paint the screen.
- screen
- The canvas object.
screen.fill(colour)paints it;screen.draw.text(...)writes on it.
Homework
4 minDesign the title screen for the game you dream of building this module. Make a window of any size, fill it with a colour that fits the mood, and write the game's title in the centre. Save it as title.py and bring a screenshot to the next class.
Stretch. Add a second, smaller line underneath — like "Press SPACE to start" — by calling screen.draw.text a second time at a lower point.
Sample · title.py
# title.py — a game title screen import pgzrun WIDTH = 640 HEIGHT = 400 def draw(): screen.fill("black") screen.draw.text("DURIAN DASH", center=(320, 160), fontsize=64, color="gold") screen.draw.text("Press SPACE to start", center=(320, 240), fontsize=28, color="white") pgzrun.go()
Your title, colours and window size will differ — that's the point. The non-negotiables: a sized window, a filled background, and a centred title.