feat(game): add button highlight

This commit is contained in:
Kristofers Solo 2024-01-06 05:17:09 +02:00
parent 7d5bf8e658
commit 6fb7fee090
2 changed files with 26 additions and 8 deletions

View File

@ -25,7 +25,6 @@ class Main(BaseScreen, SceenElement, TextScreen):
"""Update the display."""
self._draw_background()
self._draw_text()
self._draw_border()
pygame.display.update()
def update(self) -> None:
@ -38,6 +37,18 @@ class Main(BaseScreen, SceenElement, TextScreen):
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
self.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
for button in self.buttons:
if button.rect.collidepoint(mouse_pos):
button.on_click()
elif event.type == pygame.MOUSEMOTION:
mouse_pos = pygame.mouse.get_pos()
for button in self.buttons:
if button.rect.collidepoint(mouse_pos):
button.update(True)
else:
button.update(False)
def run(self) -> None:
while True:

View File

@ -12,6 +12,7 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
super().__init__(text, action)
self._initialize_surface()
self._initialize_font()
self.hover = False
def on_click(self) -> None:
"""Handle click event."""
@ -20,20 +21,22 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
def on_hover(self) -> None:
"""Handle hover event."""
self._draw_border()
self._draw_hover_background()
def run(self) -> None:
"""Display the button on the game surface."""
self.draw()
def update(self) -> None:
"""Update the button."""
pass
def update(self, hover: bool = False) -> None:
"""Update the button."""
self.hover = hover
def draw(self, surface: pygame.Surface, pos: tuple[float, float]) -> None:
"""Draw the button on the button surface."""
self._initialize_rect(pos)
self._update_display_surface()
if self.hover:
self.on_hover()
else:
self._draw_background()
self._draw_text()
self._draw_border()
@ -73,6 +76,10 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
"""Fill the background of the button."""
self.surface.fill(CONFIG.colors.bg_sidebar)
def _draw_hover_background(self) -> None:
"""Fill the background of the button."""
self.surface.fill(CONFIG.colors.bg_visual)
def _draw_border(self) -> None:
"""Draw the border of the button."""
pygame.draw.rect(