Pygame tutorials

Fifteen lessons that take you from an empty window to a scrolling platformer. Every code example is a complete program you can download and run.

Beginner path

Start here if you know basic Python but have never made a game. Each lesson builds on the one before.

Getting started

Install Pygame and open your first window. The ten lines every game starts with.

Beginner4 min

The game loop

Events, update, draw. Frame rate, delta time and smooth movement.

Beginner4 min

Drawing shapes

Rectangles, circles, lines, polygons and arcs, combined into a scene.

Beginner3 min

Keyboard input

KEYDOWN events for single actions, get_pressed() for smooth movement.

Beginner3 min

Mouse input

Clicks, position, dragging and the scroll wheel in a paint program.

Beginner3 min

Images and sprites

Load images, convert_alpha(), and organise objects with Sprite and Group.

Beginner3 min

Intermediate skills

Animation, collisions, physics, sound and game structure, the parts that turn a demo into a game.

Sprite animation

Sprite sheets, frame timing and animation states.

Intermediate2 min

Collision detection

Rects, circles, masks, sprite groups and resolving overlaps.

Intermediate3 min

Game physics

Gravity, velocity, friction, bouncing and fixed time steps.

Intermediate3 min

Sound and music

Sound effects, background music, volume and channels.

Intermediate3 min

Game menus

Title, pause and game-over screens with a state machine.

Intermediate2 min

Score system

Combos, score popups and a saved high score.

Intermediate2 min

Complete games

Larger systems that combine everything: tile-based worlds and a full platformer.

Platformer game

Tiles, gravity, responsive jumps and a scrolling camera.

Advanced2 min

Tile maps

Grid levels, CSV loading, pre-rendering and scrolling.

Advanced2 min

How to use these tutorials#

The lessons are ordered: each assumes you've done the ones before it, and each ends with exercises. Doing the exercises, even badly, is where most of the learning happens. When a lesson's code works, change one number and predict what will happen before you run it again.

What you need: Python 3 with Pygame or pygame-ce installed (installation guides) and a code editor. If you can't install anything right now, the editor examples run in Pygame Online.

What you should already know: variables, if statements, for and while loops, functions and lists. Classes are introduced gently from the sprites lesson onward. If Python itself is new to you, the official Python tutorial covers everything you need in its first nine chapters.

Python concepts you'll meet#

ConceptWhere it's used
Loops and conditionsevery game loop, event handling
Functionsdrawing helpers, resetting a game
Lists and dictionariescollections of enemies, game state
Classes and inheritancepygame.sprite.Sprite subclasses
f-stringsscore and debug text
pathlib.Pathfinding image and sound files
jsonsaving high scores and settings

After the tutorials#

Read the source of the game projects: each is a single, commented file. The API reference explains the modules in more depth, and the blog covers packaging, performance and publishing your game.

↑ ↓ to moveEnter to openEsc to close