From 9f3c5fb2d5eb2a8970f2d7575ac54e702b9fe85c Mon Sep 17 00:00:00 2001 From: XsedoX Date: Thu, 28 Apr 2022 21:50:12 +0200 Subject: [PATCH 1/2] liczenie rycerzy dziala --- logic/game.py | 3 +-- ui/stats.py | 12 +++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/logic/game.py b/logic/game.py index c7ad2e6..565a4cc 100644 --- a/logic/game.py +++ b/logic/game.py @@ -27,7 +27,6 @@ class Game: self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)} - def main_menu(self): menu = MainMenu(self.screen, self.clock, self.bg, self.game, @@ -38,7 +37,7 @@ class Game: def game(self): logs = Logs(self.screen) level = Level(self.screen, logs) - stats = Stats(self.screen) + stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red) # setup clock for rounds NEXT_TURN = pygame.USEREVENT + 1 diff --git a/ui/stats.py b/ui/stats.py index 420a55e..9fee084 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -8,8 +8,10 @@ from common.helpers import draw_text class Stats: - def __init__(self, screen): + def __init__(self, screen, list_knights_blue, list_knights_red): self.grid = [] + self.list_knights_blue = list_knights_blue + self.list_knights_red = list_knights_red self.screen = screen self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 self.y = 5 @@ -33,16 +35,12 @@ class Stats: 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) self.red_team_hp_bar.update() self.blue_team_hp_bar.update() - - # texts - 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: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue + draw_text('Fortece: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) # red 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) -- 2.20.1 From 504cfd1af01b3d2fa162df7fb4242c127db53355 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Wed, 11 May 2022 16:50:14 +0200 Subject: [PATCH 2/2] poprawki do healthbar oraz dostawania obrazen. TRZEBA UZYWAC FUNKCJI W knight.py --- logic/game.py | 5 ++--- logic/health_bar.py | 21 ++++++++++----------- models/knight.py | 15 +++++++++++++-- ui/stats.py | 14 ++++++++++++-- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/logic/game.py b/logic/game.py index 565a4cc..5bf6e60 100644 --- a/logic/game.py +++ b/logic/game.py @@ -37,7 +37,6 @@ class Game: def game(self): logs = Logs(self.screen) level = Level(self.screen, logs) - stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red) # setup clock for rounds NEXT_TURN = pygame.USEREVENT + 1 @@ -45,6 +44,7 @@ class Game: # create level level.create_map() + stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red) print_numbers_flag = False running = True @@ -60,8 +60,7 @@ class Game: running = False if event.key == pygame.K_n: print_numbers_flag = not print_numbers_flag - 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 level.handle_turn() diff --git a/logic/health_bar.py b/logic/health_bar.py index edcabbe..c362fc7 100644 --- a/logic/health_bar.py +++ b/logic/health_bar.py @@ -9,7 +9,6 @@ class HealthBar: self.rect = rect self.screen = screen self.current_hp = current_hp - self.target_hp = current_hp self.max_hp = max_hp self.x = self.rect.x self.y = self.rect.y @@ -38,22 +37,22 @@ class HealthBar: self.health_ratio = self.max_hp / self.width - 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 take_dmg(self, amount): + if self.current_hp - amount > 0: + self.current_hp -= amount + elif self.current_hp - amount <= 0: + self.current_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 + if self.current_hp + amount < self.max_hp: + self.current_hp += amount + elif self.current_hp + amount > self.max_hp: + self.current_hp = self.max_hp 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), self.height)) + pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.current_hp / self.health_ratio), self.height)) diff --git a/models/knight.py b/models/knight.py index d24c2be..93432e1 100644 --- a/models/knight.py +++ b/models/knight.py @@ -34,11 +34,10 @@ class Knight(pygame.sprite.Sprite): self.team = team 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, current_hp=self.current_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True) + self.health_bar = HealthBar(screen, self.rect, current_hp=random.randint(1, self.max_hp), max_hp=self.max_hp, calculate_xy=True, calculate_size=True) def rotate_left(self): self.direction = self.direction.left() @@ -48,6 +47,18 @@ class Knight(pygame.sprite.Sprite): self.direction = self.direction.right() self.image = self.states[self.direction.value] + def take_dmg(self, amount): + self.health_bar.take_dmg(amount) + + def heal(self, amount): + self.health_bar.heal(amount) + + def get_current_hp(self): + return self.health_bar.current_hp + + def get_max_hp(self): + return self.health_bar.max_hp + def step_forward(self): if self.direction.name == 'UP': self.position = (self.position[0], self.position[1] - 1) diff --git a/ui/stats.py b/ui/stats.py index 9fee084..a1d05ee 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -15,8 +15,14 @@ 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, 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), 100, 100) + self.blue_team_hp_bar = HealthBar(self.screen, + pygame.Rect(self.x + 30, self.y + 210, 100, 15), + current_hp=sum([knight.get_current_hp() for knight in self.list_knights_blue]), + max_hp=sum([knight.get_max_hp() for knight in self.list_knights_blue])) + self.red_team_hp_bar = HealthBar(self.screen, + pygame.Rect(self.x + 210, self.y + 210, 100, 15), + current_hp=sum([knight.get_current_hp() for knight in self.list_knights_red]), + max_hp=sum([knight.get_max_hp() for knight in self.list_knights_red])) def update(self): @@ -35,6 +41,10 @@ class Stats: draw_text('VS', FONT_DARK, self.screen, self.x + 150, self.y + 120, 36) # HP bars + self.red_team_hp_bar.take_dmg(self.red_team_hp_bar.current_hp - + sum([knight.get_current_hp() for knight in self.list_knights_red])) + self.blue_team_hp_bar.take_dmg(self.blue_team_hp_bar.current_hp - + sum([knight.get_current_hp() for knight in self.list_knights_blue])) self.red_team_hp_bar.update() self.blue_team_hp_bar.update() -- 2.20.1