diff --git a/src/ui/UiBar.py b/src/ui/UiBar.py index 8b61cd2..8368cfc 100644 --- a/src/ui/UiBar.py +++ b/src/ui/UiBar.py @@ -6,12 +6,11 @@ from src.ui.UiElement import UiElement class UiBar(UiElement): - value: int filledBarColor: Tuple[int, int, int] outlineThickness: int outlineColor: Tuple[int, int, int] emptyBarColor: Tuple[int, int, int] - filledPercent: float + filledPercent: int def __init__(self, rect: pygame.Rect, initialFilledPercent: int = 100, filledBarColor: Tuple[int, int, int] = (255, 0, 0), emptyBarColor: Tuple[int, int, int] = (0, 0, 0), @@ -26,23 +25,29 @@ class UiBar(UiElement): :param outlineThickness: """ super().__init__(rect) - self.filledPercent = initialFilledPercent / 100 + + # Make sure that filled percent is between 0 and 100 + if initialFilledPercent < 0: + initialFilledPercent = 0 + elif initialFilledPercent > 100: + initialFilledPercent = 100 + self.filledPercent = initialFilledPercent + self.emptyBarColor = emptyBarColor self.barColor = filledBarColor self.outlineColor = outlineColor self.outlineThickness = outlineThickness self.filledBarColor = filledBarColor - self.value = initialFilledPercent self.__genBar__() def __genBar__(self): """ - Generates bar image. + Generates bar image based on filled percent field. """ self.image = pygame.Surface((self.rect.width, self.rect.height)) filledPartRect = pygame.rect.Rect(self.outlineThickness / 2, self.outlineThickness / 2, - (self.rect.width - self.outlineThickness) * self.filledPercent, + (self.rect.width - self.outlineThickness) * (self.filledPercent / 100), self.rect.height - self.outlineThickness) self.image.fill(self.filledBarColor, filledPartRect) pygame.draw.rect(self.image, self.outlineColor, pygame.rect.Rect(0, 0, self.rect.width, self.rect.height), @@ -53,6 +58,5 @@ class UiBar(UiElement): Updates how much bar is filled :param filledPercent: Value between 0 and 100 """ - self.filledPercent = filledPercent / 100 - self.value = filledPercent + self.filledPercent = filledPercent self.__genBar__()