]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
player healthbar complete
author711215 <107773170+711215@users.noreply.github.com>
Wed, 6 Jul 2022 21:49:13 +0000 (17:49 -0400)
committerGitHub <noreply@github.com>
Wed, 6 Jul 2022 21:49:13 +0000 (17:49 -0400)
Player.py

index d83a7d0fd72aff660e5f5fdbdd2b5b232b5e9aee..c90ea968e6fabcccc5aa8f580d3fb8a4f5389349 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -40,16 +40,20 @@ class Player(Actor):
 
     
         #Healthbar Stuff
-        # self.position is the top left   
-
-        background_rect = pg.Rect(0, 0, 1080*0.2, 640*0.08)
-        background_rect.center = (1080*0.5, 50)
-        foreground_rect = pg.Rect(0, 0, 1080*0.19, 640*0.07)
-        foreground_rect.center = background_rect.center
+        #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, (255, 0, 0), foreground_rect)
         
-        font = pg.font.Font("Font/Exo2-Regular.ttf", 20)
+        #text
+        font = pg.font.Font("Font/Exo2-Regular.ttf" , 30)
         current_health = str(self.health) + "/100"
         current_health_display = font.render(current_health, True, (255, 255, 255))
-        surf.blit(current_health_display, foreground_rect)
+        text_rect = current_health_display.get_rect(center = background_rect.center)
+        surf.blit(current_health_display, text_rect)