Menu

Pygame Examples: Small Games You Can Run and Edit

Four complete pygame example games in under 80 lines each: catch the falling coins, Snake, Pong against the computer, and a cave flyer. Play each one on the page, read the code, and change it.

This page includes runnable editors. Edit, run, and see output instantly.

Four complete games, each short enough to read in a few minutes. Press Play, click the game to give it the keyboard, and play. Then change a number and play again: the best way to learn pygame is to break a working game and fix it.

Catch the coins

The arrow keys move a basket; catch coins, miss three and the game is over. It uses Rect collisions, a random spawn and a game over state.

raise SystemExit ends the program from inside the loop, and the window closes with it. It saves the running flag in a short game.

Snake

The snake moves one grid cell every few frames. The body is a list of cells: each step adds a new head and removes the tail, unless the snake just ate.

The check new[0] != -direction[0] or new[1] != -direction[1] stops the snake from turning straight back into itself. frame % 7 sets the speed: a smaller number is a faster snake.

Pong against the computer

You play the left paddle with Up and Down; the computer follows the ball, a little slower than it could.

Each hit speeds the ball up by 5%. score[ball.left > 480] uses the comparison as an index: False is 0 and True is 1.

Cave flyer

Space lifts the drone; gravity pulls it down; rock columns slide past with a gap to fly through.

drone.collidelist(rocks) returns the index of a rock the drone touches, or -1 when it touches none, so one line checks every rock.

Where to take them

The pygame playground has more example games and a larger editor for your own.

Frequently Asked Questions

What is a good first pygame project?

A game with one rule and one control: catching falling objects, Pong or Snake. Each fits in under 100 lines and uses the whole loop (input, update, collisions, drawing, score) without needing images.

How do I add a game over screen?

Keep a state variable ("playing" or "over"). In the update step, only move things while playing; in the draw step, show the score and a "press Space" message when over, and reset the game on Space.

Can I use these examples in my own game?

Yes. They are short starting points: copy one into your editor, run it with pygame installed, and change the rules, speeds and colors.

Coddy programming languages illustration

Learn to code with Coddy

Get started