Added walking animation
Justin Armstrong justinmeister@gmail.com
Thu, 06 Mar 2014 23:06:15 -0800
3 files changed,
70 insertions(+),
16 deletions(-)
M
data/components/player.py
→
data/components/player.py
@@ -9,6 +9,9 @@ def __init__(self):
super(Player, self).__init__() self.get_image = setup.tools.get_image self.spritesheet_dict = self.create_spritesheet() + self.animation_lists = self.create_animation_lists() + self.index = 1 + self.image_list = self.animation_lists['down'] self.image = self.spritesheet_dict['facing down 1'] self.rect = self.image.get_rect() self.state_dict = self.create_state_dict()@@ -17,6 +20,7 @@ self.state = 'resting'
self.x_vel = 0 self.y_vel = 0 self.direction = 'up' + self.timer = 0.0 def create_spritesheet(self):@@ -40,6 +44,25 @@
return dict + def create_animation_lists(self): + """Create the lists for each walking direction""" + image_dict = self.spritesheet_dict + + left_list = [image_dict['facing left 1'], image_dict['facing left 2']] + right_list = [image_dict['facing right 1'], image_dict['facing right 2']] + up_list = [image_dict['facing up 1'], image_dict['facing up 2']] + down_list = [image_dict['facing down 1'], image_dict['facing down 2']] + + dict = {'left': left_list, + 'right': right_list, + 'up': up_list, + 'down': down_list} + + return dict + + + + def create_state_dict(self): """Creates a dictionary of all the states the player can be in"""@@ -60,8 +83,11 @@
return dict - def update(self): + def update(self, keys, current_time): """Updates player behavior""" + self.keys = keys + self.current_time = current_time + self.check_for_input() state = self.state_dict[self.state] state()@@ -69,6 +95,8 @@
def resting(self): """When the player is not moving between tiles. Checks if the player is centered on a tile""" + self.image = self.image_list[self.index] + assert(self.rect.y % 32 == 0), ('Player not centered on tile: ' + str(self.rect.y)) assert(self.rect.x % 32 == 0), ('Player not centered on tile'@@ -80,18 +108,54 @@ """When the player is moving between tiles"""
self.rect.x += self.x_vel self.rect.y += self.y_vel + if (self.current_time - self.timer) > 100: + if self.index < (len(self.image_list) - 1): + self.index += 1 + else: + self.index = 0 + self.timer = self.current_time + + self.image = self.image_list[self.index] + + if self.rect.x % 32 == 0 and self.rect.y % 32 == 0: + self.begin_resting() + + assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \ + 'Not centered on tile' + def begin_moving(self, direction): """Transitions the player into the moving state""" self.state = 'moving' self.direction = direction - self.x_vel, self.y_vel = self.direction_dict[self.direction] + self.image_list = self.animation_lists[direction] + self.timer = self.current_time + + if self.rect.x % 32 == 0: + self.y_vel = self.direction_dict[self.direction][1] + if self.rect.y % 32 == 0: + self.x_vel = self.direction_dict[self.direction][0] def begin_resting(self): """Transitions the player into the resting state""" self.state = 'resting' - self.x_vel, self.y_vel = 0 + self.index = 1 + self.x_vel = self.y_vel = 0 + + + def check_for_input(self): + """Checks for player input""" + if self.state == 'resting': + if self.keys[pg.K_UP]: + self.begin_moving('up') + elif self.keys[pg.K_DOWN]: + self.begin_moving('down') + elif self.keys[pg.K_LEFT]: + self.begin_moving('left') + elif self.keys[pg.K_RIGHT]: + self.begin_moving('right') +
M
data/setup.py
→
data/setup.py
@@ -16,7 +16,7 @@ os.environ['SDL_VIDEO_CENTERED'] = '1'
pg.init() pg.event.set_allowed([pg.KEYDOWN, pg.KEYUP, pg.QUIT]) pg.display.set_caption(ORIGINAL_CAPTION) -SCREEN = pg.display.set_mode((800, 600)) +SCREEN = pg.display.set_mode((800, 608)) SCREEN_RECT = SCREEN.get_rect() FONTS = tools.load_all_fonts(os.path.join('resources', 'fonts'))
M
data/states/town.py
→
data/states/town.py
@@ -167,8 +167,7 @@ def update(self, surface, keys, current_time):
"""Updates state""" self.keys = keys self.current_time = current_time - self.check_for_player_input() - self.player.update() + self.player.update(keys, current_time) self.draw_level(surface)@@ -181,16 +180,7 @@
surface.blit(self.level_surface, (0,0), self.viewport) - def check_for_player_input(self): - """Checks for player input""" - if self.keys[pg.K_UP]: - self.player.begin_moving('up') - elif self.keys[pg.K_DOWN]: - self.player.begin_moving('down') - elif self.keys[pg.K_LEFT]: - self.player.begin_moving('left') - elif self.keys[pg.K_RIGHT]: - self.player.begin_moving('right') +