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

(singke) #1

Click here to view code image


GameScreen.blit(GameTextGraphic,(100,100))
pygame.display.update()

The text graphic GameTextGraphic is the first argument to the statement; it tells the .blit
module what is to be displayed on the screen. The second argument, (100,100), is the location on
the game screen where the text should be displayed. Finally, the pygame.display.update()
function displays the game screen and the graphics it contains on the screen.


Reading about all this can be rather confusing. Trying it yourself will help you understand these
concepts. Remember that one of the great features of game programming is that you get quick
feedback. Therefore, in the following Try It Yourself, you are going to build a game screen and
display text on it.


Try It Yourself: Create a Game Screen and Display Text Using PyGame
In the following steps, you will import and initialize the PyGame library, set up a
game screen, and display a simple test message on that screen. You will do all this via
a Python game script you create, which will be used as a basis for the other Try It
Yourself section in this hour. Follow these steps:


  1. If you have not already done so, power up your Raspberry Pi and log in to the
    system.

  2. If you do not have the LXDE GUI started automatically at boot, start it now by typing
    startx and pressing Enter.

  3. Open a script editor, such as nano or the IDLE 3 text editor, and create the script
    py3prog/script1901.py.

  4. Type all the information for script1901.py shown below. Take your time and
    avoid any typographical errors:
    Click here to view code image
    #script1901.py - Simple Game Screen & Text
    #Written by


    ##########################################



    Import Modules & Variables

    import pygame #Import PyGame library



    from pygame.locals import * #Load PyGame constants



    pygame.init() #Initialize PyGame



    Set up the Game Screen



    ScreenSize=(1000,700) #Screen size variable
    GameScreen=pygame.display.set_mode(ScreenSize)



    Set up the Game Colors



    black = 0,0,0
    white = 255,255,255
    blue = 0,0,255



Free download pdf