From: lbcmk <30442978+lbcmk@users.noreply.github.com> Date: Sun, 10 Jul 2022 00:39:47 +0000 (-0400) Subject: Moved the healthbar into its own import file X-Git-Url: http://git.skullheadx.com/nixos/static/index.html?a=commitdiff_plain;h=05006e4da0bb207f2ef7546a71480a72f36ce0a2;p=Pygame-Jam.git Moved the healthbar into its own import file --- diff --git a/Game.py b/Game.py index d0ef0d5..a4eda61 100644 --- 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() diff --git a/Player.py b/Player.py index 59f9b18..144867a 100644 --- 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 index 0000000..8021781 --- /dev/null +++ b/UI/HealthBar.py @@ -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)