I’m new to pygame and relatively new to coding in general, so I’m sure some of my code is unnecessary or inefficient. I’m working on a lifebar, and I have a green bar on top of a red bar to show their health. The lifebars are scaled to the size of the screen and the red one scales correctly, but the green one stays how it was. The width of the bar is supposed to be based on the ratio, self.health_ratio, between the current health and total health, and that’s where the scaling messes up, however, I can’t figure out how to fix it. Any other tips would also be greatly appreciated, ty <3
import pygame
class Lifebars:
def init (self,rpg_game):
“””Initialize the lifebar and set its starting position”””
self.screen = rpg_game.screen
self.settings = rpg_game.settings
self.screen_rect = rpg_game.screen
#load lifebar image and get it’s rect
#green lifebar/ health
self.lifebar_pos = pygame.image.load(“images/lifebars/lifebar_full.bmp”)
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150,50))
self.lifebar_pos_rect = self.lifebar_pos.get_rect()
#red lifebar/damage
self.lifebar_neg = pygame.image.load(“images/lifebars/red_lifebar_full.bmp”)
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150,50))
self.lifebar_neg_rect = self.lifebar_neg.get_rect()
#times resized
self.resized = 0
#health ratio
self.ratio = 1.0
def _lifebar_check(self,health, max_health):
self.ratio = health/max_health
def on_resize(self, new_screen_rect, width_ratio, height_ratio):
“””When screen is resized, lifebar is resized too”””
self.resized += 1
self.screen_rect = new_screen_rect
if self.resized % 2 == 0:
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150,50))
self.lifebar_pos_rect.x = int(self.lifebar_pos_rect.x * width_ratio)
self.lifebar_pos_rect.y = int(self.lifebar_pos_rect.y * height_ratio)
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150,50))
self.lifebar_neg_rect.x = int(self.lifebar_neg_rect.x * width_ratio)
self.lifebar_neg_rect.y = int(self.lifebar_neg_rect.y * height_ratio)
else:
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150 * width_ratio, 50 * height_ratio))
self.lifebar_pos_rect.x = int(self.lifebar_pos_rect.x * width_ratio)
self.lifebar_pos_rect.y = int(self.lifebar_pos_rect.y * height_ratio)
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150 * width_ratio,50 * height_ratio))
self.lifebar_neg_rect.x = int(self.lifebar_neg_rect.x * width_ratio)
self.lifebar_neg_rect.y = int(self.lifebar_neg_rect.y * height_ratio)
def blitme(self):
“””Draw the lifebar”””
self.screen.blit(self.lifebar_neg,self.lifebar_neg_rect)
#this is the part that fucks it up
#self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (self.lifebar_pos_rect.width * self.ratio, self.lifebar_pos_rect.height))
#end of part that fucks it up
import pygame
class Lifebars:
def init (self,rpg_game):
“””Initialize the lifebar and set its starting position”””
self.screen = rpg_game.screen
self.settings = rpg_game.settings
self.screen_rect = rpg_game.screen
#load lifebar image and get it's rect
#green lifebar/ health
self.lifebar_pos = pygame.image.load("images/lifebars/lifebar_full.bmp")
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150,50))
self.lifebar_pos_rect = self.lifebar_pos.get_rect()
#red lifebar/damage
self.lifebar_neg = pygame.image.load("images/lifebars/red_lifebar_full.bmp")
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150,50))
self.lifebar_neg_rect = self.lifebar_neg.get_rect()
#times resized
self.resized = 0
#health ratio
self.health_ratio = 1.0
def _lifebar_check(self,health, max_health):
self.health_ratio = health/max_health
def on_resize(self, new_screen_rect, width_ratio, height_ratio):
"""When screen is resized, lifebar is resized too"""
self.resized += 1
self.screen_rect = new_screen_rect
if self.resized % 2 == 0:
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150,50))
self.lifebar_pos_rect.x = int(self.lifebar_pos_rect.x * width_ratio)
self.lifebar_pos_rect.y = int(self.lifebar_pos_rect.y * height_ratio)
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150,50))
self.lifebar_neg_rect.x = int(self.lifebar_neg_rect.x * width_ratio)
self.lifebar_neg_rect.y = int(self.lifebar_neg_rect.y * height_ratio)
else:
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (150 * width_ratio, 50 * height_ratio))
self.lifebar_pos_rect.x = int(self.lifebar_pos_rect.x * width_ratio)
self.lifebar_pos_rect.y = int(self.lifebar_pos_rect.y * height_ratio)
self.lifebar_neg = pygame.transform.scale(self.lifebar_neg, (150 * width_ratio,50 * height_ratio))
self.lifebar_neg_rect.x = int(self.lifebar_neg_rect.x * width_ratio)
self.lifebar_neg_rect.y = int(self.lifebar_neg_rect.y * height_ratio)
def blitme(self):
"""Draw the lifebar"""
self.screen.blit(self.lifebar_neg,self.lifebar_neg_rect)
#this is the part that fucks it up
self.lifebar_pos = pygame.transform.scale(self.lifebar_pos, (self.lifebar_pos_rect.width * self.health_ratio, self.lifebar_pos_rect.height))
#end of part that fucks it up
self.screen.blit(self.lifebar_pos, self.lifebar_pos_rect)