Skip to content

Commit

Permalink
An attempt at AI to make party characters follow the hero around.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Fiano committed Jan 21, 2010
1 parent 6f0ff08 commit 38a3109
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
38 changes: 36 additions & 2 deletions lib/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, screen, width, height, start_direction, direction,
self.height = height
self.start_direction = start_direction
self.direction = direction
self.stopped = stopped
self.stop = stopped
self.sprite = Spritesheet('char', 'sprites', spritesheet)
images = {
'north': [
Expand Down Expand Up @@ -180,11 +180,45 @@ def __init__(self, screen, hero, char):
self.face_small = load_image("char", "faces", char + "_small")
self.hero = hero
hero_position = (hero.rect[0], hero.rect[1])
self.position = (hero_position[0],hero_position[1]+32)
self.position = (hero_position[0],hero_position[1]+64)
CharacterSprite.__init__(self, screen, CHAR_WIDTH, CHAR_HEIGHT,
start_direction, None, True, self.position, char, (32,32),
(32,32), 3, 3)

def move_check(self):
"""Check if party character is near the player."""

directions = [
self.rect.move(0, -16),
self.rect.move(0, 16),
self.rect.move(-16, 0),
self.rect.move(16, 0) ]
if len(pygame.Rect(self.hero.rect).collidelistall(directions)) == 1:
self.stop = True
else:
self.stop = False

def move(self):
if self.rect[0] > self.hero.rect[0]:
self.rect.move_ip([-PLAYER_WALK_SPEED, 0])
elif self.rect[0] == self.hero.rect[0]:
pass
else:
self.rect.move_ip([PLAYER_WALK_SPEED, 0])

if self.rect[1] > self.hero.rect[1]:
self.rect.move_ip([0, -PLAYER_WALK_SPEED])
elif self.rect[1] == self.hero.rect[1]:
pass
else:
self.rect.move_ip([0, PLAYER_WALK_SPEED])

def update(self):
self.dirty = 1
self.move_check()
if not self.stop and not self.hero.stop:
self.move()


class PlayerSprite(CharacterSprite):
"""The sprite for the character the player controls."""
Expand Down
6 changes: 6 additions & 0 deletions lib/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def player_input(self, moving, name, key):
self.player.direction = (self.move_keys[-1])
else: self.player.stop = True

def run_ai(self):
# Party movement AI
for char in self.screen.party.chars:
if not char is "hero":
self.screen.party.chars[char].move()

def show_debug(self, fps):
BaseState.show_debug(self, fps)
if SHOW_RECTS:
Expand Down

0 comments on commit 38a3109

Please sign in to comment.