diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-07 23:25:09 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-07 23:25:09 +0300 |
commit | 6ba20c61a04c688120161f6c5fbb56102f8082ef (patch) | |
tree | 33291a2c494b8fe5e126e31b5ce719aa0df0feec /src/sliceitoff/mainmenu/mainmenu.py | |
parent | d2e42bfa99d8bb417d8eaf365d290a071ad49b7f (diff) |
exploding sprites and groups as their own. more refactoring.
Diffstat (limited to 'src/sliceitoff/mainmenu/mainmenu.py')
-rw-r--r-- | src/sliceitoff/mainmenu/mainmenu.py | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/src/sliceitoff/mainmenu/mainmenu.py b/src/sliceitoff/mainmenu/mainmenu.py index 551e7b5..7d52cd8 100644 --- a/src/sliceitoff/mainmenu/mainmenu.py +++ b/src/sliceitoff/mainmenu/mainmenu.py @@ -5,7 +5,7 @@ import pygame from screens import mainmenu_screen from display import Scaling -from game.anykey import anykey +from game.explodeout import ExplodeOutGroup MOUSE_TRESHOLD = 100 @@ -16,63 +16,58 @@ class MenuItems(IntEnum): INSTRUCT = 2 QUIT = 3 -class Mainmenu(pygame.sprite.Group): +class Mainmenu(ExplodeOutGroup): """ sprite group with imputs to make selection """ def __init__(self): super().__init__() self.add(mainmenu_screen(0)) - self.explode = False - self.active = True - self.fadeout = 1_000 self.selection = 0 self.mousey = 0 - def update(self, dt = 0): + def update(self, dt = 0, **kwargs): """ Does it all. Reads keyboard and updates screen """ - if not self.active: - return - - if self.explode: - for sprite in self.sprites(): - sprite.update(dt = dt, explode = self.explode) - if self.fadeout <= 0: - self.active = False - else: - if anykey(): - self.fadeout = 0 - self.active = False - self.fadeout -= dt + if not super().update(dt = dt, **kwargs) or self.explode: return for event in pygame.event.get(): if event.type == pygame.QUIT: self.selection = MenuItems.QUIT - self.explode = True + self.do_fadeout() break if event.type == pygame.MOUSEBUTTONDOWN and event.button <= 3: - self.explode = True + self.do_fadeout() break if event.type == pygame.KEYDOWN: - match event.key: - case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: - self.explode = True - break - case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: - self.selection = MenuItems.QUIT - self.explode = True - break - case pygame.K_UP: - self.selection -= 1 - self.selection %= len(MenuItems) - case pygame.K_DOWN: - self.selection += 1 - self.selection %= len(MenuItems) + if self.process_key(event.key): + break elif event.type == pygame.MOUSEMOTION: - self.mousey += pygame.mouse.get_rel()[1] - pygame.mouse.set_pos(Scaling.center) - if abs(self.mousey) > MOUSE_TRESHOLD: - self.selection += 1 if self.mousey > 0 else -1 - self.selection %= len(MenuItems) - self.mousey = 0 + self.process_mouse_motion() self.empty() self.add(mainmenu_screen(self.selection)) + + def process_mouse_motion(self): + """ Mouse movement up or down moves menu selection """ + self.mousey += pygame.mouse.get_rel()[1] + pygame.mouse.set_pos(Scaling.center) + if abs(self.mousey) > MOUSE_TRESHOLD: + self.selection += 1 if self.mousey > 0 else -1 + self.selection %= len(MenuItems) + self.mousey = 0 + + def process_key(self, key): + """ Processes known key presses """ + match key: + case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: + self.do_fadeout() + return True + case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: + self.selection = MenuItems.QUIT + self.do_fadeout() + return True + case pygame.K_UP: + self.selection -= 1 + self.selection %= len(MenuItems) + case pygame.K_DOWN: + self.selection += 1 + self.selection %= len(MenuItems) + return False |