Operation Manual
travelling—stored in direction. If they are opposite directions, the instruction is ignored and the snake continues in the same
direction as before. Type the following lines to set up the comparisons:
if changeDirection == ‘right’ and not
direction == ‘left’:
direction = changeDirection
if changeDirection == ‘left’ and not
direction == ‘right’:
direction = changeDirection
if changeDirection == ‘up’ and not
direction == ‘down’:
direction = changeDirection
if changeDirection == ‘down’ and not
direction == ‘up’:
direction = changeDirection
With the user’s input checked to make sure it makes sense, the snake—which appears on the screen as a series of blocks—can
be moved. During each turn, the snake moves a distance equal to the size of one of its blocky segments. With each segment
measuring 20 pixels, you can tell pygame to move the snake a single segment in any direction. Type in the following code:
if direction == ‘right’:
snakePosition[0] += 20
if direction == ‘left’:
snakePosition[0] -= 20
if direction == ‘up’:
snakePosition[1] -= 20
if direction == ‘down’:
snakePosition[1] += 20
The += and -= operators are used to change the value of a variable by a certain amount: += sets the variable to its previous value
plus the new value, while -= sets the variable to its previous value minus the new value. By way of example,
snakePosition[0] += 20 is a shorthand way of writing snakePosition[0] = snakePosition[0] + 20. The number in
square brackets following the snakePosition variable name is the position in the list being affected: the first value in the
snakePosition list stores the snake’s position along the X axis, while the second value stores the position along the Y axis.
Python begins counting at zero, so the X axis is controlled with snakePosition[0] and the Y axis with snakePosition[1].
If the list were longer, additional entries could be affected by increasing the number: [2], [3] and so on.
Although the snakePosition list is always two values long, another list created at the start of the program is not:
snakeSegments. This list stores the location of the snake’s body, behind the head. As the snake eats raspberries and grows
longer, this list increases in size and provides the difficulty in the game: as the player progresses, it becomes harder to avoid
hitting the body of the snake with the head. If the head hits the body, the snake dies and the game is over. Type the following line
to make the snake’s body grow:
snakeSegments.insert(0,list(snakePosition))
This uses the insert instruction to insert a new value into the snakeSegments list: the current position of the snake. Each time
Python reaches this line, it will increase the length of the snake’s body by one segment, and locate that segment at the current
position of the snake’s head. To the player, it will look as though the snake is growing. However, you only want this to happen
when the snake eats a raspberry—otherwise the snake will just grow and grow. Type the following lines:
if snakePosition[0] == raspberryPosition[0]
and snakePosition[1] == raspberryPosition[1]:
raspberrySpawned = 0
else:
snakeSegments.pop()
The first instruction checks the X and Y coordinates of the snake’s head to see if it matches the X and Y coordinates of the
raspberry—the target the player is chasing. If the values match, the raspberry is considered to have been eaten by the snake—
and the raspberrySpawned variable is set to 0. The else instruction tells Python what to do if the raspberry has not been
eaten: pop the earliest value from the snakeSegments list.
The pop instruction is simple but clever: it returns the oldest value from the list but also removes it, making the list one item
shorter. In the case of the snakeSegment list, it tells Python to delete the portion of the snake’s body farthest away from the
head. To the player, it will look as though the entire snake has moved without growing—in reality, it grew at one end and shrank
at the other. Because of the else statement, the pop instruction only runs when a raspberry has not been eaten. If a raspberry
has been eaten, the last entry in the list doesn’t get deleted—so the snake grows in size by one segment.