Page 86 - Programming the Raspberry Pi Getting Started with Python
P. 86

Note that because the condition for the if is so long, we use the line-continuation command (\) to
          break it into two lines.
             The  function check_for_catch adds 1 to the score if the raspberry has fallen as far as the spoon
          (raspberry_y >= spoon_y) and the x position of the raspberry is between the x (left) position of the
          spoon and the x position of the spoon plus 50 (roughly the width of the business end of the spoon).
             Regardless of whether the raspberry is caught, the score is displayed using the display function.

          The check_for_catch function is also added into the main loop as one more thing we must do each
          time around the loop.
             The ‘display’ function is responsible for displaying a message on the screen.






             You write text on the screen in pygame by creating a font, in this case, of no specific font family

          but of a 36-point size and then create a text object by rendering the contents of the string message
          onto the font. The value (10, 10, 10) is the text color. The end result contained in the variable text
          can then be blitted onto the screen in the usual way.

          Timing
          You may have noticed that nothing in this program controls how fast the raspberries fall from the sky.
          We are lucky in that they fall at the right sort of speed on a Raspberry Pi. However, if we were to run
          this game on a faster computer, they would probably fly past far too fast to catch.
             To manage the speed, pygame has a built-in clock that allows us to slow down our main loop by just
          the  right  amount  to  perform  a  certain  number  of  refreshes  per  second. Unfortunately,  it  can’t  do
          anything to speed up our main loop. This clock is very easy to use; you simply put the following line
          somewhere before the main loop:


             This creates an instance of the clock. To achieve the necessary slowing of the main loop, put the
          following line somewhere in it (usually at the end):

             In this case, we use a value of 30, meaning a frame rate of 30 frames per second. You can put a
          different value in here, but the human eye (and brain) do not register any improvement in quality
          above about 30 frames per second.
          Lots of Raspberries
          Our  program  is  starting  to  look  a  little  complex. If we were to add the facility for more than one
          raspberry at this stage, it would become even more difficult to see what is going on. We are therefore

          going  to  perform refactoring,  which  means  changing  a  perfectly  good  program  and  altering  its
          structure without changing what it actually does or without adding any features. We are going to do
          this  by  creating  a  class  called Raspberry to do all the things we need a raspberry to do. This  still
          works with just one raspberry, but will make working with more raspberries easier later. The  code
          listing  for  this  stage  can  be  found  in  the  file  08_05_rasp_game_refactored.py. Here’s  the  class
          definition:
   81   82   83   84   85   86   87   88   89   90   91