Hardware Reference
In-Depth Information
he 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 snake's head. It's
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 IDLE's 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 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-
rypiuserguide2e . Downloading the source code from the website will save you some typ-
ing, but entering the code by hand is a good way of ensuring that you understand what each
Search WWH ::




Custom Search