Unit 12.2A · Term 2

PyGame Setup & Window

PyGame is a Python library for building 2D applications and games. In this lesson you will connect the library, initialise its imported modules, create the main display Surface, and configure the game window.

Learning Objectives

  • 12.5.1.1 Connect the PyGame library and its modules
  • 12.5.1.2 Create the game window

Where This Lesson Fits

TodaySetup & Window

Import PyGame, initialise modules, create the display Surface, choose window size and set the caption.

NextGame Loop

Keep the application running, process events and update frames.

LaterDrawing Primitives

Draw rectangles, circles, lines and other graphics on a Surface.

LaterMovement & Control

Animate objects and respond to keyboard and mouse input.

Stay focused

The event loop and drawing code may appear as support code so that your window remains usable, but those ideas are taught in the next lessons. Today's assessed focus is setup and window creation.

Conceptual Anchor

The TV Screen Analogy

Think of PyGame as setting up a TV. First you connect the system (import pygame), switch its components on (pygame.init()), choose the screen size (set_mode()), and give the window a title (set_caption()).

Connectimport pygame
→
Initialisepygame.init()
→
Create Surfaceset_mode((w,h))
→
Windowcaption + size

Installing & Connecting PyGame

Install PyGame
python -m pip install pygame
Verify the installation
python -c "import pygame; print(pygame.version.ver)"
import pygame pygame.init()

What pygame.init() actually does

pygame.init() initialises the imported PyGame modules. You can also initialise individual modules separately, such as pygame.display.init(), but the higher-level pygame.init() is the normal beginner setup.

ModuleNotFoundError?

If Python reports No module named 'pygame', make sure you installed PyGame into the same Python environment/interpreter that runs your program.

The Game Window & Display Surface

My First Game
display Surface
800 px width
600 px height
Captionpygame.display.set_caption(...)

Controls the title shown by the operating system's window.

Size(width, height)

A pair of integer dimensions passed to set_mode().

Display Surfacescreen = pygame.display.set_mode(...)

set_mode() returns the main display Surface. Later lessons will draw graphics onto this Surface.

Core Setup Commands

Command Purpose in this lesson
import pygame Connect the PyGame package to the Python program
pygame.init() Initialise the imported PyGame modules
pygame.display.set_mode((w, h)) Create the display Surface/window and return the Surface object
pygame.display.set_caption("Title") Set the window title
pygame.quit() Uninitialise PyGame modules when the program is finishing

What about flip() and update()?

pygame.display.flip() and pygame.display.update() make drawing changes visible. They belong naturally with the Game Loop, so we only preview them here and study frame updates in the next lesson.

Game Window Builder

Change the window dimensions and title. The browser preview and generated PyGame setup code update immediately.

My First Game
800 × 600

                                    

Code Anatomy

1import pygame
2pygame.init()
3WIDTH = 800
4HEIGHT = 600
5screen = pygame.display.set_mode((WIDTH, HEIGHT))
6pygame.display.set_caption("My First Game")

Line 1 — Import

import pygame connects the PyGame package so its modules and functions can be accessed with dot notation.

PyGame Module Map

You do not need to learn every module today. Use this map to see how the PyGame unit will grow.

Todaypygame.displayWindow & display Surface

Create/configure the game window and control display behaviour.

Game Looppygame.eventEvents

Read quit, keyboard, mouse and other application events.

Drawingpygame.drawPrimitives

Draw rectangles, circles, lines and polygons on a Surface.

Laterpygame.imageImages

Load image files and work with image Surfaces.

Laterpygame.fontText

Create Font objects and render text to Surfaces.

Laterpygame.mixerAudio

Play sound effects and music.

Game Looppygame.timeTiming

Control frame rate and measure time.

Controlpygame.keyKeyboard

Check keyboard state and keys being held down.

Controlpygame.mouseMouse

Read pointer position and mouse button state.

Spot the Setup Error

Identify the main setup mistake.
Support code: keeping the window open

A real PyGame program needs an event loop to keep the window responsive. You will study this properly in the next lesson: Game Loop. For now, use this block as support code.

running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit()

Worked Examples

Choose an example:

Pitfalls & Common Errors

Wrong set_mode() argument

Pass the size as one pair: pygame.display.set_mode((800, 600)), not two separate positional size arguments.

Forgetting to store the Surface

set_mode() returns the display Surface. Store it in a variable such as screen because later lessons draw onto it.

Wrong width / height order

The pair is (width, height).

Confusing pygame.quit() with program exit

pygame.quit() uninitialises PyGame modules; program termination is a separate Python control-flow issue.

Window closes immediately

This is not a window-creation error. A usable application needs a loop that keeps processing events; that is the focus of the next lesson.

Graded Tasks

Remember

State the purpose of pygame.init(), pygame.display.set_mode() and pygame.display.set_caption().

Remember

Name four PyGame modules and state what each module is responsible for.

Understand

Explain why the object returned by set_mode() is stored in a variable such as screen.

Understand

Explain the difference between the visible window and the display Surface referenced by the program.

Apply

Create a 1024 × 768 window titled "Space Invaders".

Apply

Create constants WIDTH and HEIGHT and use them to make a 900 × 500 window titled "NIS Arcade".

Analyze

Find and correct every setup error: pygame.init; screen=pygame.display.set_mode(800,600); pygame.display.set_caption(My Game).

Create

Design your own game-window setup: choose a meaningful title, dimensions and variable names, then explain each setup statement.

Exam Practice

Q1: Complete the statement to create an 800 × 600 display Surface. [2]

screen = pygame.display.________((____, ____))
Reveal marking points
screen = pygame.display.set_mode((800, 600))

Q2: Explain the purpose of the variable screen. [2]

Reveal marking points
screen stores the display Surface returned by set_mode(). Graphics can later be drawn/blitted onto that Surface.

Q3: Identify and correct the error. [2]

pygame.display.set_mode(800, 600)
Reveal marking points
The size must be passed as one pair/tuple: pygame.display.set_mode((800, 600))

Q4: Write one statement that sets the window caption to "Robot Arena". [1]

Reveal marking point
pygame.display.set_caption("Robot Arena")

Self-Check Quiz

Eight quick checksClick each answer to reveal
1. What statement connects the PyGame package to a Python program?
Click to reveal answer
2. What does pygame.init() do?
Click to reveal answer
3. What does pygame.display.set_mode((800, 600)) return?
Click to reveal answer
4. In (800, 600), which number is the width?
Click to reveal answer
5. Which command changes the text in the window title bar?
Click to reveal answer
6. Which module is responsible for the game window/display?
Click to reveal answer
7. Does pygame.quit() itself mean “end all Python code immediately”?
Click to reveal answer
8. Why does a real PyGame window also need a main/event loop?
Click to reveal answer