Hardware Reference
In-Depth Information
Listing 6-9 continued
cBackground = ( (i & 1)*255, ((i>>1) & 1)*255,;
((i>>2) & 1)*255)
drawWords(str(hiScore),screenOffset+playWidth, ;
textHeight*2)
pygame.display.update()
time.sleep(0.2)
cBackground = (0,0,0)
drawWords(str(hiScore),screenOffset+playWidth,textHeight*2)
pygame.display.update()
time.sleep(2.0)
here is a lot going on here, so let's walk through it. Listing 6-9 starts of by initialising the
sound, the window and the drawing planes - all stuf you have seen before. he lines
pygame.event.set_allowed(None)
pygame.event.set_allowed([pygame.KEYDOWN,pygame.QUIT])
restrict the number and type of events that can be generated. his is important because the
event bufer can be illed up with mouse-movement events and other things you are not
interested in. As these events are checked only once per step, it could make the game seem
insensitive to the control keys. Next, the following lines associate each Pie Man sprite with
the correct image number that is shown in Figure 6-3:
picName = [ “pie1left”, “pie2left”, “pie1right”, ;
“pie2right”, “pie1up”, “pie2up”, “pie1down”, “pie2down” ]
piPicture = [ pygame.image.load(“pies/”+picName[frame];
+”.png”).convert_alpha() for frame in range(0,8)]
his works by irst generating a list of image names and then using that list to load in the
image iles into the piPicture list, with the index number matching the image number
that you want. hen come a whole slew of variable deinitions that control the game.
The Game Begins
he main function is where it all happens. he part between the irst two while statements
sets up the game by initialising a lot of variables; you can't rely on the values they got in the
global deinitions because these will have changed by the time you play the second game. he
second while statement pauses the program at this point until a key is pressed. You will
remember that any key press event sets the variable start to true. So when it is the game
proper starts. he irst thing that happens is the variable ghostRelease is set to the current
time plus 4 seconds, which means the player has four seconds of hassle-free pill munching.
Search WWH ::




Custom Search