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

(singke) #1
By the Way: Colors
You can create just about any color in a game by using the RGB settings.
Several sites show samples of various colors, along with the RGB settings to
achieve them. One such site is http://www.tayloredmktg.com/rgb/.


  1. Use the new color for the game screen’s background by changing blue to
    RazPiRed in the fill module attribute, as shown here:
    GameScreen.fill(RazPiRed)

  2. Change the font by setting the variable DefaultFont to FreeSans, as shown
    here:
    Click here to view code image
    DefaultFont='/usr/share/fonts/truetype/freefont/FreeSans.ttf'
    (There are some fonts installed by default on your Raspberry Pi. One of them is
    FreeSans. You can use any installed font instead of the PyGame default font.)

  3. To make things more interesting, change that boring text message from "Hello" to
    "I love my Raspberry Pi!", as shown here:
    Click here to view code image
    GameText="I love my Raspberry Pi!"

  4. Test your latest game modifications by exiting the editor, typing python3
    py3prog/script1901.py, and pressing Enter. You should now see (at least
    for 10 seconds) a screen similar to that in Figure 19.1.


FIGURE 19.1 The script1901.py game screen.

Good job! You can see the benefits of the instant feedback of writing game scripts. To get more
experience, try changing the screen’s background color and the location of the text message and see
what effects those changes have.


Learning More About PyGame


Displaying colors and text on a screen is fun, but it doesn’t exactly make a game. You need to learn a
few more basic concepts before you can write game scripts.


Staying in the Game


As you saw in the Try It Yourself section, a Python game script displays the game’s screen and then
exits. So how do you keep the game running? You use a loop construct and the pygame.event

Free download pdf