import pygame 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, 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 = 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: 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 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))