Hi everyone,
having trouble coming up with the right code for 8 directional movement [ up, upright, right, downright, down, downleft, left, upleft]
My keypress handling is:
if event.type == KEYDOWN:
if event.key == K_LEFT:
player.moveLeft = True
player.moveRight = False
if event.key == K_RIGHT:
player.moveRight = True
player.moveLeft = False
if event.key == K_UP:
player.moveUp = True
player.moveDown = False
if event.key == K_DOWN:
player.moveDown = True
player.moveUp = False
if event.type == KEYUP:
if event.key == K_LEFT:
player.moveLeft = False
if event.key == K_RIGHT:
player.moveRight = False
if event.key == K_UP:
player.moveUp = False
if event.key == K_DOWN:
player.moveDown = False
And my directional checking is:
if self.moveRight == True and self.moveUp == True:
print "MOVE UPRIGHT"
if self.moveRight == True and self.moveDown == True:
print "MOVE DOWNRIGHT"
if self.moveRight == True and self.moveUp == False and self.moveDown == False:
print "MOVE RIGHT"
if self.moveLeft == True and self.moveUp == True:
print "MOVE UPLEFT"
if self.moveLeft == True and self.moveDown == True:
print "MOVE DOWNLEFT"
if self.moveLeft == True and self.moveUp == False and self.moveDown == False:
print "MOVE LEFT"
if self.moveUp == True:
print "MOVE UP"
if self.moveDown == True:
print "MOVE DOWN"
It worked fine until I added the last two statements for moving up and down: It fires off MOVE UP and MOVE UPRIGHT at the same time, for example.
This also seems more complicated than it needs to be. Can you help make this work, and if possible, help me find a more efficient, easier solution?
Thanks