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

Figure 8-1   Hello Pygame

























             This is a very crude example, and it doesn’t have any way of exiting gracefully. Closing the Python
          console from which this program was launched should kill it after a few seconds.
             Looking at the code for this example, you can see that the first thing we do is import pygame. The
          method init (short for initialize) is then run to get pygame set up and ready to use. We then assign a

          variable called screen using the line

          which creates a new window that’s 200 by 200 pixels. We then fill it with white (the color 255, 255,
          255) on the next line before setting a caption for the window of “Hello Pygame.”
             Games use graphics, which usually means using images. In this example, we read an image file into

          pygame:

             In  this  case,  the  image  is  a  file  called  raspberry.jpg,  which  is  included  along  with  all  the  other
          programs in this book in the programs download section on the book’s website. The call to convert(
          )  at  the  end  of  the  line  is  important  because  it  converts  the  image  into  an  efficient  internal
          representation that enables it to be drawn very quickly, which is vital when we start to make the image
          move around the window.
             Next, we draw the raspberry image on the screen at coordinates 100, 100 using the blit command.
          As with the Tkinter canvas you met in the previous chapter, the coordinates start with 0, 0 in the top-

          left corner of the screen.
             Finally, the last command tells pygame to update the display so that we get to see the image.
   77   78   79   80   81   82   83   84   85   86   87