refactor(game): mouse position

This commit is contained in:
Kristofers Solo 2024-01-06 16:50:37 +02:00
parent 40b6556c97
commit a56f457482
2 changed files with 11 additions and 9 deletions

View File

@ -17,14 +17,18 @@ class Button(BaseButton, BaseScreen, SceenElement, TextScreen):
self._set_default_background_color()
self._set_default_text_color()
def on_click(self) -> None:
def on_click(self, event: pygame.Event) -> None:
"""Handle click event."""
if self.action:
if (
event.type == pygame.MOUSEBUTTONDOWN
and self.rect.collidepoint(event.pos)
and self.action
):
self.action()
def on_hover(self, event: pygame.Event) -> None:
"""Handle hover event."""
if self.rect.collidepoint(event.pos):
if event.type == pygame.MOUSEMOTION and self.rect.collidepoint(event.pos):
self._set_background_color(CONFIG.colors.bg_visual)
else:
self._set_default_background_color()

View File

@ -38,13 +38,10 @@ 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:
if not self.game:
for button in self.buttons:
button.on_click(event)
button.on_hover(event)
def run(self) -> None:
@ -66,6 +63,7 @@ class Main(BaseScreen, SceenElement, TextScreen):
sys.exit()
def play(self) -> "Main":
self._draw_background()
self.game = Game(self.game_mode)
return self