From 786db30b588ba3a825a230e27e7b589688f6d854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Thu, 14 May 2020 21:15:57 +0200 Subject: [PATCH] Remove unnecessary field in UiBar class --- src/ui/UiBar.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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__()