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

(singke) #1
#
##########################################
#
##### Import Modules & Variables ######
import pygame #Import PyGame library
import sys #Import System module
#
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 Color ################
#
blue = 0,0,255
#
# Set up the Game Image Graphics #######
#
GameImage="/usr/share/raspberrypi-artwork/raspberry-pi-logo.png"
GameImageGraphic=pygame.image.load(GameImage).convert_alpha()
#
GameImageLocation=GameImageGraphic.get_rect() #Current location
#
ImageOffset=[10,10] #Moving speed
#
# Set up the Game Sound ################
#
ClickSound=pygame.mixer.Sound('/home/pi/python_games/match1.wav')
#
#
###### Play the Game ######################################
#
while True:
for event in pygame.event.get():
if event.type in (QUIT,MOUSEBUTTONDOWN):
ClickSound.play()
pygame.time.delay(300)
sys.exit()
#Move game image
GameImageLocation=GameImageLocation.move(ImageOffset)
#Draw screen images
GameScreen.fill(blue)
GameScreen.blit(GameImageGraphic,GameImageLocation)
#Update game screen
pygame.display.update()
#


  1. Test your game script by exiting the editor, typing python3
    py3prog/script1902.py, and pressing Enter. If you get any syntax errors, fix
    them. If you don’t get any errors, watch as the Raspberry Pi image moves...right off
    the screen! Click anywhere in the game screen with your mouse to play a sound and
    end the game.

  2. To keep the Raspberry Pi image on the game screen, open script1902.py in a
    script editor again. The first change you need to make this little tweak to the

Free download pdf