Remove unnecessary field in UiBar class

This commit is contained in:
Michał Czekański 2020-05-14 21:15:57 +02:00 committed by Marcin Kostrzewski
parent 9d1ecf5d90
commit 08643c7083

View File

@ -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__()