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
Import PyGame, initialise modules, create the display Surface, choose window size and set the caption.
Keep the application running, process events and update frames.
Draw rectangles, circles, lines and other graphics on a Surface.
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()).
import pygame
pygame.init()
set_mode((w,h))caption + size
Installing & Connecting PyGame
python -m pip install pygame
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
pygame.display.set_caption(...)
Controls the title shown by the operating system's window.
(width, height)
A pair of integer dimensions passed to set_mode().
screen = 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.
Code Anatomy
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.
pygame.displayWindow & display
Surface
Create/configure the game window and control display behaviour.
pygame.eventEvents
Read quit, keyboard, mouse and other application events.
pygame.drawPrimitives
Draw rectangles, circles, lines and polygons on a Surface.
pygame.imageImages
Load image files and work with image Surfaces.
pygame.fontText
Create Font objects and render text to Surfaces.
pygame.mixerAudio
Play sound effects and music.
pygame.timeTiming
Control frame rate and measure time.
pygame.keyKeyboard
Check keyboard state and keys being held down.
pygame.mouseMouse
Read pointer position and mouse button state.
Spot the Setup Error
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
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
State the purpose of pygame.init(),
pygame.display.set_mode() and
pygame.display.set_caption().
Name four PyGame modules and state what each module is responsible for.
Explain why the object returned by set_mode() is stored in a variable
such as screen.
Explain the difference between the visible window and the display Surface referenced by the program.
Create a 1024 × 768 window titled "Space Invaders".
Create constants WIDTH and HEIGHT and use them to make a
900 × 500 window titled "NIS Arcade".
Find and correct every setup error:
pygame.init; screen=pygame.display.set_mode(800,600); pygame.display.set_caption(My Game).
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
Q2: Explain the purpose of the variable screen. [2]
Reveal marking points
Q3: Identify and correct the error. [2]
pygame.display.set_mode(800, 600)Reveal marking points
Q4: Write one statement that sets the window caption to
"Robot Arena". [1]
Reveal marking point
Self-Check Quiz
pygame.init() do?pygame.display.set_mode((800, 600))
return?(800, 600), which number is the width?pygame.quit() itself mean “end all Python
code immediately”?