diff options
Diffstat (limited to 'src/sliceitoff/mainmenu')
-rw-r--r-- | src/sliceitoff/mainmenu/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/mainmenu/mainmenu.py | 64 |
2 files changed, 66 insertions, 0 deletions
diff --git a/src/sliceitoff/mainmenu/__init__.py b/src/sliceitoff/mainmenu/__init__.py new file mode 100644 index 0000000..3dd1fe3 --- /dev/null +++ b/src/sliceitoff/mainmenu/__init__.py @@ -0,0 +1,2 @@ +""" mainmenu - Menu where user can new game, quit etc """ +from .mainmenu import Mainmenu diff --git a/src/sliceitoff/mainmenu/mainmenu.py b/src/sliceitoff/mainmenu/mainmenu.py new file mode 100644 index 0000000..c907443 --- /dev/null +++ b/src/sliceitoff/mainmenu/mainmenu.py @@ -0,0 +1,64 @@ +""" mainmenu.mainmenu - Let's user choose """ +from enum import Enum +import pygame + +from screens import mainmenu_screen + +from game.anykey import anykey + +class MenuItems(Enum): + """ Items in the menu. Should match mainmenu_screen """ + NEWGAME = 0 + HISCORES = 1 + INSTRUCT = 2 + QUIT = 3 + +class Mainmenu(pygame.sprite.Group): + """ 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 + + def update(self, dt = 0): + """ 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 + return + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.selection = MenuItems.QUIT + self.explode = True + 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) + self.empty() + self.add(mainmenu_screen(self.selection)) |