Skip to content
Tutorials Center
My learning

Topics

Pygame Tutorial

Learn Pygame step by step: 41 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 Introduction to Pygame Meet Pygame, the Python library for 2D games. Learn what it does, how pygame and pygame-ce differ, what a game loop is, and run your first headless pygame code.
  2. 2 Installing Pygame Install pygame-ce or pygame with pip on Windows, macOS and Linux, use a virtual environment, verify the install with a short script and fix the common errors.
  3. 3 Import and Initialize Pygame Learn what pygame.init() does, how to initialize only some modules, read its success and failure counts, check get_init() and shut down cleanly with pygame.quit().
  4. 4 Your First Pygame Window Create your first Pygame window step by step, set its size and background color, run a minimal loop that closes on QUIT, and see what each line does.

Window, Loop and Events

  1. 5 The Window and Display Control the Pygame window - size, caption, icon, resizable and fullscreen modes, vsync, scaling a fixed-size game to any window, and flip() versus update().
  2. 6 The Game Loop Build a correct Pygame game loop. Handle events, update, draw, cap the frame rate, move with delta time so speed is the same on every computer, and use a fixed time step.
  3. 7 Clock and Time in Pygame Master pygame.time - Clock.tick and get_fps, tick_busy_loop, get_ticks for cooldowns and countdowns, wait and delay, and when to use timers instead of sleeping.
  4. 8 Event Handling in Pygame Understand the Pygame event queue. Read events with event.get, check event.type, handle key, mouse, wheel and window events, and organize handlers with a dispatch table.
  5. 9 Custom Events and Timers Create your own Pygame events with custom_type and USEREVENT, post them to the queue, carry data in attributes, and use set_timer for spawning enemies and repeating actions.
  6. 10 Keyboard and Mouse Input in Pygame Read the keyboard and mouse in Pygame. Use key.get_pressed for smooth movement, normalize diagonal speed, track the mouse, detect hovers and clicks, and build a text input box.

Drawing and Images

  1. 11 Surfaces and Blitting Learn the Pygame Surface and blit(). Create surfaces, copy one onto another, control draw order, use per-pixel alpha, subsurfaces, convert() and fill for fast rendering.
  2. 12 Colors and Rects Work with pygame.Color and pygame.Rect - RGB, names, hex, HSVA and lerp, plus Rect position attributes, move_ip, inflate, clamp, union, clip and the collision methods.
  3. 13 Drawing Shapes in Pygame Draw with pygame.draw - filled and outlined rectangles, rounded corners, circles, ellipses, lines, polygons and arcs. Learn line width, antialiasing and animated colors.
  4. 14 Text and Fonts in Pygame Render text in Pygame with Font and SysFont, blit and center it, measure widths for layout, wrap long paragraphs, cache rendered text and show scores and FPS counters.
  5. 15 Images and Transforms in Pygame Load and display images in Pygame, read their size, scale, rotate and flip them, rotate around the center, make an image face the mouse, and cache transformed images.

Sprites and Collision

  1. 16 Moving Objects and Boundaries Move objects with Rects and keys in Pygame, then handle the edges of the screen three ways - clamp, wrap around and bounce - and leave a trail to draw with arrow keys.
  2. 17 Sprites and Groups in Pygame Organize game objects with pygame.sprite.Sprite and Group. Subclass Sprite, give it image and rect, update and draw whole groups, remove with kill, and use layers.
  3. 18 Collision Detection in Pygame Detect collisions in Pygame with Rect.colliderect, circle collisions, spritecollide and groupcollide. Shrink hitboxes, choose a collision shape and avoid tunneling.
  4. 19 Collision Response and Walls React to collisions in Pygame. Block a player at walls by resolving x and y separately, slide along edges, bounce a ball off a paddle by hit position, and find which side was hit.
  5. 20 Masks and Pixel-Perfect Collision Use pygame.mask for pixel-perfect collision. Build masks from surfaces, test overlap and overlap_area, use collide_mask with sprites, find outlines and combine with rect checks.

