From 62e78ce88522bc91bbbefc55befc2f5b65988045 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Sat, 9 Apr 2022 21:28:31 +0200 Subject: [PATCH 1/6] =?UTF-8?q?odkry=C5=82em=20=C5=BCe=20istnieje=20grupa?= =?UTF-8?q?=20do=20jednego=20sprite'a=20-=20GroupSingle=20i=20zmieni=C5=82?= =?UTF-8?q?em=20grupe=20z=20tile'ami=20na=20single.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/game.py | 2 +- logic/grid.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/logic/game.py b/logic/game.py index 436d514..cba84eb 100644 --- a/logic/game.py +++ b/logic/game.py @@ -117,7 +117,7 @@ class Game: castle_spawn.spawn() - #grid.put_on_tile(0, 0, knights_left[0]) + # grid.put_on_tile(0, 0, knights_left[0]) while running: self.screen.blit(self.bg, (0, 0)) diff --git a/logic/grid.py b/logic/grid.py index 8074713..715bbd1 100644 --- a/logic/grid.py +++ b/logic/grid.py @@ -58,7 +58,7 @@ class Grid: else: self.free_fields.append(field) - self.grid[row].append(pygame.sprite.Group(field)) + self.grid[row].append(pygame.sprite.GroupSingle(field)) def update(self, screen): self.draw(screen) @@ -68,7 +68,7 @@ class Grid: return self.textures[texture_index] def get_tile(self, row, column): - return pygame.sprite.Group.sprites(self.grid[row][column])[0] # iteruj kolumny i wiersze od 0 + return self.grid[row][column].sprite # iteruj kolumny i wiersze od 0 def put_on_tile(self, row, column, obj): # iteruj kolumny i wiersze od 0 try: From 0a7b6136a5e4f4950b702f8cdade67b64a6816f0 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Mon, 11 Apr 2022 17:51:46 +0200 Subject: [PATCH 2/6] healthbar WIP --- common/constants.py | 3 +++ logic/health_bar.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ models/knight.py | 4 ++-- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 logic/health_bar.py diff --git a/common/constants.py b/common/constants.py index 31fd0a9..5f9c058 100644 --- a/common/constants.py +++ b/common/constants.py @@ -33,3 +33,6 @@ TILES = [ 'water.png', 'grass_with_tree.jpg', ] + +#HEALTH_BAR +BAR_ANIMATION_SPEED = 1 diff --git a/logic/health_bar.py b/logic/health_bar.py new file mode 100644 index 0000000..00fbc1a --- /dev/null +++ b/logic/health_bar.py @@ -0,0 +1,49 @@ +import pygame +from common.constants import BAR_ANIMATION_SPEED + + +class HealthBar: + def __init__(self, rect, current_hp, max_hp): + self.rect = rect + self.bar_animation_speed = BAR_ANIMATION_SPEED + self.current_hp = current_hp + self.target_hp = current_hp + self.max_hp = max_hp + self.width = self.rect.width * 0.8 + self.height = self.rect.height * 0.05 + self.x = self.rect.width * 0.1 + self.rect.x + self.y = self.rect.height * 0.95 + self.rect.y + self.health_ratio = self.max_hp/self.width + + def update(self): + self.animation() + + def take_dmg(self, dmg_taken): + if self.target_hp > 0: + self.target_hp -= dmg_taken + elif self.target_hp < 0: + self.target_hp = 0 + + def heal(self, amount): + if self.target_hp < self.max_hp: + self.target_hp += amount + elif self.target_hp > self.max_hp: + self.target_hp = self.max_hp + + def animation(self): + animation_bar_width = 0 + animation_bar_color = (255, 0, 0) + + if self.current_hp < self.target_hp: + self.current_hp += self.bar_animation_speed + animation_bar_width = int((self.current_hp - self.target_hp) / self.health_ratio) + animation_bar_color = (0, 255, 0) + + elif self.current_hp > self.target_hp: + self.current_hp -= self.bar_animation_speed + animation_bar_width = int((self.target_hp - self.current_hp) / self.health_ratio) + animation_bar_color = (255, 255, 0) + + + + diff --git a/models/knight.py b/models/knight.py index a4e264c..eba0297 100644 --- a/models/knight.py +++ b/models/knight.py @@ -1,6 +1,7 @@ import pygame.image import random + class Knight(pygame.sprite.Sprite): def __init__(self, img): super().__init__() @@ -12,6 +13,5 @@ class Knight(pygame.sprite.Sprite): self.health = random.randint(7, 12) self.attack = random.randint(4, 7) - self.defense = random.randint(1,4) + self.defense = random.randint(1, 4) self.points = 1 - From e5af464eb2d5f2f2b7a99ca86f8bc41b0715efaf Mon Sep 17 00:00:00 2001 From: XsedoX Date: Sun, 24 Apr 2022 22:06:20 +0200 Subject: [PATCH 3/6] healthbar WIP - team health bars jako klasa health_bar.py --- common/colors.py | 1 + logic/game.py | 10 +++++++--- logic/health_bar.py | 47 +++++++++++++++++++++++++-------------------- ui/stats.py | 47 +++++++++++++++++++++++++++------------------ 4 files changed, 62 insertions(+), 43 deletions(-) diff --git a/common/colors.py b/common/colors.py index 8a8f41b..e74689a 100644 --- a/common/colors.py +++ b/common/colors.py @@ -2,5 +2,6 @@ BLACK = (0, 0, 0) WHITE = (255, 255, 255) ORANGE = (249, 141, 42) RED = (255, 58, 58) +GREEN = (0, 255, 0) FONT_DARK = (37, 37, 37) diff --git a/logic/game.py b/logic/game.py index 755a851..e5aaa69 100644 --- a/logic/game.py +++ b/logic/game.py @@ -34,7 +34,7 @@ class Game: menu.display_screen() def game(self): - stats = Stats() + stats = Stats(self.screen) logs = Logs() # setup clock for rounds @@ -56,12 +56,16 @@ class Game: if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False - if event.key == 110: # clicked n letter on keyboard + 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 self.level.handle_turn() - stats.draw(self.screen) + stats.draw() logs.draw(self.screen) self.level.update() diff --git a/logic/health_bar.py b/logic/health_bar.py index 00fbc1a..65dc24f 100644 --- a/logic/health_bar.py +++ b/logic/health_bar.py @@ -1,22 +1,34 @@ import pygame -from common.constants import BAR_ANIMATION_SPEED +from common.constants import BAR_ANIMATION_SPEED, GRID_CELL_SIZE +from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK class HealthBar: - def __init__(self, rect, current_hp, max_hp): + def __init__(self, screen, rect, current_hp, max_hp, x=None, y=None, width=None, height=None): self.rect = rect - self.bar_animation_speed = BAR_ANIMATION_SPEED + self.screen = screen self.current_hp = current_hp self.target_hp = current_hp self.max_hp = max_hp - self.width = self.rect.width * 0.8 - self.height = self.rect.height * 0.05 - self.x = self.rect.width * 0.1 + self.rect.x - self.y = self.rect.height * 0.95 + self.rect.y + 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 + def update(self): - self.animation() + self.show() def take_dmg(self, dmg_taken): if self.target_hp > 0: @@ -30,19 +42,12 @@ class HealthBar: elif self.target_hp > self.max_hp: self.target_hp = self.max_hp - def animation(self): - animation_bar_width = 0 - animation_bar_color = (255, 0, 0) - - if self.current_hp < self.target_hp: - self.current_hp += self.bar_animation_speed - animation_bar_width = int((self.current_hp - self.target_hp) / self.health_ratio) - animation_bar_color = (0, 255, 0) - - elif self.current_hp > self.target_hp: - self.current_hp -= self.bar_animation_speed - animation_bar_width = int((self.target_hp - self.current_hp) / self.health_ratio) - animation_bar_color = (255, 255, 0) + 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)) + + diff --git a/ui/stats.py b/ui/stats.py index 521b901..f35cfd0 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -1,44 +1,53 @@ import pygame +import time +from logic.health_bar import HealthBar from common.colors import FONT_DARK, ORANGE, WHITE, RED from common.constants import COLUMNS, GRID_CELL_PADDING, GRID_CELL_SIZE, BORDER_WIDTH, BORDER_RADIUS from common.helpers import draw_text class Stats: - def __init__(self): + def __init__(self, screen): self.grid = [] + 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) - def draw(self, screen): - x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 - y = 5 + def draw(self): # background - pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, 340, 450), 0, BORDER_RADIUS) + pygame.draw.rect(self.screen, WHITE, pygame.Rect(self.x, self.y, 340, 450), 0, BORDER_RADIUS) # title - draw_text('STATS', FONT_DARK, screen, x + 120, y + 10, 36) - pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) + draw_text('STATS', FONT_DARK, self.screen, self.x + 120, self.y + 10, 36) + pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 65, 340, 3)) # shields shield_blue = pygame.image.load('./resources/textures/shield_blue.png') shield_red = pygame.image.load('./resources/textures/shield_red.png') - screen.blit(shield_blue, (x + 20, y + 80)) - screen.blit(shield_red, (x + 200, y + 80)) - draw_text('VS', FONT_DARK, screen, x + 150, y + 120, 36) + self.screen.blit(shield_blue, (self.x + 20, self.y + 80)) + self.screen.blit(shield_red, (self.x + 200, self.y + 80)) + draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36) # HP bars - pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4) - pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4) + #pygame.draw.rect(screen, RED, pygame.Rect(x + 30, y + 210, 100, 15), 0, 4) + #pygame.draw.rect(screen, RED, pygame.Rect(x + 210, y + 210, 100, 15), 0, 4) + self.red_team_hp_bar.update() + self.blue_team_hp_bar.update() + + # texts - draw_text('Rycerze: 2', FONT_DARK, screen, x + 35, y + 240, 18) - draw_text('Fortece: 1', FONT_DARK, screen, x + 35, y + 270, 18) + draw_text('Rycerze: 2', FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) + draw_text('Fortece: 1', FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) - draw_text('Rycerze: 4', FONT_DARK, screen, x + 215, y + 240, 18) - draw_text('Fortece: 0', FONT_DARK, screen, x + 215, y + 270, 18) + draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18) + draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 215, self.y + 270, 18) # points - pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 390, 340, 3)) - draw_text('PUNKTY: 10', FONT_DARK, screen, x + 35, y + 408, 18, True) - draw_text('PUNKTY: 10', FONT_DARK, screen, x + 215, y + 408, 18, True) + pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3)) + draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 35, self.y + 408, 18, True) + draw_text('PUNKTY: 10', FONT_DARK, self.screen, self.x + 215, self.y + 408, 18, True) From c11fdae8a5908a9a7cd7cdab1d9b79fd7c578831 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Thu, 28 Apr 2022 09:50:24 +0200 Subject: [PATCH 4/6] healthbar WIP - team health bars jako klasa health_bar.py --- logic/game.py | 2 -- logic/health_bar.py | 46 ++++++++++++++++++++++++++------------------- logic/level.py | 4 ++-- models/knight.py | 5 ++++- ui/stats.py | 4 ++-- 5 files changed, 35 insertions(+), 26 deletions(-) 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): From 0f6f2354d366256aba6abe2c85fe1f1d94ac4638 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Thu, 28 Apr 2022 10:18:17 +0200 Subject: [PATCH 5/6] =?UTF-8?q?sprite=20group=20w=20level.py=20zamienilem?= =?UTF-8?q?=20na=20LayeredUpdates()=20oraz=20doda=C5=82em=20do=20ka=C5=BCd?= =?UTF-8?q?ego=20sprite'a=20layer,=20wiec=20nie=20beda=20na=20siebie=20nac?= =?UTF-8?q?hodzic=20nigdy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/game.py | 5 ++--- logic/level.py | 4 +++- models/castle.py | 1 + models/knight.py | 2 +- models/monster.py | 1 + models/tile.py | 3 ++- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/logic/game.py b/logic/game.py index 2643f1b..f738fe9 100644 --- a/logic/game.py +++ b/logic/game.py @@ -35,7 +35,7 @@ class Game: menu.display_screen() def game(self): - stats = Stats() + stats = Stats(self.screen) # setup clock for rounds NEXT_TURN = pygame.USEREVENT + 1 @@ -63,9 +63,8 @@ class Game: if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds self.level.handle_turn() - stats.draw(self.screen) + stats.draw() self.logs.draw() - self.level.update() if print_numbers_flag: diff --git a/logic/level.py b/logic/level.py index 7179f32..1a9a3a8 100644 --- a/logic/level.py +++ b/logic/level.py @@ -18,7 +18,7 @@ class Level: self.screen = screen self.logs = logs # sprite group setup - self.sprites = pygame.sprite.Group() + self.sprites = pygame.sprite.LayeredUpdates() self.map = [['g' for _ in range(COLUMNS)] for y in range(ROWS)] @@ -154,3 +154,5 @@ class Level: # update and draw the game self.sprites.draw(self.screen) + + diff --git a/models/castle.py b/models/castle.py index 629f524..04bffad 100644 --- a/models/castle.py +++ b/models/castle.py @@ -6,6 +6,7 @@ from common.helpers import parse_cord class Castle(pygame.sprite.Sprite): def __init__(self, position, group): super().__init__(group) + self._layer = 1 self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha() self.image = pygame.transform.scale(self.image, (78, 78)) self.position = position diff --git a/models/knight.py b/models/knight.py index 6855328..0e264d1 100644 --- a/models/knight.py +++ b/models/knight.py @@ -28,6 +28,7 @@ class Knight(pygame.sprite.Sprite): self.image = self.states[self.direction.value] self.position = position + self._layer = 1 position_in_px = (parse_cord(position[0]), parse_cord(position[1])) self.rect = self.image.get_rect(topleft=position_in_px) @@ -37,7 +38,6 @@ class Knight(pygame.sprite.Sprite): 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/models/monster.py b/models/monster.py index fa42177..0535896 100644 --- a/models/monster.py +++ b/models/monster.py @@ -14,6 +14,7 @@ monster_images = [ class Monster(pygame.sprite.Sprite): def __init__(self, position, group): super().__init__(group) + self._layer = 1 self.image = random.choice(monster_images) self.image = pygame.transform.scale(self.image, (40, 40)) position_in_px = (parse_cord(position[0]), parse_cord(position[1])) diff --git a/models/tile.py b/models/tile.py index ccbefa6..b6a7857 100644 --- a/models/tile.py +++ b/models/tile.py @@ -7,6 +7,7 @@ class Tile(pygame.sprite.Sprite): def __init__(self, position, image, group, tile_type=' '): super().__init__(group) self.image = image + self._layer = 0 position_in_px = (parse_cord(position[0]), parse_cord(position[1])) self.rect = self.image.get_rect(topleft=position_in_px) - self.tile_type = tile_type \ No newline at end of file + self.tile_type = tile_type From dca9c82b7083e06d3b585d16cde184b2e7f3d0c6 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Thu, 28 Apr 2022 14:13:59 +0200 Subject: [PATCH 6/6] 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)