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

(singke) #1
red = 255,0,0
green = 0,255,0
#
# Set up the Game Font ##################
#
DefaultFont=None #Default to PyGame font
GameFont=pygame.font.Font(DefaultFont,60)
#
# Set up the Game Text Graphic #########
#
GameText="Hello"
GameTextGraphic=GameFont.render(GameText,True,white)
#
###### Draw the Game Screen & Add Game Text #####
#
GameScreen.fill(blue)
GameScreen.blit(GameTextGraphic,(100,100))
pygame.display.update()
#


  1. Test your game script by exiting the editor, typing python3
    py3prog/script1901.py, and pressing Enter. If you get any syntax errors, fix
    them. If you don’t get any errors, you probably just saw the game screen with its text
    appear briefly on the screen and then disappear. (You will address this in the next
    step.)

  2. Open script1901.py in a script editor. Under the import pygame line, add
    import time to import the time module, as shown here:


Click here to view code image


##### Import Modules & Variables ######
import pygame #Import PyGame library
import time #Import Time module
#


  1. On the very last line of script1901.py, add the line time.sleep(10). This
    causes your Python game script to pause, or “sleep,” for 10 seconds after it writes
    the game screen to the monitor.

  2. Now test your modifications by exiting the editor, typing python3
    py3prog/script1901.py, and pressing Enter. You should now see (at least
    for 10 seconds) the game screen and your text displayed. (You will learn later this
    hour how to control the screen display without using the time module.)

  3. Just for fun, open script1901.py in a script editor again. This time, add a new
    color, called RazPiRed, to the game colors, as shown here:


Click here to view code image


# Set up the Game Colors ################
#
black = 0,0,0
white = 255,255,255
blue = 0,0,255
red = 255,0,0
RazPiRed = 210,40,82
green = 0,255,0
#
Free download pdf