Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
the game. You’ve created a nice collide point, but the Raspberry Pi image is so large
that clicking it is hardly a game.


  1. Open script1902.py in a script editor and change the size of the Raspberry Pi
    image. To do this, under where you load the image and convert it using the
    .convert_alpha method, add the following two lines to make the Raspberry Pi
    image smaller:
    Click here to view code image

    Resize image (make smaller)


    GameImageGraphic=pygame.transform.scale(GameImageGraphic,(75,75))



  2. Exit the editor, type python3 py3prog/script1902.py, and press Enter to
    test this change. Does the Raspberry Pi image seem a lot smaller? It should. Chase
    the image around the screen until you can click somewhere on it to play a sound and
    end the game. Now you need one more tweak: You are going to make it a little harder
    to catch that raspberry.

  3. Make the Raspberry Pi image move “faster” every time it hits a screen wall. To do
    this, open script1902.py in your favorite script editor. Change the whole “Keep
    game image on screen” section so that it matches the following:
    Click here to view code image
    #Keep game image on screen
    if GameImageLocation.left < 0 or GameImageLocation.right > ScreenWidth:
    ImageOffset[0] = -ImageOffset[0]
    #Speed it up
    if ImageOffset[1] < 0:
    ImageOffset[1] = ImageOffset[1] - 1
    else:
    ImageOffset[1] = ImageOffset[1] + 1


    if GameImageLocation.top < 0 or GameImageLocation.bottom > ScreenHeight:
    ImageOffset[1] = -ImageOffset[1]
    #Speed it up
    if ImageOffset[0] < 0:
    ImageOffset[0] = ImageOffset[0] - 1
    else:
    ImageOffset[0] = ImageOffset[0] + 1




  4. Exit the editor, type python3 py3prog/script1902.py, and press Enter to
    test the last change. Let the Raspberry Pi image hit the game screen walls several
    times before you begin chasing it with the mouse pointer. Does it appear to speed up?
    Careful! Don’t wait too long to chase it, or you may never catch it!


That was fun. But this hour is almost over, and you have only touched your little toe into the Python
gaming world’s ocean. Listing 19.7 is a last gift to help you on your way: the Raspberry Pie game.


LISTING 19.7 The Raspberry Pie Game Script


Click here to view code image

Free download pdf