Hardware Reference
In-Depth Information
sys.exit()
def checkForEvent(): # see if you need to quit
global pieDirection, position, start, ppCount
event = pygame.event.poll()
if event.type == pygame.KEYDOWN :
start = True
if event.key == pygame.K_ESCAPE :
terminate()
if event.key == pygame.K_DOWN :
if pieDirection == 0 or pieDirection == 2: #check under
if wallHt2(position,0,pieMan + 1,pieMan):
pieDirection = 6
else :
pieDirection = 6
if event.key == pygame.K_UP :
pieDirection = 4
if event.key == pygame.K_RIGHT :
pieDirection = 2
if event.key == pygame.K_LEFT :
pieDirection = 0
if event.key == pygame.K_p : # cheat - power up at will
ppCount = 0
elif event.type == pygame.QUIT :
terminate()
if __name__ == '__main__':
main()
Unlike the other listings, which can be placed anywhere in the ile you are building up, Listing 6-8
has to be at the end - or at least the last line in this listing must be the last line in your ile. here
is the terminate function that shuts things down when you press the Esc key or close the
Pygame window, which is the same for many programs in this topic. It is the checkForEvent
function that actually provides the user with the controls to play the game - namely, the cursor
keys. hese mainly look at keyboard events and control the direction of Pie Man through the
global variable pieDirection . Primarily, the key simply changes the direction number, but in
the case of Pie Man going horizontally and wanting to turn down, an extra check of the wall
underneath Pie Man must be made to avoid its walking through a wall. Putting the check here
means you don't have to do it in the normal direction checks that have to be done on every step;
you need to do this only on a turn in the downward direction from a left- or right-moving situa-
tion. Note as soon as any key is pressed a global variable start is set to be true. his is used at the
Search WWH ::




Custom Search