WMICraft/logic/health_bar.py

55 lines
1.6 KiB
Python

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, current_hp, max_hp, x=None, y=None, width=None, height=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
def update(self):
self.show()
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))