Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit cd3f88d1c8d13dafc20d31413a27a3410e666d61
parent fcc26c91d8eb70727ba785d8473497633cdaef76
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed, 26 Aug 2020 19:13:50 -0500

animation of vgos now functional

Diffstat:
Msrc/game.py | 3++-
Msrc/images.py | 17++++++++++++++++-
Msrc/vgo.py | 42+++++++++++++++++++++++++++++++++++-------
3 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/src/game.py b/src/game.py @@ -18,7 +18,8 @@ class Game(object): images.convert_sheets(images.SHEETS) #TESTING - self.testvgo = vgo.Entity(images.SHEETS["jisella_1"].sprites[(0, 0)], (100, 100)) + self.testvgo = vgo.Entity(images.SHEETS["jisella_1"], (100, 100)) + self.testvgo.set_animation(images.ANIMATIONS["jisella_1"]["stand"], True, 0) # Utility methods def toggle_on(self): diff --git a/src/images.py b/src/images.py @@ -79,7 +79,22 @@ class Sheet(object): # FILE SOMEWHERE!!! # ####################################### SHEETS = { - "jisella_1" : Sheet("jisella_1.png", (TILE_WIDTH, TILE_HEIGHT), 72) + "jisella_1" : Sheet("jisella_1.png", (TILE_WIDTH, TILE_HEIGHT), 72) +} + +ANIMATIONS = { + "jisella_1" : { + "stand" : [ + { "sprite" : (0, 0), "timer" : 4 }, + { "sprite" : (1, 0), "timer" : 4 }, + { "sprite" : (2, 0), "timer" : 4 }, + { "sprite" : (3, 0), "timer" : 4 }, + { "sprite" : (0, 1), "timer" : 4 }, + { "sprite" : (1, 1), "timer" : 4 }, + { "sprite" : (2, 1), "timer" : 4 }, + { "sprite" : (3, 1), "timer" : 4 } + ] + } } def convert_sheets(sheets): diff --git a/src/vgo.py b/src/vgo.py @@ -5,19 +5,47 @@ class VisibleGameObject(pygame.sprite.Sprite): Extended sprite class used throughout game. """ - def __init__(self, image, pos): + def __init__(self, sheet, pos): pygame.sprite.Sprite.__init__(self) - self.image = image - self.rect = self.image.get_rect() + self.sheet = sheet + self.image = sheet.sprites[(0, 0)] + self.rect = sheet.sprites[(0, 0)].get_rect() self.rect.topleft = pos + # Animations + self.animation = None + self.current_animation_frame = 0 + self.animation_timer = 0 + self.play_animation = False + + def set_animation(self, new_anim, play = False, initframe = 0): + self.animation = new_anim + self.current_animation_frame = initframe + self.animation_timer = new_anim[initframe]["timer"] + self.image = self.sheet.sprites[new_anim[initframe]["sprite"]] + self.play_animation = play + + def animate(self): + + if self.animation_timer > 0: + self.animation_timer -= 1 + else: + if self.current_animation_frame < len(self.animation) - 1: + self.current_animation_frame += 1 + else: + self.current_animation_frame = 0 + self.animation_timer = self.animation[self.current_animation_frame]["timer"] + self.image = self.sheet.sprites[self.animation[self.current_animation_frame]["sprite"]] + def act(self): pass def update(self, surface = None): if surface != None: + if self.play_animation: + self.animate() surface.blit(self.image, self.rect) ############################################################################### @@ -25,9 +53,9 @@ class VisibleGameObject(pygame.sprite.Sprite): class Tile(VisibleGameObject): - def __init__(self, image, pos, passable, occupant = None): + def __init__(self, sheet, pos, passable, occupant = None): - super().__init__(image, pos) + super().__init__(sheet, pos) self.passable = passable self.occupant = occupant @@ -41,9 +69,9 @@ class Entity(VisibleGameObject): game objects (player characters, obstacles, etc). """ - def __init__(self, image, pos, tile = None, solid = False): + def __init__(self, sheet, pos, tile = None, solid = False): - super().__init__(image, pos) + super().__init__(sheet, pos) self.tile = tile self.solid = solid