Hardware Reference
In-Depth Information
Death of a Pie Man - or a Ghost
Next, you look at the situation in which a ghost and Pie Man collide, which results in a death;
however, whose death it is depends on the mode. If the power play is in force, the ghost dies.
Otherwise, the ghost kills Pie Man. He is turned into a ghost because he is killed, and that
ghost then dies. his conveniently allows you to use the same animated sequence for any
sort of death. his is the only part of the game that relies on knowing where the objects are
rather than looking at the screen pixels. his is done by creating a rectangle for Pie Man and
the two ghosts, moving that rectangle to match the current playing position and then calling
the collide-rectangle function in Pygame, which returns a logic value of true if the two rect-
angles overlap in any way. he functions that do this are shown in Listing 6-5.
Listing 6-5 Detecting a Touch
def kill(p,g1,g2): # see if man and ghosts are touching
global ghostPos, lives, position, ghostRelease, gNumber
playRect = pygame.Rect(0,0,pieMan,pieMan)
g1Rect = pygame.Rect(0,0,pieMan,pieMan)
g2Rect = pygame.Rect(0,0,pieMan,pieMan)
playRect = playRect.move(p[0],p[1])
g1Rect = g1Rect.move(g1[0],g1[1])
g2Rect = g2Rect.move(g2[0],g2[1])
if playRect.colliderect(g1Rect):
if powerPlay:
ghostDieSound.play()
updateScore(150)
ghostDisolve(g1)
ghostPos[0]=[526,298]
if playRect.colliderect(g2Rect):
if powerPlay:
ghostDieSound.play()
updateScore(260)
ghostDisolve(g2)
ghostPos[1]=[422,298]
if (playRect.colliderect(g1Rect) or playRect.colliderect;
(g2Rect) ) and not powerPlay:
pieDieSound.play()
ghostDisolve(p)
time.sleep(0.4)
lives -= 1
updateLives()
ghostPos = copy.deepcopy(ghostInitalPos) ;
# return ghosts to base
position = [screenOffset +pieIncrement*2, ;
continued
Search WWH ::




Custom Search