Raspberry Pi A User Manual page 208

Hide thumbs Also See for Raspberry Pi A:
Table of Contents

Advertisement

192
P A R T I I I
P R O G R A M M I N G W I T H T H E R A S P B E R R Y P I
The
statement runs through each of the snake segments' locations, from the second list
for
entry to the end of the list, and compares it to the current position of the snake's head. It's
important to start the comparison at the second entry using
and not
snakeSegments[1:]
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
variable. Without the variable, which you created at the start of the program, the
fpsClock
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
, and
raspberrysnake.py
run it either by using IDLE's Run Module option in the Run menu or from the terminal by
typing
. The game will start as soon as it has loaded (see
python raspberrysnake.py
Figure 12-6), so make sure you're ready!
Figure 12-6:
Playing
Raspberry Snake
on the
Raspberry Pi
A full copy of the program listing for Raspberry Snake is included in Appendix A, "Python
Recipes", and on the Raspberry Pi User Guide website at
www.wiley.com/go/raspber-
. Downloading the source code from the website will save you some typ-
rypiuserguide2e
ing, but entering the code by hand is a good way of ensuring that you understand what each

Hide quick links:

Advertisement

Table of Contents
loading

Related Products for Raspberry Pi Raspberry Pi A

This manual is also suitable for:

Raspberry pi b

Table of Contents