WMICraft/logic/health_bar.py
2022-04-11 17:51:46 +02:00

50 lines
1.5 KiB
Python

import pygame
from common.constants import BAR_ANIMATION_SPEED
class HealthBar:
def __init__(self, rect, current_hp, max_hp):
self.rect = rect
self.bar_animation_speed = BAR_ANIMATION_SPEED
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.health_ratio = self.max_hp/self.width
def update(self):
self.animation()
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 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)