-
Notifications
You must be signed in to change notification settings - Fork 3
/
health.py
110 lines (91 loc) · 3.98 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pygame
import random
import os
import pyautogui
class healthbar:
pygame.init()
def __init__ (self):
#Value of player's starting health
self.player_health = 100
#Shield init value, starts at 0 until regeneration begins
self.player_shield = 0
#Fonts
self.font = pygame.font.Font('freesansbold.ttf', 32)
#Window Constants
#self.WIDTH, self.HEIGHT = pyautogui.size()
#Health Constants
#self.player_health = 100 #Health of the player, as a percent
#Colors
self.GREEN = (0, 255, 0)
self.YELLOW = (224, 247, 20)
self.RED = (255, 0, 0)
self.BLACK = (0, 0, 0)
self.BLUE = (63, 229, 235)
self.COLOR = ()
#Generation of the healthbar
def gen_healthbar(self, window, WIDTH):
#Checks to change color based on the player's health, changing form green to yellow to red as they get clsoer to loosing all of their health
if self.player_health <= 100 and self.player_health > 50:
self.COLOR = self.GREEN
pygame.draw.rect(window, self.GREEN, [WIDTH - 350, 10, 300 * (self.player_health/100), 50])
elif self.player_health <= 50 and self.player_health > 25:
self.COLOR = self.YELLOW
pygame.draw.rect(window, self.YELLOW, [WIDTH - 350, 10, 300 * (self.player_health/100), 50])
elif self.player_health <= 25 and self.player_health > 0:
self.COLOR = self.RED
pygame.draw.rect(window, self.RED, [WIDTH - 350, 10, 300 * (self.player_health/100), 50])
pygame.draw.rect(window, self.COLOR, [WIDTH - 350, 10, 302, 52], 2)
self.gen_health_percent(window, WIDTH)
def gen_health_percent(self, window, WIDTH):
health = self.font.render(str(round(self.player_health)) + " |", True, self.COLOR)
healthRect = health.get_rect()
if self.player_health == 100:
healthRect.center = (WIDTH - 150, 140)
else:
healthRect.center = (WIDTH - 140, 140)
window.blit(health, healthRect)
def gen_shieldbar(self, window, WIDTH):
pygame.draw.rect(window, self.BLUE, [WIDTH - 350, 60, 302, 52], 2)
pygame.draw.rect(window, self.BLUE, [WIDTH - 350, 60, 300 * (self.player_shield/50), 50])
self.gen_shieldbar_percent(window, WIDTH)
def gen_shieldbar_percent(self, window, WIDTH):
health = self.font.render("| " + str(round(self.player_shield)), True, self.BLUE)
healthRect = health.get_rect()
if self.player_health == 100:
healthRect.center = (WIDTH - 80, 140)
else:
healthRect.center = (WIDTH - 70, 140)
window.blit(health, healthRect)
def take_damage(self, dmg):
if self.player_shield >= dmg:
self.player_shield -= dmg
elif self.player_shield > 0 and self.player_shield < dmg:
dmg -= self.player_shield
self.player_shield = 0
self.player_health -= dmg
else:
self.player_health -= dmg
self.player_health = int(round(max(self.player_health, 0)))
self.player_shield = int(round(max(self.player_shield, 0)))
def heal(self, recover):
if self.player_health < 100:
if self.player_health + recover > 100:
self.player_health = 100
else:
self.player_health = self.player_health + recover
else:
if self.player_shield == 50:
pass
elif self.player_shield + recover > 50:
self.player_shield = 50
else:
self.player_shield = self.player_shield + recover
def regen(self, recover):
if self.player_shield == 50:
pass
elif self.player_shield + recover > 50:
self.player_shield = 50
else:
self.player_shield = self.player_shield + recover
def get_health(self):
return self.player_health