Your first pygame window
Initialize pygame, open a window, fill it with color, and quit cleanly — the minimum scaffold every game builds on.
- Initialize pygame and create a display surface
- Set a window caption
- Fill the background with a solid color
- Handle the quit event and exit cleanly
Pygame is a Python library that wraps SDL — a low-level media library — so you can open windows, draw shapes and images, play sounds, and read input without writing C. It is the standard starting point for 2D game development in Python, and the concepts you learn here transfer directly to more sophisticated engines.
Every pygame program has the same skeleton: initialize, create a window, enter a loop, draw, repeat until the user quits. This lesson covers everything up to the loop — just enough to get a window on screen and close it without errors.
The minimum scaffold
Pyodide (the in-browser Python runner) does not support pygame's display system.
Read through this code carefully in the browser, then run it locally:
pip install pygame followed by python game.py.
import pygame
import sys
# 1. Initialize all pygame modules (audio, display, font, etc.)
pygame.init()
# 2. Create the display surface — the window you draw onto.
# set_mode takes (width, height) in pixels.
screen = pygame.display.set_mode((640, 480))
# 3. Give the window a title.
pygame.display.set_caption("My First Game")
# 4. Fill the window with a solid colour. Colours are (R, G, B) tuples, 0–255.
screen.fill((30, 30, 40)) # dark navy background
# 5. Push the drawing from memory to the screen.
pygame.display.flip()
# 6. Wait until the user closes the window.
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 7. Shut down pygame cleanly, then exit the process.
pygame.quit()
sys.exit()What each part does
pygame.init() starts all the pygame subsystems at once. Call this once,
before anything else. It returns a tuple of (succeeded, failed) counts, but for
beginner projects you can ignore the return value.
pygame.display.set_mode((width, height)) creates the window and returns a
Surface object. A Surface is pygame's word for a rectangular grid of pixels
you can draw onto. The window's surface is special because pygame copies it to the
screen on every flip().
screen.fill((R, G, B)) paints every pixel of the surface with one colour.
Colours in pygame are tuples of three integers, each 0–255, representing red,
green, and blue intensity. (0, 0, 0) is black; (255, 255, 255) is white;
(255, 0, 0) is pure red.
pygame.display.flip() copies the surface to the physical screen buffer. If
you draw something but forget to call flip(), nothing appears. You'll call this
once per frame in the game loop.
pygame.event.get() drains the event queue — the list of things that happened
since the last call: key presses, mouse clicks, window close requests. Checking for
pygame.QUIT is what lets the window close when the user clicks the X button.
Without it, clicking X does nothing and your program just keeps running.
pygame.quit() + sys.exit() shut down the library and end the process. Call
them together; pygame.quit() alone does not exit Python.
The screen.fill() call here is outside the loop, so the colour is painted once
and stays. In a real game you'll fill the background at the start of every loop
iteration to erase the previous frame before drawing the new one.
Where to go next
Next: the game loop — the repeating cycle of input, update, and draw that makes everything move.