Operation Manual

At this point in the program, its possible that the player has eaten a raspberry. A game in which only a single raspberry is
available is boring, so type the following lines to add a new raspberry back to the playing surface if the player has eaten the
existing raspberry:
if raspberrySpawned == 0:
x = random.randrange(1,32)
y = random.randrange(1,24)
raspberryPosition = [int(x*20),int(y*20)]
raspberrySpawned = 1
This section of code checks to see if the raspberry has been eaten by testing if the raspberrySpawned variable is set to 0, and
if so, the code picks a random location on the playing surface using the random module you imported at the start of the program.
This location is then multiplied by the size of a snakes segment20 pixels wide and 20 pixels tallto give Python a place on
the playing field to position the new raspberry. Its important that the location of the raspberry is set randomly: this prevents the
player from learning where the raspberry will appear next. Finally, the raspberrySpawned variable is set back to 1, to make
sure that there will only be a single raspberry on the playing surface at any given time.
Now you have the code required to make the snake move and grow, and cause raspberries to be eaten and createda process
known in gaming as respawning. However, nothing is being drawn to the screen. Type the following lines:
playSurface.fill(blackColour)
for position in snakeSegments:
pygame.draw.rect(playSurface,whiteColour,Rect
(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface,redColour,Rect
(raspberryPosition[0], raspberryPosition[1], 20, 20))
pygame.display.flip()
These tell pygame to fill in the background of the playing surface in black, draw the snakes head and body segments in white,
and finally, draw a raspberry in red. The last line, pygame.display.flip(), tells pygame to update the screenwithout this
instruction, items will be invisible to the player. Every time you finish drawing objects onto the screen, remember to use
pygame.display.flip() so the user can see the changes.
Currently, its impossible for the snake to die. A game where the player can never die would rapidly get boring, so enter the
following lines to set up some scenarios for the snakes death:
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameOver()
if snakePosition[1] > 460 or snakePosition[1] < 0:
gameOver()
The first if statement checks to see if the snake has gone off the playing surface vertically, while the second if statement checks
if the snake has gone off the playing surface horizontally. In either case, its bad news for the snake: the gameOver function,
defined earlier in the program, is called to print a message to the screen and quit the game. The snake should also die if its head
hits any portion of its body, so add the following lines:
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and
snakePosition[1] == snakeBody[1]:
gameOver()
The for statement runs through each of the snake segments locations, from the second list entry to the end of the list, and
compares it to the current position of the snakes head. Its important to start the comparison at the second entry using
snakeSegments[1:] and not the first. The first entry is always set to the position of the head, and starting the comparison here
would result in instant death for the snake as soon as the game begins.
Finally, all that is required for the game to be complete is to control the speed using the fpsClock variable. Without the variable,
which you created at the start of the program, the game would run too quickly to play. Type in the following line to finish the
program:
fpsClock.tick(20)
If you think the game is too easy or too slow, you can increase this number; or if the game is too hard or too fast, decrease the
number. Save the program as raspberrysnake.py, and run it either by using IDLEs Run Module option in the Run menu or
from the terminal by typing python raspberrysnake.py. The game will start as soon as it has loaded (see Figure 11-6), so