Pygame code examples
Short, complete programs that each demonstrate one idea. Copy them, download them, or open the browser-ready ones in Pygame Online.
On this page
Every example below is a complete program tested with pygame-ce 2.5.8 and pygame 2.6.1. Each links to the tutorial or reference page that explains it. Examples marked runs online use the browser-compatible loop and open directly in Pygame Online.
Getting started#
| Example | What it shows | Explained in |
|---|---|---|
| hello_window.py | the smallest working window | Getting started |
| first_window.py | clock, shapes and text | Getting started |
| game_loop_dt.py | delta time and FPS caps | The game loop |
| Hello Pygame runs online | async loop for the browser | Pygame Online |
Drawing and graphics#
| Example | What it shows | Explained in |
|---|---|---|
| drawing_scene.py | a scene made with pygame.draw | Drawing shapes |
| draw_example.py | every draw function and option | pygame.draw |
| surface_example.py | colorkey, alpha and subsurfaces | pygame.Surface |
| transform_example.py | scaling, flipping, rotating | pygame.transform |
| Drawing shapes runs online | animated arc and shapes | Drawing shapes |
Input#
| Example | What it shows | Explained in |
|---|---|---|
| keyboard_input.py | events versus held keys, jumping | Keyboard input |
| mouse_paint.py | a paint program with the mouse wheel | Mouse input |
| event_example.py | custom events and timers | pygame.event |
| Keyboard movement runs online | smooth 8-direction movement | Keyboard input |
| Mouse interaction runs online | hover and drag | Mouse input |
Sprites, animation and collisions#
| Example | What it shows | Explained in |
|---|---|---|
| sprites_basics.py | loading images, Sprite and Group | Images and sprites |
| sprite_animation.py | sprite sheets and frame timing | Sprite animation |
| collision_types.py | rect, circle and mask collisions | Collision detection |
| sprite_example.py | groupcollide with circles | pygame.sprite |
| Collision detection runs online | live collision tests | Collision detection |
The image files used by the sprite examples are in the ZIP downloads on their tutorial pages: images and sprites and sprite animation.
Movement and physics#
| Example | What it shows | Explained in |
|---|---|---|
| physics_basics.py | gravity, bouncing, fixed time step | Game physics |
| math_example.py | Vector2 steering towards the mouse | pygame.math |
| Moving rectangle runs online | delta-time movement | The game loop |
| Bouncing ball runs online | velocity and gravity | Game physics |
Sound, text and timing#
| Example | What it shows | Explained in |
|---|---|---|
| sound_basics.py | effects and looping music | Sound and music |
| mixer_example.py | generated sound, panning | pygame.mixer |
| font_example.py | fonts, measuring and caching text | pygame.font |
| time_example.py | timers and cooldowns | pygame.time |
Game structure#
| Example | What it shows | Explained in |
|---|---|---|
| catch_game.py | a complete small game | Build your first game |
| game_states.py | menus, pause and game over | Menus and game states |
| score_highscore.py | combos and a saved high score | Score system |
| tilemap_scroll.py | tile levels and camera | Tile maps |
| display_example.py | SCALED windows and fullscreen | pygame.display |
| rect_example.py | Rect attributes and helpers | pygame.Rect |
Generate your own starting point#
The game loop generator and Pygame code generator build starter programs from a few choices, and the game projects are complete games to learn from.