Program Arcade Games
With Python And Pygame

Language
English
Russian - Русский
Turkish - Türkçe

Example code and programs

Download all these examples along with supporting files in a zip file: python_examples.zip.

Non-graphic Python Examples

MPG
Program that calculates miles-per-gallon. (Chapter 1)
calculate_miles_per_gallon.py
MPG
Calculates the kinetic energy of an object. (Chapter 1)
calculate_kinetic_energy.py
MPG
Simple example if statements. (Chapter 3)
if_statement_examples.py
MPG
Simple example for loops. (Chapter 4)
for_loop_examples.py
MPG
Simple example while loops. (Chapter 4)
while_loop_examples.py
MPG
Simple example for encrypting text. (Chapter 7)
simple_encryption.py
MPG
Simple example for decrypting text. (Chapter 7)
simple_decryption.py

Pygame Graphics Examples

Base template screenshot
This opens up a blank pygame window. It is a good template to use when starting a new program. (Chapter 5)
pygame_base_template.py
move_keyboard.png
This file demonstrates setting up a window and drawing rectangles, polygons, text, and other basic shapes. (Chapter 5)
simple_graphics_demo.py
move_keyboard.png
Demos all the commands in the draw module.
draw_module_example.py
move_keyboard.png
Animate a bouncing rectangle around the screen. (Chapter 8)
bouncing_rectangle.py
move_keyboard.png
This shows how to use an array to animate and track multiple objects. In this case, snow flakes. (Chapter 8)
animating_snow.py
move_keyboard.png
This file draws a snowman inside of a function. The program can then call the draw snowman function and draw several snowmen easily. (Chapter 9)
functions_and_graphics.py
move_keyboard.png
Move an object with the mouse (Chapter 11)
move_mouse.py
move_keyboard.png
Move an object with the keyboard (Chapter 11)
move_keyboard.py
move_keyboard.png
Move an object with the game controller (Chapter 11)
move_game_controller.py
move_keyboard.png
Show how to read everything off a gamepad/controller/joystick. (Chapter 11)
joystick_calls.py
move_keyboard.png
Display bitmapped images (png, jpg) to a screen. Make sounds. (Chapter 12)
bitmapped_graphics.py
move_keyboard.png
Display a board based on a two-dimensional grid. Useful when creating games like tic-tac-toe, minesweeper, memory-match, connect-four, etc. (Chapter 17)
array_backed_grid.py
move_keyboard.png
Display one or more pages of instruction before the game starts.
instruction_screen.py
move_keyboard.png
Plays background music. When the music is over, an event is triggered and a different song starts. The music is included in the downloadable zip file, or you can follow the links in the comments to download the music.
background_music.py
move_keyboard.png
Shows how to display a centered "Game Over" message, and stop game play when a game is over.
game_over.py
move_keyboard.png
Display a line sweeping around in a circle like a radar.
radar_sweep.py
move_keyboard.png
Count up and count down timers. (Chapter 21)
timer.py
Recursive Rectangles
Using recusion to make nested rectangles. (Chapter 20)
recursive_rectangles.py
fractal
Using recusion to make fractals. (Chapter 20)
fractal.py
cannons
Using Trigonometry (off-site link) - If you want things that bounce, wave, or even pointing cannons, then follow this link to Al Sweigart's page and read up on using trigonometry with your code.
timer.py

Pygame Sprite Examples

Move a sprite with the mouse and collect blocks. (Chapter 14)
sprite_collect_blocks.py
Same as collect blocks example, but all the sprites move. (Chapter 14)
moving_sprites.py
Expands the prior example to show how to manage a game with levels. Level advances when all the blocks are cleared.
sprite_collect_blocks_levels.py
Same as sprite_collect_blocks.py but with a black circle instead of a block. (Chapter 14)
sprite_collect_circle.py
Same as sprite_collect_blocks.py but with a graphic instead of a block. (Chapter 14)
sprite_collect_graphic.py
Move a sprite around the screen with the mouse. (Chapter 14)
move_sprite_mouse.py
Move a sprite with the keyboard in discrete 'jumps'. (Chapter 14)
move_sprite_keyboard_jump.py
Move a sprite with the keyboard smoothly and continously while a key is pressed. (Chapter 14)
move_sprite_keyboard_smooth.py
Move a sprite with a game controller or joystick. (Chapter 14)
move_sprite_game_controller.py
Basic Pong game using two game controlers.
pong.py
Make a cat animate walking. How to animate a sprite using several images. (You'll need to download the zip file to get the cat images.)
sprite_animation.py
How to manage bullets with sprites.
bullets.py
Move a sprite around the screen, but not let it move through walls.
move_with_walls_example.py
More complex version of prior example, with multi-colored walls, and multiple rooms.
maze_runner.py
Get the player to jump off platforms.
platform_jumper.py
Like platform jumper, but scroll side to side.
platform_scroller.py
Control a snake as it moves around the screen.
snake.py

Longer Game Examples

word_search.py - Shows the beginnings of how to create a grid of letters to be used in a word search. bounce with paddle
bounce_ball_with_paddle.py - Two players bounce a ball back and forth with paddles. Requires two joysticks to work. bounce with paddle
breakout_simple.py - A simple version of Breakout. Shows how to do a "Game Over" message. breakout
cat_adventure.py - A simple 32x32 tile game. Needs the graphic terrain_atlas.png Cat Adventure

Videos of games created by students after taking this course. Spring 2012

spring 2012 videos

Videos of games created by students after taking this course. Spring 2011

spring 2011 videos

Videos of games created by students after taking this course. Fall 2010

Download games - Download and install games created by students.

fall 2010 videos

 

Creating an Installer

Python Pygame Installer Tutorial- Create an installer for your Python program. Allow other people to play your game too!

Searching and Sorting Examples

example_sorted_names.txt - Sample file of names used in searching_example.py

searching_example.py - Example linear and binary searches

AliceInWonderLand.txt - Text of Alice In Wonderland. Source: Project Gutenberg

AliceInWonderLand200.txt

dictionary.txt

sorting_examples.py - Example code for the insertion and selection sorts.

File Examples

high_score.py - Example that shows how to read and write a high score to the disk so that it persists between program runs.

Additional Information

Pygame Website - The main website for Pygame

Pygame Documentation - Documentation on the commands availabe for Pygame


You are not logged in. Log in here and track your progress.