Stage 2 ยท SETUP SCREEN

Setup the Screen โ€” your game's blank page

Before anything can move, you need a window for the game to live in. That's what Stage 2 builds: an 800ร—600 window called Galaxy Defender, plus a clock to keep the game running smoothly.

๐Ÿง  Quick Recap (30 seconds)

Before we dive in โ€” can you remember from Lesson 1 (Imports)?

Q1. What does import do?

It grabs a library โ€” a pre-written toolbox of code โ€” and brings it into your program so you can use it. import pygame means "I want the graphics toolbox."

Q2. Which library is the GAMES toolbox?

pygame. It draws shapes, plays sounds, reads the keyboard, and runs game loops.

Where we are on the rocket

Rocket Ladder infographic โ€” Stage 2 highlighted by arrows. โ–ถ โ—€

You're on Stage 2 of 10.

What is "setup the screen"?

In Stage 1 you grabbed the pygame toolbox. Now you're going to open it and turn it on. Think of it like unboxing a new TV: you take it out, plug it in, pick the screen size, and stick a name sticker on the front. After this stage, you have a real, working game window โ€” empty for now, but ready for everything we add next.

The four things we do in Stage 2

The big idea: Stage 2 is the "blank canvas" of your game. Every later stage paints on top of this canvas.

The recipe

  1. Start with import pygame (from Stage 1).
  2. Turn pygame on with pygame.init().
  3. Make a 800ร—600 window and save it in screen.
  4. Give the window a title: "Galaxy Defender".
  5. Make a clock for smooth speed.
  6. Run a tiny loop so the window actually stays open (or it would close instantly).

Steps 1โ€“5 are pure Stage 2. Step 6 is a sneak peek at Stage 10 (the Game Loop) โ€” we just need it now so the window doesn't blink and disappear.

The code so far

# Stage 1 โ€” Imports
import pygame

# Stage 2 โ€” Setup the Screen
pygame.init()

WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Galaxy Defender")
clock = pygame.time.Clock()

# Tiny loop so we can actually SEE the window
# (we'll learn this properly in Stage 10)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 50))   # paint it space-blue so we know it works
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Run this and you'll see a dark blue window titled Galaxy Defender. Click the close button to end the program. That's it โ€” Stage 2 is done. ๐ŸŽ‰

๐ŸŽฎ Want to play with more code? Scroll down to the Code Library (10 mini programs). Copy any program, paste it into the editor, and press the green โ–ถ Run button.

Try it yourself

Type the code into this in-browser editor and press Run

Type the code above into the editor, then click โ–ถ Run.

๐Ÿ† Mini-challenges

Remember: colours are (Red, Green, Blue), each 0โ€“255.

Handouts for this lesson

Print these, keep them open in another tab, or download them to your computer.

๐Ÿ“š Lesson 2 Code Library โ€” 5 brand-new pygame mini-games made just for this stage: Hello Window, Click the Balloon, Spotlight, Colour Switcher, and Smiley Face. Each one comes with challenges to try.

๐Ÿ“„ Lesson 2 Code Library (5 pygame games) ๐Ÿ“„ Lesson 1 Code Library

๐Ÿ—บ๏ธ The big-picture handouts โ€” keep these by your side for the whole course.

๐Ÿ“„ Big Picture ๐Ÿ“„ Rocket Ladder ๐Ÿ“„ Pixel Cheat Sheet ๐Ÿ“„ Code Recipe ๐Ÿ–ผ๏ธ 6 Parts of a Turtle Program

๐Ÿค– Want to try your own idea? Ask an AI like Claude or ChatGPT something like "Write me a short pygame program that opens a window with a red circle in the middle", then paste the code into the Trinket above and click Run. Reading other people's code is one of the fastest ways to get better.

โ† Previous Lesson 1 ยท Imports