Sound and Input

  1. 21 Sound Effects and Music in Pygame Learn pygame.mixer: load sound effects, stream background music, control volume and channels, handle end-of-music events and build a small sound manager.
  2. 22 Input Maps, Held Keys and Mouse Dragging in Pygame Go beyond basic input in Pygame: events versus held-key polling, key bindings in a dictionary for remapping, mouse wheel and button events, and drag and drop with offsets.
  3. 23 Gamepad and Touch Input in Pygame Add controller support to Pygame: detect joysticks, read axes and buttons, handle dead zones and hot-plugging, and unify keyboard, gamepad and touch input.
  4. 24 Buttons and Text Input Boxes in Pygame Build clickable buttons with hover states and a text input box with a cursor, backspace and focus in Pygame, plus reusable widget classes you can drop in.

Game Physics and Animation

  1. 25 Movement and Delta Time in Pygame Move objects at a speed that does not depend on frame rate: delta time with Clock.tick, float positions, boundaries, bouncing and a fixed time step loop.
  2. 26 Gravity, Jumping and Platforms in Pygame Make a character fall and jump in Pygame: gravity as acceleration, jump impulse, ground and platform collision, coyote time, jump buffering and variable jump height.
  3. 27 Vectors, Angles and Trigonometry in Pygame Use pygame.math.Vector2 for movement, normalized direction, distance, aiming with atan2, rotation, projectiles and circular motion in your games.
  4. 28 Sprite Sheet Animation in Pygame Animate characters in Pygame: slice a sprite sheet into frames, switch animations by state, time frames with delta time, flip for direction and build an Animation class.
  5. 29 Particles and Visual Effects in Pygame Add polish to Pygame games: particle systems, snowfall, explosions, color breathing, screen shake and math art such as Rhodonea curves and the Maurer rose.

Game Architecture

  1. 30 Game States, Start Menus and Pause Screens Structure a Pygame game with a state machine: start menu, playing, paused and game over screens, keyboard-navigable menus, state stacks and clean transitions.
  2. 31 Organizing Pygame Code: Classes, Modules and Assets Structure a growing Pygame project: modules, a Game class, entity classes, settings, an asset loader with caching, configuration files and simple event messaging.
  3. 32 Tilemaps and Scrolling Cameras in Pygame Build levels from tiles in Pygame: map data in text or JSON, drawing only visible tiles, tile collision, a camera that follows the player and clamps to the world.

Packaging and Next Steps

  1. 33 Saving High Scores and Settings in Pygame Persist game data in Pygame: JSON high score tables, user settings, safe file locations per operating system, atomic writes, validation and handling corrupt save files.
  2. 34 Pygame Performance, Profiling and Debugging Make Pygame games faster and fix bugs: convert surfaces, show FPS, profile with cProfile, use spatial grids for collisions, avoid per-frame allocation and debug with logging.
  3. 35 Packaging and Sharing Your Pygame Game Turn a Pygame project into something others can play: asset paths that survive packaging, PyInstaller executables, web builds with pygbag, checklists and publishing on itch.io.

Projects and Practice

  1. 36 Project: Build the Snake Game with Pygame Build Snake in Pygame step by step: grid movement, timed steps, growth, food, collisions, score, game over and restart, with the game logic separated from drawing.
  2. 37 Project: Tic Tac Toe with Pygame and an Unbeatable AI Build a mouse-driven Tic Tac Toe in Pygame: board logic, click-to-cell mapping, win detection, a minimax AI that never loses, game-over screen and restart.
  3. 38 Project: Visualize Sorting and Searching Algorithms Build an algorithm visualizer in Pygame: generators that yield each step, bar charts for bubble, insertion and selection sort, binary and ternary search, speed controls.
  4. 39 Project: Build a Sudoku Game with Pygame Build Sudoku in Pygame: board model, rule checking, backtracking solver with the most-constrained-cell heuristic, puzzle generator with unique solutions, mouse and keyboard GUI.
  5. 40 Project: A Complete Platformer Game in Pygame Build a tile-based platformer in Pygame: level from text, player physics with coyote time, coins, spikes, moving enemies, scrolling camera, lives and a win condition.
  6. 41 Pygame Practice Exercises and Project Ideas Practice Pygame with self-checking exercises on movement, collisions, animation timing, grids, scores and easing, plus ten game project ideas with outlines and milestones.