-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.py
28 lines (25 loc) · 972 Bytes
/
health.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pygame
class HealthBar(pygame.sprite.Sprite):
def __init__(self, hp):
super(HealthBar, self).__init__()
self.max_hp = hp
self.hp = self.max_hp
self.image = pygame.image.load("resources/UI_Health.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (self.image.get_width() //2 + 17, self.image.get_height() // 2))
self.rect = self.image.get_rect()
self.rect.y = 8
self.rect.x = 33
self.velX = 0
self.velY = 0
self.max_width = self.image.get_width()
def update(self):
self.rect.x += self.velX
self.rect.y += self.velY
def decrease_hp(self, dmg):
self.hp -= dmg
self.image = pygame.transform.scale(self.image, (self.max_width * self.hp // self.max_hp, self.image.get_height()))
x = self.rect.x
y = self.rect.y
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y