0% found this document useful (0 votes)
287 views4 pages

Python and Pygame - Teacher Tutorial Preview

python

Uploaded by

Salif Ndiaye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
287 views4 pages

Python and Pygame - Teacher Tutorial Preview

python

Uploaded by

Salif Ndiaye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Computing

Science Software Design


and Development

Games
Programming
(using Python and Pygame)

Teacher Tutorial - Dodge

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

Computational Constructs and Explain and Implement the following constructs:


Principles (for software and reading and writing data from sequential files
information systems) reading and writing data to and from databases
Data Types and Structures records, linked lists
2-D arrays
queues, stacks
arrays of records and/or array of objects
Standard Algorithms linear and binary search
selection with two lists
sort algorithms (insertion, bubble, quicksort)

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

How PyGame Works


Pygame is a set of library procedures and functions that are installed as an add-on to the programming
language Python. The files allow games programmers to create and manipulate windows and sprite
objects.
When working through the project booklets pupils are given the basic template file below. This will
also be your starting point.
PyGame Template.py

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.

3 Mr G. Reid, Buckhaven High School - November 2015

You might also like