From dca9c82b7083e06d3b585d16cde184b2e7f3d0c6 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Thu, 28 Apr 2022 14:13:59 +0200 Subject: [PATCH] health_bar.py - gotowy --- common/constants.py | 4 ++- logic/__tests__/knights_queue_test.py | 48 +++++++++++++-------------- logic/game.py | 4 ++- logic/health_bar.py | 32 +++++++++--------- logic/knights_queue.py | 2 +- logic/level.py | 20 ++++++++--- models/castle.py | 9 +++-- models/knight.py | 5 +-- models/monster.py | 15 +++++---- ui/stats.py | 4 +-- 10 files changed, 83 insertions(+), 60 deletions(-) diff --git a/common/constants.py b/common/constants.py index 32ed1ee..caf995f 100644 --- a/common/constants.py +++ b/common/constants.py @@ -63,5 +63,7 @@ ACTION = { "go": 0, } -#HEALTH_BAR +# HEALTH_BAR BAR_ANIMATION_SPEED = 1 +BAR_WIDTH_MULTIPLIER = 0.9 # (0;1> +BAR_HEIGHT_MULTIPLIER = 0.1 diff --git a/logic/__tests__/knights_queue_test.py b/logic/__tests__/knights_queue_test.py index ecf9548..40c614b 100644 --- a/logic/__tests__/knights_queue_test.py +++ b/logic/__tests__/knights_queue_test.py @@ -7,40 +7,40 @@ from models.knight import Knight class KnightsQueueTest(unittest.TestCase): def test_should_skip_dead_knights(self): knight1 = Knight(None) - knight1.health = 0 + knight1.max_hp = 0 knight2 = Knight(None) - knight2.health = 0 + knight2.max_hp = 0 knight3 = Knight(None) - knight3.health = 1 + knight3.max_hp = 1 knight4 = Knight(None) - knight4.health = 0 + knight4.max_hp = 0 knight5 = Knight(None) - knight5.health = 0 + knight5.max_hp = 0 knight6 = Knight(None) - knight6.health = 1 + knight6.max_hp = 1 knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4, knight5, knight6]) res1 = knights_queue.dequeue_knight() res2 = knights_queue.dequeue_knight() - self.assertEqual(res1.health, 1) - self.assertEqual(res2.health, 1) + self.assertEqual(res1.max_hp, 1) + self.assertEqual(res2.max_hp, 1) def test_should_return_first_alive_knight(self): knight1 = Knight(None) - knight1.health = 222 + knight1.max_hp = 222 knight2 = Knight(None) - knight2.health = -1 + knight2.max_hp = -1 knight3 = Knight(None) - knight3.health = 1 + knight3.max_hp = 1 knights_queue = KnightsQueue([knight1, knight2], [knight3]) @@ -55,22 +55,22 @@ class KnightsQueueTest(unittest.TestCase): def test_should_raise_when_knight_died_and_whole_team_dead(self): with self.assertRaises(Exception): knight1 = Knight(None) - knight1.health = 222 + knight1.max_hp = 222 knight2 = Knight(None) - knight2.health = 1 + knight2.max_hp = 1 knights_queue = KnightsQueue([knight1], [knight2]) knights_queue.dequeue_knight() knights_queue.dequeue_knight() - knight2.health = -2 + knight2.max_hp = -2 knights_queue.dequeue_knight() knights_queue.dequeue_knight() def test_should_make_valid_next_turn(self): knight1 = Knight(None) - knight1.health = 222 + knight1.max_hp = 222 knight2 = Knight(None) - knight2.health = 1 + knight2.max_hp = 1 knights_queue = KnightsQueue([knight1], [knight2]) previous_turn = knights_queue.team_idx_turn @@ -82,13 +82,13 @@ class KnightsQueueTest(unittest.TestCase): def test_should_raise_when_team_has_dead_knights(self): with self.assertRaises(Exception): knight1 = Knight(None) - knight1.health = 0 + knight1.max_hp = 0 knight2 = Knight(None) - knight2.health = -1 + knight2.max_hp = -1 knight3 = Knight(None) - knight3.health = -2 + knight3.max_hp = -2 knight4 = Knight(None) - knight4.health = 20 + knight4.max_hp = 20 knights_queue = KnightsQueue([knight1, knight2, knight3], [knight4]) @@ -97,9 +97,9 @@ class KnightsQueueTest(unittest.TestCase): def test_should_return_knight_from_any_team_and_add_to_queue_again(self): knight1 = Knight(None) - knight1.health = 10 + knight1.max_hp = 10 knight2 = Knight(None) - knight2.health = 20 + knight2.max_hp = 20 knights_queue = KnightsQueue([knight1], [knight2]) result1 = knights_queue.dequeue_knight() @@ -109,12 +109,12 @@ class KnightsQueueTest(unittest.TestCase): self.assertIsNotNone(result1) self.assertIsNotNone(result2) self.assertIsNotNone(result3) - self.assertTrue(result1.health == result3.health) + self.assertTrue(result1.max_hp == result3.max_hp) def test_should_raise_when_only_one_team_alive(self): with self.assertRaises(Exception): knight = Knight(None) - knight.health = 21 + knight.max_hp = 21 knights_queue = KnightsQueue([knight], []) knights_queue.dequeue_knight() diff --git a/logic/game.py b/logic/game.py index f738fe9..0a1b678 100644 --- a/logic/game.py +++ b/logic/game.py @@ -10,6 +10,7 @@ from ui.screens.credits import Credits from ui.screens.main_menu import MainMenu from ui.screens.options import Options from ui.stats import Stats +from logic.health_bar import HealthBar class Game: @@ -35,6 +36,7 @@ class Game: menu.display_screen() def game(self): + health_bar = HealthBar(self.screen, pygame.Rect(150, 150, 100, 15), 100, 100) stats = Stats(self.screen) # setup clock for rounds @@ -63,7 +65,7 @@ class Game: if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds self.level.handle_turn() - stats.draw() + stats.update() self.logs.draw() self.level.update() diff --git a/logic/health_bar.py b/logic/health_bar.py index fbf86fc..edcabbe 100644 --- a/logic/health_bar.py +++ b/logic/health_bar.py @@ -1,5 +1,5 @@ import pygame -from common.constants import BAR_ANIMATION_SPEED, GRID_CELL_SIZE +from common.constants import BAR_ANIMATION_SPEED, BAR_WIDTH_MULTIPLIER, BAR_HEIGHT_MULTIPLIER from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK @@ -11,32 +11,30 @@ class HealthBar: self.current_hp = current_hp self.target_hp = current_hp self.max_hp = max_hp - 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 + + if calculate_size: + self.width = int(self.rect.width * BAR_WIDTH_MULTIPLIER) - 2 + self.height = int(self.rect.width * BAR_HEIGHT_MULTIPLIER) - 2 + else: + self.width = self.rect.width - 2 + self.height = self.rect.height - 2 + self.update_stats() def update(self): - self.show() self.update_stats() + self.show() 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 + self.x = int(self.rect.width * (1 - BAR_WIDTH_MULTIPLIER)/2) + self.rect.x + 1 + self.y = int(self.rect.height * BAR_HEIGHT_MULTIPLIER/2) + self.rect.y + 1 else: - self.width = self.rect.width - self.height = self.rect.height + self.x = self.rect.x + 1 + self.y = self.rect.y + 1 self.health_ratio = self.max_hp / self.width @@ -55,7 +53,7 @@ class HealthBar: def show(self): pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2)) pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height)) - pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp * self.health_ratio)-1, self.height)) + pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp / self.health_ratio), self.height)) diff --git a/logic/knights_queue.py b/logic/knights_queue.py index 4b63e1f..d9db7fb 100644 --- a/logic/knights_queue.py +++ b/logic/knights_queue.py @@ -10,7 +10,7 @@ class KnightsQueue: def dequeue_knight(self): if self.both_teams_alive(): knight = self.queues[self.team_idx_turn].popleft() - if knight.health <= 0: + if knight.max_hp <= 0: return self.dequeue_knight() else: self.queues[self.team_idx_turn].append(knight) diff --git a/logic/level.py b/logic/level.py index 1a9a3a8..00d2044 100644 --- a/logic/level.py +++ b/logic/level.py @@ -96,13 +96,13 @@ class Level: self.map[row_index][col_index] = knight self.list_knights_red.append(knight) elif col == "m": - monster = Monster((col_index, row_index), self.sprites) + monster = Monster(self.screen, (col_index, row_index), self.sprites) self.map[row_index][col_index] = monster self.list_monsters.append(monster) elif col == "c": castle_count += 1 if castle_count == 4: - castle = Castle((col_index, row_index), self.sprites) + castle = Castle(self.screen, (col_index, row_index), self.sprites) self.map[row_index][col_index] = castle self.list_castles.append(castle) @@ -147,6 +147,19 @@ class Level: self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.') self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias() + def update_health_bars(self): + for knight in self.list_knights_blue: + knight.health_bar.update() + + for knight in self.list_knights_red: + knight.health_bar.update() + + for monster in self.list_monsters: + monster.health_bar.update() + + for castle in self.list_castles: + castle.health_bar.update() + def update(self): bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH @@ -154,5 +167,4 @@ class Level: # update and draw the game self.sprites.draw(self.screen) - - + self.update_health_bars() # has to be called last diff --git a/models/castle.py b/models/castle.py index 04bffad..915b514 100644 --- a/models/castle.py +++ b/models/castle.py @@ -1,10 +1,13 @@ +import random + import pygame.image from common.helpers import parse_cord +from logic.health_bar import HealthBar class Castle(pygame.sprite.Sprite): - def __init__(self, position, group): + def __init__(self, screen, position, group): super().__init__(group) self._layer = 1 self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha() @@ -12,4 +15,6 @@ class Castle(pygame.sprite.Sprite): self.position = position position_in_px = (parse_cord(position[0]), parse_cord(position[1])) self.rect = self.image.get_rect(center=position_in_px) - self.health = 80 + self.max_hp = 80 + self.current_hp = random.randint(1, self.max_hp) + self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True) diff --git a/models/knight.py b/models/knight.py index 0e264d1..d24c2be 100644 --- a/models/knight.py +++ b/models/knight.py @@ -33,11 +33,12 @@ class Knight(pygame.sprite.Sprite): self.rect = self.image.get_rect(topleft=position_in_px) self.team = team - self.health = random.randint(7, 12) + self.max_hp = random.randint(7, 12) + self.current_hp = random.randint(1, self.max_hp) 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 = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True) def rotate_left(self): self.direction = self.direction.left() diff --git a/models/monster.py b/models/monster.py index 0535896..b8415f1 100644 --- a/models/monster.py +++ b/models/monster.py @@ -2,6 +2,7 @@ import pygame.image import random from common.helpers import parse_cord +from logic.health_bar import HealthBar monster_images = [ pygame.image.load("./resources/textures/dragon2.png"), @@ -12,7 +13,7 @@ monster_images = [ class Monster(pygame.sprite.Sprite): - def __init__(self, position, group): + def __init__(self, screen, position, group): super().__init__(group) self._layer = 1 self.image = random.choice(monster_images) @@ -20,21 +21,23 @@ class Monster(pygame.sprite.Sprite): position_in_px = (parse_cord(position[0]), parse_cord(position[1])) self.rect = self.image.get_rect(topleft=position_in_px) - self.health = random.randrange(15, 25) + self.max_hp = random.randrange(15, 25) + self.current_hp = random.randint(1, self.max_hp) + self.health_bar = HealthBar(screen, self.rect, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True) self.attack = random.randrange(2, 10) if self.image == monster_images[0]: - self.health = 20 + self.max_hp = 20 self.attack = 9 self.points = 10 elif self.image == monster_images[1]: - self.health = 15 + self.max_hp = 15 self.attack = 7 self.points = 7 elif self.image == monster_images[2]: - self.health = 10 + self.max_hp = 10 self.attack = 4 self.points = 4 elif self.image == monster_images[3]: - self.health = 7 + self.max_hp = 7 self.attack = 2 self.points = 2 diff --git a/ui/stats.py b/ui/stats.py index c5aacde..420a55e 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -14,9 +14,9 @@ class Stats: self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 self.y = 5 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) + self.red_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 210, self.y + 210, 100, 15), 100, 100) - def draw(self): + def update(self): # background pygame.draw.rect(self.screen, WHITE, pygame.Rect(self.x, self.y, 340, 450), 0, BORDER_RADIUS)