Hardware Reference
In-Depth Information
start of the game on the irst key press. Finally, there is a hidden cheat key, much in keeping with
early computer games. Of course, because this is typed in a listing, it's not that “hidden”, anyway:
By pressing the P key, you can enter a power play at any time, so if the ghost is about to get you,
you can quickly turn the tables. It is easy to remove these two lines for competition play.
The Final Function
Now you are in a position to put it all together and deine how the game actually plays.
Basically, there are two nested loops: he outer one runs forever and plays game after game,
whereas the inner one runs for only as long as there are lives left. he main function sets up
all the parameters for a game, and then the inner loop generates the moves and evaluates the
results by calling functions you have already typed in. So go ahead and enter the code in
Listing 6-9 into your ile. You need to place Listing 6-9 at the top of your ile because it
deines all the global variables and sets up the sounds, sprites and windows.
Listing 6-9 The Global Variables and main Function
#!/usr/bin/env python
“””
Pie Man
A Raspberry Pi Game
“””
import time # for delays
import os, pygame, sys
import random, copy
pygame.init() # initialise graphics interface
pygame.mixer.quit()
pygame.mixer.init(frequency=22050, size=-16, channels=2, ;
buffer=512)
eatSound = pygame.mixer.Sound(“sounds/eatPill.ogg”)
ppSound = pygame.mixer.Sound(“sounds/powerPill.ogg”)
ghostDieSound = pygame.mixer.Sound(“sounds/ghostDie.ogg”)
pieDieSound = pygame.mixer.Sound(“sounds/pieDie.ogg”)
pieStartSound = pygame.mixer.Sound(“sounds/pieStart.ogg”)
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
pygame.display.set_caption(“Pie Man”)
pygame.event.set_allowed(None)
pygame.event.set_allowed([pygame.KEYDOWN,pygame.QUIT])
textHeight = 36
font = pygame.font.Font(None, textHeight)
screenWidth = 1000
Search WWH ::




Custom Search