Pygame code examples

Short, complete programs that each demonstrate one idea. Copy them, download them, or open the browser-ready ones in Pygame Online.

3 min readUpdated
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#

ExampleWhat it showsExplained in
hello_window.pythe smallest working windowGetting started
first_window.pyclock, shapes and textGetting started
game_loop_dt.pydelta time and FPS capsThe game loop
Hello Pygame runs onlineasync loop for the browserPygame Online

Drawing and graphics#

ExampleWhat it showsExplained in
drawing_scene.pya scene made with pygame.drawDrawing shapes
draw_example.pyevery draw function and optionpygame.draw
surface_example.pycolorkey, alpha and subsurfacespygame.Surface
transform_example.pyscaling, flipping, rotatingpygame.transform
Drawing shapes runs onlineanimated arc and shapesDrawing shapes

Input#

ExampleWhat it showsExplained in
keyboard_input.pyevents versus held keys, jumpingKeyboard input
mouse_paint.pya paint program with the mouse wheelMouse input
event_example.pycustom events and timerspygame.event
Keyboard movement runs onlinesmooth 8-direction movementKeyboard input
Mouse interaction runs onlinehover and dragMouse input

Sprites, animation and collisions#

ExampleWhat it showsExplained in
sprites_basics.pyloading images, Sprite and GroupImages and sprites
sprite_animation.pysprite sheets and frame timingSprite animation
collision_types.pyrect, circle and mask collisionsCollision detection
sprite_example.pygroupcollide with circlespygame.sprite
Collision detection runs onlinelive collision testsCollision 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#

ExampleWhat it showsExplained in
physics_basics.pygravity, bouncing, fixed time stepGame physics
math_example.pyVector2 steering towards the mousepygame.math
Moving rectangle runs onlinedelta-time movementThe game loop
Bouncing ball runs onlinevelocity and gravityGame physics

Sound, text and timing#

ExampleWhat it showsExplained in
sound_basics.pyeffects and looping musicSound and music
mixer_example.pygenerated sound, panningpygame.mixer
font_example.pyfonts, measuring and caching textpygame.font
time_example.pytimers and cooldownspygame.time

Game structure#

ExampleWhat it showsExplained in
catch_game.pya complete small gameBuild your first game
game_states.pymenus, pause and game overMenus and game states
score_highscore.pycombos and a saved high scoreScore system
tilemap_scroll.pytile levels and cameraTile maps
display_example.pySCALED windows and fullscreenpygame.display
rect_example.pyRect attributes and helperspygame.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.

↑ ↓ to moveEnter to openEsc to close