diff --git a/logic/game.py b/logic/game.py index e5aaa69..ec40e2f 100644 --- a/logic/game.py +++ b/logic/game.py @@ -58,8 +58,6 @@ class Game: running = False if event.key == pygame.K_n: print_numbers_flag = not print_numbers_flag - if event.key == pygame.K_b: - stats.blue_team_hp_bar.take_dmg() if event.key == pygame.K_r: stats.red_team_hp_bar.take_dmg(5) if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds diff --git a/logic/health_bar.py b/logic/health_bar.py index 65dc24f..fbf86fc 100644 --- a/logic/health_bar.py +++ b/logic/health_bar.py @@ -4,31 +4,41 @@ from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK class HealthBar: - def __init__(self, screen, rect, current_hp, max_hp, x=None, y=None, width=None, height=None): + def __init__(self, screen, rect: pygame.rect, current_hp, max_hp, calculate_xy=False, calculate_size=False): + self.health_ratio = None self.rect = rect self.screen = screen self.current_hp = current_hp self.target_hp = current_hp self.max_hp = max_hp - self.width = width - self.height = height - self.x = x - self.y = y - - if self.width is None: - self.width = int(GRID_CELL_SIZE * 0.9) - if self.height is None: - self.height = int(GRID_CELL_SIZE * 0.05) - - self.health_ratio = self.max_hp/self.width - - if self.x is None: - self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x - if self.y is None: - self.y = int(GRID_CELL_SIZE/2) + self.rect.y + self.width = self.rect.width + self.height = self.rect.height + self.x = self.rect.x + self.y = self.rect.y + self.calculate_xy = calculate_xy + self.calculate_size = calculate_size + self.update_stats() def update(self): self.show() + self.update_stats() + + def update_stats(self): + if self.calculate_size: + self.width = int(GRID_CELL_SIZE * 0.9) + self.height = int(GRID_CELL_SIZE * 0.05) + else: + self.x = self.rect.x + self.y = self.rect.y + + if self.calculate_xy: + self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x + self.y = int(GRID_CELL_SIZE/2) + self.rect.y + else: + self.width = self.rect.width + self.height = self.rect.height + + self.health_ratio = self.max_hp / self.width def take_dmg(self, dmg_taken): if self.target_hp > 0: @@ -50,5 +60,3 @@ class HealthBar: - - diff --git a/logic/level.py b/logic/level.py index 17db1f0..4aba1a0 100644 --- a/logic/level.py +++ b/logic/level.py @@ -83,11 +83,11 @@ class Level: # add objects, e.g. knights, monsters, castle if col == "k_b": - knight = Knight((col_index, row_index), self.sprites, "blue") + knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue") self.map[row_index][col_index] = knight self.list_knights_blue.append(knight) elif col == "k_r": - knight = Knight((col_index, row_index), self.sprites, "red") + knight = Knight(self.screen, (col_index, row_index), self.sprites, "red") self.map[row_index][col_index] = knight self.list_knights_red.append(knight) elif col == "m": diff --git a/models/knight.py b/models/knight.py index f9bac44..85c166c 100644 --- a/models/knight.py +++ b/models/knight.py @@ -3,6 +3,7 @@ import random from common.constants import GRID_CELL_SIZE, Direction from common.helpers import parse_cord +from logic.health_bar import HealthBar def load_knight_textures(): @@ -18,7 +19,7 @@ def load_knight_textures(): class Knight(pygame.sprite.Sprite): - def __init__(self, position, group, team): + def __init__(self, screen, position, group, team): super().__init__(group) self.direction = Direction.DOWN @@ -34,6 +35,8 @@ class Knight(pygame.sprite.Sprite): self.attack = random.randint(4, 7) self.defense = random.randint(1, 4) self.points = 1 + self.health_bar = HealthBar(screen, self.rect, self.health, self.health) + self.health_bar.update() def rotate_left(self): self.direction = self.direction.left() diff --git a/ui/stats.py b/ui/stats.py index f35cfd0..c5aacde 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -13,8 +13,8 @@ class Stats: self.screen = screen self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 self.y = 5 - self.blue_team_hp_bar = HealthBar(self.screen, (0, 0), 50, 100, self.x + 30, self.y + 210, 100, 15) - self.red_team_hp_bar = HealthBar(self.screen, (0, 0), 50, 100, self.x + 210, self.y + 210, 100, 15) + self.blue_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 30, self.y + 210, 100, 15), current_hp=50, max_hp=100) + self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 50, 100) def draw(self):