Hardware Reference
In-Depth Information
Listing 6-5 continued
pieIncrement + 2] # position of the pieman
ghostRelease = time.time()+ 5.0 ;
# time for subsequent ghosts
gNumber = 0
def ghostDisolve(p):
for i in range(0,8):
pygame.draw.rect(screen, cBackground, (p[0],p[1], ;
pieMan, pieMan),0)
screen.blit(ghostdPicture[i],[p[0],p[1]])
pygame.display.update()
time.sleep(0.1)
hese two functions do the job of detection. After the rectangles have been created and
moved to the correct position the two ghosts are checked to see if Pie Man has caught up
with them. If this is true and the global variable powerPlay indicates that a power play is in
force, the ghost is killed, the die sound is triggered, the score is updated, the death animation
function ghostDisolve is called and the ghost is returned to its box. Note that the two
ghosts are worth diferent amounts in the score. However, if a power play is not in force and
one of the ghosts has struck a mortal blow to the brave Pie Man, then it is he who dies. One is
subtracted from his total number of lives, the lives display is updated and the players are set
back to their initial starting points. he ghostDisolve function simply draws successive
ghost pictures on the screen. he sleep time controls the speed of this short animation, and
makes it large for a slower, lingering death - or at least lasts as long as your dying sound efect.
The Hunting Pack
Now it is time to look at the control of the ghosts. hey must be set to chase Pie Man but run
away when there is a power play in force. You need to write something that will determine
the movement of the ghosts. Some of this work is done in the main function, but there is
one function that hunts you down, getNewDir , which is shown in Listing 6-6.
Listing 6-6 Finding a Way to You
def getNewDir(g):
deltax = ghostPos[g][0] - position[0]
deltay = ghostPos[g][1] - position[1]
#print deltax,deltay
if abs(deltax) > abs(deltay) and abs(deltax)> pieMan:
if deltax < 0:
nd = 2
else:
 
Search WWH ::




Custom Search