diff --git a/logic/game.py b/logic/game.py index fe8cb28..5bf6e60 100644 --- a/logic/game.py +++ b/logic/game.py @@ -26,6 +26,7 @@ class Game: self.bg = pygame.image.load("./resources/textures/bg.jpg") 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, @@ -43,9 +44,7 @@ class Game: # create level level.create_map() - stats = Stats(self.screen, - level.list_knights_blue, - level.list_knights_red) # has to be called after level.create_map() + stats = Stats(self.screen, level.list_knights_blue, level.list_knights_red) print_numbers_flag = False running = True @@ -61,6 +60,7 @@ class Game: running = False if event.key == pygame.K_n: print_numbers_flag = not print_numbers_flag + 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 8d12156..a1d05ee 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -15,25 +15,14 @@ class Stats: self.screen = screen self.x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 self.y = 5 - self.max_hp = 0 - self.blue_team_current_hp = 0 - self.red_team_current_hp = 0 - - for knight in list_knights_red: - self.max_hp += knight.max_hp - self.red_team_current_hp += knight.current_hp - - for knight in list_knights_blue: - self.blue_team_current_hp += knight.current_hp - self.blue_team_hp_bar = HealthBar(self.screen, pygame.Rect(self.x + 30, self.y + 210, 100, 15), - current_hp=self.blue_team_current_hp, - max_hp=self.max_hp) + 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=self.red_team_current_hp, - max_hp=self.max_hp) + 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): @@ -52,14 +41,18 @@ 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() # texts draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue - draw_text('Fortece: 0', FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) # red + draw_text('Fortece: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 35, self.y + 270, 18) # red - draw_text('Rycerze: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 215, self.y + 240, 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