]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
Moved the healthbar into its own import file
authorlbcmk <30442978+lbcmk@users.noreply.github.com>
Sun, 10 Jul 2022 00:39:47 +0000 (20:39 -0400)
committerlbcmk <30442978+lbcmk@users.noreply.github.com>
Sun, 10 Jul 2022 00:39:47 +0000 (20:39 -0400)
Game.py
Player.py
UI/HealthBar.py [new file with mode: 0644]

diff --git a/Game.py b/Game.py
index d0ef0d5b20806b45cb2fb3d078cff72e39f691f1..a4eda61d94d2e440ea3f5796db49a38a16120e0a 100644 (file)
--- a/Game.py
+++ b/Game.py
@@ -9,6 +9,7 @@ from PhysicsBody import PhysicsBody
 from World import World
 from EndScreen import EndScreen
 from UI.DashMeter import DashMeter
+from UI.HealthBar import HealthBar
 
 
 class Game:
@@ -30,6 +31,7 @@ class Game:
                         enemy_positions]
         self.scene = EndScreen()
         self.dashMeter = DashMeter(self.player.dashCooldown)
+        self.healthBar = HealthBar()
         self.level = 1
         self.scene.level = self.level
 
@@ -61,6 +63,7 @@ class Game:
         self.player.draw(surf)
         self.dashMeter.update(self.player.lastDash)
         self.dashMeter.draw(surf)
+        self.healthBar.draw(surf, self.player.health)
 
         if self.player.dead:
             self.scene.update()
index 59f9b18649629dbbf992cbaccb7af9e76be4547c..144867aa333fc2b15a96babddc1eb04cf3135f3d 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -159,20 +159,22 @@ class Player(Actor):
 
         # pg.draw.rect(surf,self.colour,pg.Rect(center, (self.width, self.height)),border_radius=8)
 
-        # Healthbar Stuff
-        # bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved
-        background_rect = pg.Rect(20, 20, 1080 * 0.2, 640 * 0.08)
-
-        # idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7
-        foreground_rect = pg.Rect(0, 0, 1080 * 0.185 * (self.health * 0.01), 640 * 0.06)
-        # make sure the red part health bar always sits on the left
-        # sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left
-        foreground_rect.center = (
-            background_rect.centerx - 1080 * 0.185 * ((1 - self.health * 0.01) / 2), background_rect.centery)
-        pg.draw.rect(surf, (54, 54, 54), background_rect)
-        pg.draw.rect(surf, red, foreground_rect)
-
-        # text
-        current_health_display = createText(0, 0, 30, white, "Regular", str(self.health) + "/100")[0]
-        text_rect = current_health_display.get_rect(center=background_rect.center)
-        surf.blit(current_health_display, text_rect)
+
+
+        # # Healthbar Stuff
+        # # bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved
+        # background_rect = pg.Rect(20, 20, 1080 * 0.2, 640 * 0.08)
+
+        # # idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7
+        # foreground_rect = pg.Rect(0, 0, 1080 * 0.185 * (self.health * 0.01), 640 * 0.06)
+        # # make sure the red part health bar always sits on the left
+        # # sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left
+        # foreground_rect.center = (
+        #     background_rect.centerx - 1080 * 0.185 * ((1 - self.health * 0.01) / 2), background_rect.centery)
+        # pg.draw.rect(surf, (54, 54, 54), background_rect)
+        # pg.draw.rect(surf, red, foreground_rect)
+
+        # # text
+        # current_health_display = createText(0, 0, 30, white, "Regular", str(self.health) + "/100")[0]
+        # text_rect = current_health_display.get_rect(center=background_rect.center)
+        # surf.blit(current_health_display, text_rect)
diff --git a/UI/HealthBar.py b/UI/HealthBar.py
new file mode 100644 (file)
index 0000000..8021781
--- /dev/null
@@ -0,0 +1,31 @@
+from Setup import *
+from CommonImports.colours import white, red
+from Function.createText import createText
+from datetime import datetime
+
+class HealthBar:
+
+    def __init__(self):
+        return;
+        
+    def update(self):
+        return;
+
+    def draw(self, surf, health):
+        # Healthbar Stuff
+        # bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved
+        background_rect = pg.Rect(20, 20, 1080 * 0.2, 640 * 0.08)
+
+        # idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7
+        foreground_rect = pg.Rect(0, 0, 1080 * 0.185 * (health * 0.01), 640 * 0.06)
+        # make sure the red part health bar always sits on the left
+        # sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left
+        foreground_rect.center = (
+            background_rect.centerx - 1080 * 0.185 * ((1 - health * 0.01) / 2), background_rect.centery)
+        pg.draw.rect(surf, (54, 54, 54), background_rect)
+        pg.draw.rect(surf, red, foreground_rect)
+
+        # text
+        current_health_display = createText(0, 0, 30, white, "Regular", str(health) + "/100")[0]
+        text_rect = current_health_display.get_rect(center=background_rect.center)
+        surf.blit(current_health_display, text_rect)