Page 85 - Programming the Raspberry Pi Getting Started with Python
P. 85
You should already be familiar with the first two items in this list, so let’s start with the new
functions:
The function update_spoon just takes the code we had in the main loop in
08_02_rasp_game_mouse and puts it in a function of its own. This helps to keep the size of the main
loop down so that it is easier to tell what’s going on.
The function update_raspberry changes the values of raspberry_x and raspberry_y. It adds 5 to
the y position to move the raspberry down the screen and moves the x position by a random amount
between –5 and +5. This makes the raspberries wobble unpredictably during their descent. However,
the raspberries will eventually fall off the bottom of the screen, so once the y position is greater than
the position of the spoon, the function moves them back up to the top and to a new random x position.
There is also a danger that the raspberries may disappear off the left or right side of the screen.
Therefore, two further tests check that the raspberries aren’t too near the edge of the screen, and if
they are then they aren’t allowed to go any further left or right.
Here’s the new main loop that calls these new functions:
Try out 08_03_rasp_game_one. You will see a basically functional program that looks like the
game is being played. However, nothing happens when you catch a raspberry.
Catch Detection and Scoring
We are now going to add a message area to display the score (that is, the number of raspberries
caught). To do this, we must be able to detect that we have caught a raspberry. The extended program
that does this is in the file 08_04_rasp_py_game_scoring.py.
The main changes for this version are two new functions, check_for_catch and display: