PyGame Setup & Window
PyGame is a Python library for building 2D games. Before you draw anything, you need to install the library, initialize its modules, and create a display window.
Learning Objectives
- 12.5.1.1 Connect the PyGame library and its modules
- 12.5.1.2 Create the game window
Conceptual Anchor
The TV Screen Analogy
Think of PyGame as setting up a TV: first you plug it in
(pygame.init()), then you set the screen size
(set_mode()), give it a channel name (set_caption()),
and then you're ready to display content.
Rules & Theory
Installing PyGame
# Install via pip
pip install pygame
# Verify installation
python -c "import pygame; print(pygame.ver)"Core Setup Commands
| Command | Purpose |
|---|---|
import pygame |
Import the PyGame library |
pygame.init() |
Initialize ALL PyGame modules (display, sound, fonts, etc.) |
pygame.display.set_mode((w, h)) |
Create the game window with width × height |
pygame.display.set_caption("Title") |
Set the window title bar text |
pygame.display.flip() |
Update the full display surface |
pygame.display.update() |
Update portions of the display (or full if no args) |
pygame.quit() |
Shut down all PyGame modules |
sys.exit() |
Exit the Python program completely |
Common PyGame Modules
| Module | Purpose |
|---|---|
pygame.display |
Window and screen management |
pygame.event |
Event handling (keyboard, mouse, quit) |
pygame.draw |
Drawing shapes on surfaces |
pygame.image |
Loading and saving images |
pygame.font |
Rendering text |
pygame.mixer |
Sound effects and music |
pygame.time |
Timing and frame rate control |
pygame.key |
Keyboard state checking |
pygame.mouse |
Mouse position and button state |
Worked Examples
1 Minimal PyGame Window
import pygame
import sys
# 1. Initialize PyGame
pygame.init()
# 2. Create window (800 × 600 pixels)
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 3. Set window title
pygame.display.set_caption("My First Game")
# 4. Main loop (keeps window open)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the screen with a color
screen.fill((30, 30, 30)) # dark gray
# Update the display
pygame.display.flip()
# 5. Clean up
pygame.quit()
sys.exit()2 Window with Custom Colors
import pygame, sys
pygame.init()
# Define colors
SKY_BLUE = (135, 206, 235)
DARK_GREEN = (34, 139, 34)
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Colored Window")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Sky background
screen.fill(SKY_BLUE)
# Green ground (bottom 100px)
pygame.draw.rect(screen, DARK_GREEN, (0, 380, 640, 100))
pygame.display.flip()
pygame.quit()
sys.exit()Common Pitfalls
Forgetting pygame.init()
Without pygame.init(), modules like display, font, and
mixer won't work. Always call it before any other PyGame function.
Window Closes Immediately
Without a main loop (while running), the window flashes and closes.
The loop keeps the program alive and processing events.
Tasks
List 5 PyGame modules and their purposes.
Create a 1024×768 window titled "Space Invaders" with a black background.
Create a window that changes background color every time it refreshes (hint: use
random.randint).
Self-Check Quiz
Q1: What does pygame.init() do?
Q2: What is the difference between flip() and
update()?
flip() updates the
entire display surface. update() can update specific areas (or the whole display if
no args). flip() is simpler and most commonly used for beginners.Q3: Why do we need a main loop?