Hardware Reference
In-Depth Information
nd = 0
else:
if deltay < 0:
nd = 6
else:
nd = 4
if powerPlay: # reverse direction
nd ^= 0x2
return nd
Here the function getNewDir takes in the ghost number and irst works out the delta, or dif-
ference, between Pie Man and the selected ghost. his could be a positive or negative value
depending on what side of the ghost Pie Man is on. herefore, when you test these delta values,
you need to discard the sign information and just look at the absolute magnitude, which is
done with the abs function. he code then sets the direction number to be in the direction of
the largest delta. he last few lines of this function check if there is a power play in force, and if
there is, it reverses the direction. his is done in a rather clever way. If you refer back to Figure
6-3 and the numbers associated with direction and if you look at bit 1 of the number (remem-
ber you start numbering bits from zero, so bit 1 is the middle bit of the three), you will see that
for any given direction number, if bit 1 is inverted, the direction is changed to the opposite
direction. So by using the exclusive OR operation, ^ , you can reverse the direction number no
matter what it is. his clever code line saves you from writing many lines of if tests.
Drawing the Screen
he functions for updating the screen are quite short because most of the heavy lifting has already
been done, which is fortunate as this needs to be done every step. his is shown in Listing 6-7.
Listing 6-7 Drawing the Screen
def drawScreen(p,g1p,g2p) : # draw to the screen
screen.blit(background,[screenOffset,0]) # draw background
screen.blit(pillsLayer,[screenOffset,0]) # draw pills
screen.blit(piPicture[pieDirection ^ step],[p[0],p[1]])
if powerPlay:
screen.blit(ghostbPicture[gStep],[g1p[0],g1p[1]])
screen.blit(ghostbPicture[gStep],[g2p[0],g2p[1]])
else :
screen.blit(ghost1Picture[gStep],[g1p[0],g1p[1]])
screen.blit(ghost2Picture[gStep],[g2p[0],g2p[1]])
# blank out exit tunnels
pygame.draw.rect(screen, cBackground, (0,285, ;
continued
 
Search WWH ::




Custom Search