healthbar #22
@ -33,3 +33,6 @@ TILES = [
|
||||
'water.png',
|
||||
'grass_with_tree.jpg',
|
||||
]
|
||||
|
||||
#HEALTH_BAR
|
||||
BAR_ANIMATION_SPEED = 1
|
||||
|
49
logic/health_bar.py
Normal file
49
logic/health_bar.py
Normal 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)
|
||||
|
||||
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import pygame.image
|
||||
import random
|
||||
|
||||
|
||||
class Knight(pygame.sprite.Sprite):
|
||||
def __init__(self, img):
|
||||
super().__init__()
|
||||
@ -14,4 +15,3 @@ class Knight(pygame.sprite.Sprite):
|
||||
self.attack = random.randint(4, 7)
|
||||
self.defense = random.randint(1, 4)
|
||||
self.points = 1
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user