[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] AI bug
I'm working on a sort of RPG/Roguelike game, and right now I'm have
problems with the monster's AI. It works just fine if the Ninja (The
player) is above, below, or to the left of the monster, but if it's tot
he right, the monster just goes up or down, but not right. Here's the
directional code, are there any problems people can see? (For the
monster's direction, 1 = right, 2 = up, 3 = left, and 4 = down)
if self.ninja.rect.x < monster.rect.x and monster.rect.x -
self.ninja.rect.x > monster.rect.y - self.ninja.rect.y:
monster.direction = 1
elif self.ninja.rect.x > monster.rect.x and monster.rect.x -
self.ninja.rect.x > monster.rect.y - self.ninja.rect.y:
monster.direction = 3
if self.ninja.rect.y < monster.rect.y and monster.rect.y -
self.ninja.rect.y > monster.rect.x - self.ninja.rect.x:
monster.direction = 2
elif self.ninja.rect.y > monster.rect.y and monster.rect.y -
self.ninja.rect.y > monster.rect.x - self.ninja.rect.x:
monster.direction = 4
elif self.ninja.rect.y < monster.rect.y and monster.rect.x ==
self.ninja.rect.x:
monster.direction = 2
elif self.ninja.rect.y > monster.rect.y and monster.rect.x ==
self.ninja.rect.x:
monster.direction = 4
elif self.ninja.rect.x < monster.rect.x and self.ninja.rect.y ==
monster.rect.y:
monster.direction = 1
elif self.ninja.rect.x > monster.rect.x and self.ninja.rect.y ==
monster.rect.y:
monster.direction = 3
If you can't see anything, thanks anyway!