healthbar WIP

This commit is contained in:
XsedoX 2022-04-11 17:51:46 +02:00
parent 62e78ce885
commit 0a7b6136a5
3 changed files with 54 additions and 2 deletions

View File

@ -33,3 +33,6 @@ TILES = [
'water.png', 'water.png',
'grass_with_tree.jpg', 'grass_with_tree.jpg',
] ]
#HEALTH_BAR
BAR_ANIMATION_SPEED = 1

49
logic/health_bar.py Normal file
View File

@ -0,0 +1,49 @@
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)

View File

@ -1,6 +1,7 @@
import pygame.image import pygame.image
import random import random
class Knight(pygame.sprite.Sprite): class Knight(pygame.sprite.Sprite):
def __init__(self, img): def __init__(self, img):
super().__init__() super().__init__()
@ -14,4 +15,3 @@ class Knight(pygame.sprite.Sprite):
self.attack = random.randint(4, 7) self.attack = random.randint(4, 7)
self.defense = random.randint(1, 4) self.defense = random.randint(1, 4)
self.points = 1 self.points = 1