Operation Manual
import pygame, sys, time, random
from pygame.locals import *
The first line imports the main pygame module along with the Python modules sys, time and random, which will also be used in
this program. Typically, a module must then be called by typing its name followed by a full stop and the name of the instruction
from within the module, but the second line in the preceding code tells Python to load all the instructions from the
pygame.locals module as though they’re native instructions. As a result, you will need to do less typing when using these
instructions. Other module names—such as pygame.clock, which is separate to pygame.locals—will still need to be typed
in full.
Enter the next two lines to set up pygame so it’s ready to use in the example program:
pygame.init()
fpsClock = pygame.time.Clock()
The first line tells pygame to initialise itself, and the second line sets up a new variable called fpsClock, which will be used to
control the speed of the game. Next, set up a new pygame display surface—the canvas onto which in-game objects will be
drawn—with the following two lines:
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption(‘Raspberry Snake’)
Next, you should define some colours for the program to use. Although this step isn’t strictly necessary, it again saves on typing:
if you want to set a particular object to be red, you can simply use the redColour variable rather than having to call the
pygame.Color instruction and remember the three colour values for red, green and blue. To define the colours for this example
program, type the following lines:
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
The next few lines initialise some of the game’s variables so they’re ready for use. This is an important step, because if these
variables are left blank when the game begins, Python won’t know what to do. Don’t worry about what each variable does for
now—just type in the following lines:
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
raspberryPosition = [300,300]
raspberrySpawned = 1
direction = ‘right’
changeDirection = direction
Notice that three of the variables—snakePosition, snakeSegments and raspberryPosition—are set to a list of comma-
separated values. This causes Python to create the variables as lists—a number of different values stored in a single variable
name. Later, you’ll see how you can access individual values stored in a list.
Next, you need to define a new function—a fragment of Python code which can be called upon later in the program. Functions
are useful for avoiding code repetition and making the program easier to understand. If you have a particular set of instructions
that are needed at multiple points in the same program, using def to create a function means you’ll only have to type them once
—and only have to change them in a single place if you alter the program later. Type the following lines to define the gameOver
function:
def gameOver():
gameOverFont = pygame.font.Font
(‘freesansbold.ttf’, 72)
gameOverSurf = gameOverFont.render
(‘Game Over’, True, greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (320, 10)
playSurface.blit(gameOverSurf, gameOverRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
As with loops, the code for a function should be indented. Every line after the def instruction should have four spaces at the start