Operation Manual

if youre using IDLE, these spaces will be inserted automatically, but if youre using a text editor, you will need to insert the
spaces yourself. After the final line of the functionsys.exit()you can stop indenting.
The gameOver function uses a selection of pygame’s commands to perform a simple task: write the words Game Over to the
screen in a large font, pause for 5 seconds, and then quit both pygame and Python itself. It may seem strange to set up the
instructions for quitting the game before the game has even begun, but functions should always be defined before they are called.
Python wont execute these instructions until it is told to do so using the newly-created gameOver instruction.
With the beginning of the program complete, its time to start the main section. This takes place in an infinite loopa while loop
that never exits. This is so that the game can continue to run until the player dies by hitting a wall or eating his or her own tail.
Begin the main loop with the following line:
while True:
Without anything to evaluate, Python will check to see if True is true. Because thats always the case, the loop will continue to
run foreveror, at least until you tell Python to quit out of the loop by calling the gameOver function.
Continue the program with the following lines, paying attention to the indentation levels:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
The first line, which comes right after the while loop begins, should be indented four spacesbut its a loop of its own, using a
for instruction to check for pygame events like key presses. As a result, the line under that needs to be indented an additional
four spaces for a total of eightbut that line, too, is a loop, using an if instruction to check whether the user has pressed a key.
As a result, the next line—pygame.quit()is indented an additional four spaces for a total of 12 spaces. This logical
progression of indentation tells Python where each loop begins and ends, which is important: if the wrong number of spaces is
used, the program wont work correctly. This is why using a development environment like IDLE, which attempts to
automatically indent code where required, can be easier than using a plain text editor to create Python programs.
An if loop tells Python to check to see if a particular evaluation is true. The first check, if event.type == QUIT, tells
Python to execute the indented code below if pygame reports a QUIT message (which happens when the user presses the
Escape key). The two lines beneath that should be familiar from the gameOver function: they tell pygame and Python to close
down and exit.
The line beginning elif is used to extend if loops. Short for else if, an elif instruction is evaluated when a previous if
instruction was found to be false. In this case, the elif instruction is used to see if pygame is reporting a KEYDOWN event, which
is returned when the user is pressing a key on the keyboard. As with the if instruction, code to be executed when an elif is
true should be indented by an additional four spaces plus whatever indentation the elif instruction itself has. Type the following
lines to give the elif instruction something to do when the user presses a key:
if event.key == K_RIGHT or event.key == ord(d):
changeDirection = right
if event.key == K_LEFT or event.key == ord(a):
changeDirection = left
if event.key == K_UP or event.key == ord(w):
changeDirection = up
if event.key == K_DOWN or event.key == ord(s):
changeDirection = down
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
These instructions modify the value of the changeDirection variable, used to control the direction the players snake is
travelling during the game. Using or with an if statement allows more than one evaluation to be made. In this case, it provides
two ways of controlling the snake: the player can use the cursor keys, or the W, A, S and D keys to make the snake go up, right,
down or left. Until a key is pressed, the snake travels to the right according to the value set for changeDirection at the start of
the program.
If you look back at the variables you initialised at the start of the program, youll see that there’s another variable called
direction. This is used alongside changeDirection to see if the instruction the user has given is valid. The snake should not
be allowed to turn immediately back on itselfif it does, the snake dies and the game is over. To prevent this from happening,
the direction requested by the playerstored in changeDirectionis compared to the current direction in which the snake is