Return to repo list

heart-of-gold

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

commit 71f2b137322bc99074f9d04c6346551c5acb2c7b
parent 1f2f7cff3c19c78d884de2aec3637fe7c6cc7d20
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed,  9 Sep 2020 22:44:57 -0500

begin to be able to draw board

Diffstat:
Msrc/board.py | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/constants.py | 1+
Asrc/manager.py | 7+++++++
3 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -1,14 +1,56 @@ import pygame, pytmx +from . import manager +from .constants import BOARD_PATH ############ # board.py # ############ # This file contains: -# 1. The Board class, which represents a grid of Tile objects and is the play area +# 1. The BoardManager class, which manages boards and swaps between them +# 2. The Board class, which represents a grid of Tile objects and is the play area + +################################### +# Section 1 - BoardManager Object # +################################### + +class BoardManager(manager.Manager): + """ + BoardManager handles loading and managing Board objects. It + is directly subordinate to Game and is mostly useful for + switching between Boards. + """ + + def __init__(self, game): + + super().__init__(game) + + # Board values + self.current_board = None + self.loaded_boards = {} + + def load_board_from_file(self, boardfile): + """ + Load a board from a given tmx file. + """ + self.loaded_boards[boardfile] = Board(self, boardfile) + + def switch_to_board(self, boardname): + """ + Switch to a given loaded board. + """ + if boardname in self.loaded_boards.keys(): + self.current_board = self.loaded_boards[boardname] + + def update_board(self, surface = None): + """ + Update the current board. + """ + if surface != None: + self.current_board.draw_board(surface) ############################ -# Section 1 - Board Object # +# Section 2 - Board Object # ############################ class Board(object): @@ -29,3 +71,15 @@ class Board(object): self.tmx_data = pytmx.load_pygame(self.filename) self.grid_width = self.tmx_data.width * self.tmx_data.tilewidth self.grid_height = self.tmx_data.height * self.tmx_data.tileheight + + def draw_map(self, surface = None): + """ + Draw the tiles of the board onto the provided PyGame + surface object. + """ + for layer in self.tmx_data.visible_layers: + if isinstance(layer, pytmx.TiledTileLayer): + for x, y, gid in layer: + t = self.tmx_data.get_tile_image_by_gid(gid) + if t: + surface.blit(t, (x * self.tmx_data.tilewidth, y * self.tmx_data.tileheight)) diff --git a/src/constants.py b/src/constants.py @@ -28,3 +28,4 @@ DATA_PATH = os.path.join(os.getcwd(), "data") IMAGE_PATH = os.path.join(DATA_PATH, "img") SOUND_PATH = os.path.join(DATA_PATH, "snd") FONT_PATH = os.path.join(DATA_PATH, "font") +BOARD_PATH = os.path.join(DATA_PATH, "map") diff --git a/src/manager.py b/src/manager.py @@ -0,0 +1,7 @@ +import pygame + +class Manager(object): + + def __init__(self, game): + + self.game = game