Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 41 ยท Projects and Practice

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.

You learn game programming by making games, and the fastest way to make games is to practice the small skills that every game uses. This final lesson has two parts: seven self-checking exercises you can run right here, and ten project ideas with milestones so you always know what to build next.

Every exercise is a function with tests. The editor starts with a deliberately incomplete function, so the first run prints FAIL lines. Write the missing code until every test prints PASS. Stuck? Set SHOW_SOLUTION = True at the top to run the reference solution, then read it, close it and try again from scratch later.

How to work through an exercise

flowchart LR
  A["Read the goal"] --> B["Run: see FAIL"]
  B --> C["Edit the function"]
  C --> D["Run: fewer FAIL?"]
  D -- "no" --> C
  D -- "all PASS" --> E["Peek at the solution<br/>and compare"]

The tests are your safety net: change one thing, run, read the failures.

Exercise 1: Movement direction without the diagonal speed-up

Write velocity(left, right, up, down, speed) that returns (vx, vy) in pixels per second. Opposite keys cancel out, and moving diagonally must have the same speed as moving straight (length speed). Hint: normalize the direction vector, as in Vectors and Angles.

Python Exercise 1: velocity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2: Which side did I hit?

Write hit_side(mover, wall) for two rectangles given as (x, y, w, h). It returns "left", "right", "top", "bottom" or None, describing the side of the wall the mover touches. Use the smallest overlap: whichever of the four overlaps is smallest is the side of contact. This is the heart of platform collision.

Python Exercise 2: collision side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 3: Which animation frame is it?

Write frame_index(elapsed, fps, count, loop) that returns the index of the frame to show after elapsed seconds. For a looping animation the index wraps around; for a non-looping one it stops on the last frame. See Sprite Sheet Animation.

Python Exercise 3: frame index

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 4: Wrap around the screen

In a Snake variant with wrap-around walls, a head that leaves one edge reappears on the opposite one. Write move_wrapped(pos, direction, cols, rows) for grid coordinates. Python's % operator handles negative numbers the way you need here.

Python Exercise 4: wrap-around movement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 5: A top-three leaderboard

Write insert_score(table, name, score, limit=3) returning a new list of (name, score) pairs sorted from highest to lowest and trimmed to limit entries. When two scores tie, the earlier entry stays ahead. Do not modify the input. Compare with Saving High Scores and Settings.

Python Exercise 5: leaderboard

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 6: Easing

Smooth motion looks better than linear motion. Write ease_out(t) for t from 0 to 1 using the formula 1 - (1 - t)^3 (fast at the start, slow at the end), and lerp(a, b, t). Then tween(a, b, elapsed, duration) returns the value after elapsed seconds, clamped so that it never overshoots. Use it for menu slides and fades (particles and effects).

Python Exercise 6: easing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 7: A pause stack

Implement press_escape(stack), which returns a new stack of game states. In "play" Esc pushes "pause". In "pause" it pops back. In "menu" it does nothing. See Game States and Menus.

Python Exercise 7: pause stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Ten project ideas with milestones

Choose one and build it in small steps. Each milestone gives you a playable game, so you are never stuck with a half-finished pile of code.

Project Milestones New skill
Pong 1. Two paddles and a ball. 2. Bounce off walls and paddles. 3. Score and serve. 4. Simple AI paddle. 5. Sound. Collision response, AI
Breakout 1. Paddle and ball. 2. Grid of bricks. 3. Remove bricks on hit. 4. Lives, levels. 5. Power-ups. Rect lists, levels
Flappy-style game 1. Gravity and flap. 2. Scrolling pipes. 3. Gap collision. 4. Score and best score. Procedural obstacles
Space Invaders 1. Ship and bullets. 2. Row of aliens moving as a block. 3. Alien shots. 4. Shields and waves. Sprite groups, timing
Asteroids 1. Rotating ship with thrust. 2. Wrap-around screen. 3. Splitting rocks. 4. Particles. Vectors, rotation
Memory card match 1. Grid of face-down cards. 2. Flip on click. 3. Match logic. 4. Timer and moves. State, timers
Minesweeper 1. Hidden grid of mines. 2. Neighbor counts. 3. Reveal with flood fill. 4. Flags, win and lose. Recursion, grids
Conway's Game of Life 1. Grid of cells. 2. Rules for the next generation. 3. Click to toggle cells. 4. Speed controls. Cellular automata
Tetris 1. Falling piece. 2. Rotation. 3. Locking and line clearing. 4. Levels, next piece preview. Matrix rotation
Top-down shooter 1. WASD movement. 2. Aim at the mouse. 3. Enemies chase. 4. Health, waves, tilemap level. Camera, pathing

How to keep growing

  • Finish small games. A finished Pong teaches more than an abandoned RPG.
  • Read other people's code. Pygame's own examples (python -m pygame.examples.aliens) are good study material.
  • Game jams (such as the Pygame community jams or Ludum Dare) force you to ship in days.
  • Explore tools: Tiled for levels, Aseprite for art, pygame-menu and pygame_gui for ready-made interface widgets, pymunk for real physics.
  • Move on when ready: Pygame teaches the fundamentals that transfer to Godot, Unity or Love2D, and nothing stops you from making a professional 2D game with it.

Congratulations on finishing the track.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why normalize the movement direction before multiplying by speed?

    An unnormalized diagonal direction has length about 1.41, giving a speed boost.
  2. 2. In the pause-stack exercise, what should Esc do while the top state is 'menu'?

    Only 'play' pushes the pause screen and only 'pause' pops it.
  3. 3. Which Python feature keeps equal scores in their original order when sorting?

    Python's sort is stable, so entries comparing equal keep their relative order.
  4. 4. What is the best way to choose a first project?

    Small, finished projects with playable milestones build skills and motivation fastest.

Summary

  • Practice small skills (movement, collision sides, frame timing, wrapping, score tables, easing, state stacks) with self-checking functions.
  • Tests tell you immediately whether your function is right; peek at solutions only after trying.
  • Pick a project from the list, split it into playable milestones and finish it.
  • Keep learning through jams, example code and community tools.

Next: Back to the Pygame track