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 theyre native instructions. As a result, you will need to do less typing when using these
instructions. Other module namessuch as pygame.clock, which is separate to pygame.localswill still need to be typed
in full.
Enter the next two lines to set up pygame so its 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 surfacethe canvas onto which in-game objects will be
drawnwith 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 isnt 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 games variables so theyre ready for use. This is an important step, because if these
variables are left blank when the game begins, Python wont know what to do. Dont worry about what each variable does for
nowjust 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 variablessnakePosition, snakeSegments and raspberryPositionare set to a list of comma-
separated values. This causes Python to create the variables as listsa number of different values stored in a single variable
name. Later, youll see how you can access individual values stored in a list.
Next, you need to define a new functiona 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 youll 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