From: 711215 <107773170+711215@users.noreply.github.com> Date: Wed, 6 Jul 2022 21:49:13 +0000 (-0400) Subject: player healthbar complete X-Git-Url: http://git.skullheadx.com/nixos/README?a=commitdiff_plain;h=07892dc8065bf69d062996823c8b89d55ab4078a;p=Pygame-Jam.git player healthbar complete --- diff --git a/Player.py b/Player.py index d83a7d0..c90ea96 100644 --- 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)