From e5af464eb2d5f2f2b7a99ca86f8bc41b0715efaf Mon Sep 17 00:00:00 2001 From: XsedoX Date: Sun, 24 Apr 2022 22:06:20 +0200 Subject: [PATCH] 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)