Python and Pygame - Teacher Tutorial Preview
Python and Pygame - Teacher Tutorial Preview
Games
Programming
(using Python and Pygame)
G. Reid, 2015
Advanced Higher Programming
How to Use this Booklet
This tutorial contains a worked example of a Pygame program. The tutorial contains the complete code
required to create the game along with numerous program comment lines explaining the function of each
line of code. By working through this tutorial you will gain an understanding of how a PyGame program
is created.
Starting with the supplied PyGame Template file, work through each stage in turn adding the code
highlighted using red boxes. The purpose of each additional section of code is explained through the
green comment lines typed next to the code.
A copy of the code at each of the 10 stages of the games production is supplied with the tutorial.
The tutorial has been written to aid staff in the delivery of the four booklets in the series: Advanced
Higher Programming (which is sold as Games Programming using Python and Pygame). This series
comprises four project booklets.
Project 1 - Balloon Burst
Project 2 - Tile Match
Project 3 - Centipede
Project 4 - Galaxians
The four booklets have been written to cover the following content in Scotlands Advanced Higher
Computing Qualification but could be used as part of any Games Programming course or club.
Advanced Higher
Languages and Environments Object Orientated
Programming Paradigms object
encapsulation
method
property
class
inheritance
Imperative
variables
sequence
selection
iteration
modularity
1 Copyright Mr G. Reid, Buckhaven High School & Mr D. Stott, Kirkland High School and Community College - April 2015
Advanced Higher Programming - INSET Training
Introduction
The plan for this tutorial is to create a simple computer game using Python and Pygame.
Requirements for writing a PyGame program are:
A suitable Python editor (PyScripter)
Python 3.4
The PyGame library files
During the coding of a PyGame program the programmer will code sprite objects, set up a game
window (size and background) and then code how the sprites will behave (appear, move, collide etc).
Arguably the most important part of a PyGame program is the Main Program Loop. While a PgGame
program is running this loop senses any input (a mouse click, key press etc), controls the sprites
(creates, moves or deletes sprites, reacts to collisions) and then redraws the game windows contents.
The Main Program Loop repeats a set number of times per second. A complete new window is drawn
each loop meaning that a running PyGame program is effectively creating video frames. The
template file is set to run at 20 frames per second by the command clock.tick(20).
SDD 2
Advanced Higher Programming - INSET Training
Dodge
This tutorial will create a simple computer game, Dodge, using the PyGame Template file.
In the game Dodge, apples will fall from the sky at regular intervals. A superhero character must
dodge the falling apples. Dodge will be scored by awarding the player a point for each apple
successfully dodged and will finish when an apple hits the superhero character.
To complete the game you will be required to code the following stages:
1. Create the game window with an appropriate background.
2. Make apple sprites appear randomly at the top of the window.
3. Add movement to make the apple sprites fall down the window.
4. Control the frequency with which apples appear.
5. Delete the apple sprites when they reach the bottom of the window.
6. Create a superhero character, sprite, for the game.
7. Control the character is moved using keyboard input.
8. Create a scoring system in the game.
9. Display the score in the game window.
10. End the game when the character collides with an apple.