Return to repo list

heart-of-gold

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

commit e7365e74b5d4423e2a6ea3b12118e6404e06a287
parent 0ff1d90262f68cdd0a13379dc5dde44caa1cd724
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Sep 2020 07:34:44 -0500

Rebuilt vgo.py

Diffstat:
Asrc/vgo.py | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 144 insertions(+), 0 deletions(-)

diff --git a/src/vgo.py b/src/vgo.py @@ -0,0 +1,144 @@ +import pygame + +########## +# vgo.py # +########## + +# This file contains: +# 1. The VisibleGameObject class that defines the abstract parent of all entities, tiles, and other visible sprites +# 2. The Entity class that defines the objects that represent non-tile, non-UI-element game objects +# 3. Various kinds of specialized entity classes +# 4. The Tile class that defines the objects that make up the game board + +# TODO: This should eventually use LayeredDirty sprites for performance reasons + +################################### +# Section 1 - Visible Game Object # +################################### + +class VisibleGameObject(pygame.sprite.Sprite): + """ + The parent of all visible objects. VisibleGameObject (VGO) is + essentially an extended, customised version of the PyGame Sprite + object. + """ + + def __init__(self, sheet, position, visible = True): + + # Parent initialization + super().__init__(self) + + # Saved values + self.sheet = sheet + self.image = sheet.sprites[(0, 0)] # TODO: Should this be assigned?? + self.rect = self.image.get_rect() + self.rect.topleft = position # TODO: Maybe this should not be defined here? + self.visible = visible + + # Animation values + self.animated = False + self.animation = {} + self.animation_timer = 0 + self.animation_frame = 0 + + def set_animation(self, new_animation, play = False, init_frame = 0): + """ + Assign a new animation to this VGO and configure all + necessary values to get it playing. + """ + self.animation = new_animation + self.animation_frame = init_frame + self.animation_timer = new_animation[init_frame]["timer"] + self.image = self.sheet.sprites[new_anim[init_frame]["sprite"]] + self.play_animation = play + + def animate(self): + """ + Play the current animation. This method is called as part of + the update() logic. + """ + if self.animation_timer > 0: + self.animation_timer -= 1 + else: + if self.animation_frame < len(self.animation) - 1: + self.animation_frame += 1 + else: + self.animation_framer = 0 + self.animation_timer = self.animation[self.animation_framer]["timer"] + self.image = self.sheet.sprites[self.animation[self.animation_framer]["sprite"]] + + def act(self): + """ + This method is called as part of update() and is meant + to be overwritten in subclasses with any logic that needs + to happen each update. + """ + pass + + def update(self, surface = None): + """ + Draw the VGO to the surface. Also calls act() for update + logic for specific VGO children, and animate() to animate + the sprite image. + """ + self.act() + if surface != None: + if self.play_animation: + self.animate() + surface.blit(self.image, self.rect) + +############################# +# Section 2 - Entity Object # +############################# + +class Entity(VisibleGameObject): + """ + Entity is a class for all non-tile, non-UI-element sprite objects + that are drawn in the game. Things like character units and board + decorations/obstacles are entities. + """ + + def __init__(self, sheet, position, visible = True, passable = False, tile = None): + + # Parent initialization + super().__init__(self, sheet, position, visible) + + # Saved values + self.passable = passable + self.tile = tile + +######################################### +# Section 3 - Various Entity subclasses # +######################################### + +########################## +# Section 4 - Tile Class # +########################## + +class Tile(VisibleGameObject): + """ + Tile is a class for the tile objects that make up the visible game + board. Tiles are drawn in a grid, managed by a BoardManager object, + are part of a Board, and can be either passable or impassable, and + occupied by 0 or more Entity objects. Tiles have adjacent Tiles + they need to be aware of, but they are supplied with these adjacents + by the Board. + """ + + def __init__(self, sheet, position, visible = True, board = None, gridpos = (0, 0), passable = True): + + # Parent initialization + super().__init__(self, sheet, position, visible) + + # Saved values + self.board = board + self.grid_position = gridpos + self.passable = passable + + # Adjacents + self.adjacents = { + "left": None, + "right": None, + "top": None, + "bottom": None + }