Return to repo list

heart-of-gold

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

commit 5fc83ae6e7c3048ea94d87c2fa975591c996c253
parent 216770300fef98a8058a2076ba2e67c5f9cbfbd7
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon, 16 Nov 2020 17:10:29 -0600

names and hp bars in turn icons

Diffstat:
Msrc/piece.py | 7++++---
Msrc/turn.py | 38++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/piece.py b/src/piece.py @@ -261,11 +261,12 @@ class Piece(entity.Entity): on the board. The bar is green on red, with the green percentage determined by the remaining HP percentage. """ - self.health_bar = pygame.Surface((TILE_WIDTH // 2, TILE_HEIGHT // 10)) + self.health_bar = pygame.Surface((TILE_WIDTH // 2, TILE_HEIGHT // 10)).convert() self.health_bar_rect = self.health_bar.get_rect() - self.health_bar_fill = pygame.Surface((round((self.active_stats["HP"] / self.normal_stats["HP"]) * (self.health_bar_rect.width - 2)), self.health_bar_rect.height - 2)) + self.health_bar_fill = pygame.Surface((round((self.active_stats["HP"] / self.normal_stats["HP"]) * (self.health_bar_rect.width - 2)), + self.health_bar_rect.height - 2)).convert() self.health_bar_fill_rect = self.health_bar_fill.get_rect() - self.health_bar.fill((145, 0, 0)) + self.health_bar.fill((45, 45, 45)) self.health_bar_fill.fill((0, 200, 0)) def execute_tile_path_move(self): diff --git a/src/turn.py b/src/turn.py @@ -286,6 +286,35 @@ class TurnIcon(entity.Entity): self.set_position((800, 32 + (31 * index))) self.piece_picture.set_position(self.rect.topleft) self.clickable = False + self.health_bar = None + self.health_bar_fill = None + self.health_bar_rect = None + self.health_bar_fill_rect = None + self.create_health_bar() + self.font = None + self.name_surface = None + self.name_surface_rect = None + self.render_name() + + def create_health_bar(self): + """ + Create a health bar for the given piece. + """ + self.health_bar = pygame.Surface((self.rect.width // 2, self.rect.height // 6)).convert() + self.health_bar_rect = self.health_bar.get_rect() + self.health_bar_fill = pygame.Surface((round((self.piece.active_stats["HP"] / self.piece.normal_stats["HP"]) * (self.health_bar_rect.width - 2)), + self.health_bar_rect.height - 2)).convert() + self.health_bar_fill_rect = self.health_bar_fill.get_rect() + self.health_bar.fill((45, 45, 45)) + self.health_bar_fill.fill((0, 200, 0)) + + def render_name(self): + """ + Render the name font image for display. + """ + self.font = pygame.font.Font(os.path.join(FONT_PATH, UI_FONT), self.rect.height // 2) + self.name_surface = self.font.render(self.piece.name, False, (255, 255, 255)).convert() + self.name_surface_rect = self.name_surface.get_rect() def update(self, surface = None): """ @@ -294,6 +323,15 @@ class TurnIcon(entity.Entity): """ super().update(surface) self.piece_picture.update(surface) + # TODO: Hardcoding magic numbers + if self.health_bar != None and self.health_bar_fill != None: + self.health_bar_rect.center = (self.rect.center[0] + 8, self.rect.center[1] + 8) + self.health_bar_fill_rect.topleft = (self.health_bar_rect.topleft[0] + 1, self.health_bar_rect.topleft[1] + 1) + surface.blit(self.health_bar, self.health_bar_rect) + surface.blit(self.health_bar_fill, self.health_bar_fill_rect) + if self.name_surface != None: + self.name_surface_rect.center = (self.rect.center[0] + 8, self.rect.center[1] - 4) + surface.blit(self.name_surface, self.name_surface_rect) class TurnTrayMoreButton(entity.Entity): """