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

(singke) #1

#script1903.py - The Raspberry Pie Game
#Written by Blum and Bresnahan
#######################################



Import Modules & Variables

import pygame #Import PyGame library
import random #Import Random module
import sys #Import System module



from pygame.locals import * #Local PyGame constants



pygame.init() #Initialize PyGame objects



Set up Functions


Delete a Raspberry


def deleteRaspberry (RaspberryDict, RNumber):
key1 = 'RasLoc' + str(RNumber)
key2 = 'RasOff' + str(RNumber)



#Make a copy of Current Dictionary
NewRaspberry = dict(RaspberryDict)



del NewRaspberry[key1]
del NewRaspberry[key2]



return NewRaspberry



Set up the Game Screen



ScreenSize = ScreenWidth,ScreenHeight = 1000,700
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()
GameImageGraphic=pygame.transform.scale(GameImageGraphic,(75,75))



GameImageLocation=GameImageGraphic.get_rect() #Current location



ImageOffset=[10,10] #Starting Speed



Build the Raspberry Dictionary



RAmount = 17 #Number of Raspberries on screen
Raspberry = {} #Initialize the dictionary



for RNumber in range(RAmount): #Create the Raspberry dictionary
Position_x = (ImageOffset[0] + RNumber) random.randint(9,29)
Position_y = (ImageOffset[1] + RNumber)
random.randint(8,18)
RasKey = 'RasLoc' + str(RNumber)
Location = GameImageLocation.move(Position_x, Position_y)
Raspberry[RasKey] = Location
RasKey = 'RasOff' + str(RNumber)
Raspberry[RasKey] = ImageOffset



Setup Game Sound

Free download pdf