mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add button highlight
This commit is contained in:
parent
7d5bf8e658
commit
6fb7fee090
@ -25,7 +25,6 @@ class Main(BaseScreen, SceenElement, TextScreen):
|
|||||||
"""Update the display."""
|
"""Update the display."""
|
||||||
self._draw_background()
|
self._draw_background()
|
||||||
self._draw_text()
|
self._draw_text()
|
||||||
self._draw_border()
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
@ -38,6 +37,18 @@ class Main(BaseScreen, SceenElement, TextScreen):
|
|||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_q:
|
if event.key == pygame.K_q:
|
||||||
self.exit()
|
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:
|
def run(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@ -12,6 +12,7 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
|
|||||||
super().__init__(text, action)
|
super().__init__(text, action)
|
||||||
self._initialize_surface()
|
self._initialize_surface()
|
||||||
self._initialize_font()
|
self._initialize_font()
|
||||||
|
self.hover = False
|
||||||
|
|
||||||
def on_click(self) -> None:
|
def on_click(self) -> None:
|
||||||
"""Handle click event."""
|
"""Handle click event."""
|
||||||
@ -20,21 +21,23 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
|
|||||||
|
|
||||||
def on_hover(self) -> None:
|
def on_hover(self) -> None:
|
||||||
"""Handle hover event."""
|
"""Handle hover event."""
|
||||||
self._draw_border()
|
self._draw_hover_background()
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Display the button on the game surface."""
|
|
||||||
self.draw()
|
|
||||||
|
|
||||||
def update(self) -> None:
|
|
||||||
"""Update the button."""
|
|
||||||
pass
|
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:
|
def draw(self, surface: pygame.Surface, pos: tuple[float, float]) -> None:
|
||||||
"""Draw the button on the button surface."""
|
"""Draw the button on the button surface."""
|
||||||
self._initialize_rect(pos)
|
self._initialize_rect(pos)
|
||||||
self._update_display_surface()
|
self._update_display_surface()
|
||||||
self._draw_background()
|
if self.hover:
|
||||||
|
self.on_hover()
|
||||||
|
else:
|
||||||
|
self._draw_background()
|
||||||
self._draw_text()
|
self._draw_text()
|
||||||
self._draw_border()
|
self._draw_border()
|
||||||
|
|
||||||
@ -73,6 +76,10 @@ class MenuButton(Button, BaseScreen, SceenElement, TextScreen):
|
|||||||
"""Fill the background of the button."""
|
"""Fill the background of the button."""
|
||||||
self.surface.fill(CONFIG.colors.bg_sidebar)
|
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:
|
def _draw_border(self) -> None:
|
||||||
"""Draw the border of the button."""
|
"""Draw the border of the button."""
|
||||||
pygame.draw.rect(
|
pygame.draw.rect(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user