summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-31 13:47:27 +0300
committerViljami Ilola <+@hix.fi>2024-03-31 13:47:27 +0300
commit20b23c0cd4cf613a2634cc57bf6f2430cc9fc364 (patch)
tree8c997e4f2b961a94e2eacf9593a9134b2bd0776e
parentab9ba6bd09a3b8a3ee89656b099f8f1c002c7f06 (diff)
Mainmenu selector sprite group
-rw-r--r--src/sliceitoff/game/game.py10
-rw-r--r--src/sliceitoff/mainmenu/__init__.py2
-rw-r--r--src/sliceitoff/mainmenu/mainmenu.py64
-rw-r--r--src/sliceitoff/screens/mainmenu.py19
4 files changed, 88 insertions, 7 deletions
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index b7d5212..18c47b2 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -11,6 +11,7 @@ from text import Fonts
from stats import Stats
from screens import welcome_screen, hiscores_screen
from hiscores import HiScores
+from mainmenu import Mainmenu
from .level import Level
from .show import Show
@@ -63,8 +64,17 @@ class Game:
self.display.update([initials])
return initials.name
+ def mainmenu(self):
+ """ menu where one select what to do """
+ menu = Mainmenu()
+ while menu.active:
+ menu.update(dt = self.clock.tick())
+ self.display.update([menu])
+ return menu.selection
+
def run(self):
""" This is the main loop of the game (not loop at the moment) """
+ self.mainmenu()
self.welcome()
self.newgame()
if self.hiscores.high_enough(self.stats.score):
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))
diff --git a/src/sliceitoff/screens/mainmenu.py b/src/sliceitoff/screens/mainmenu.py
index e4c2dc1..2eacefe 100644
--- a/src/sliceitoff/screens/mainmenu.py
+++ b/src/sliceitoff/screens/mainmenu.py
@@ -1,17 +1,22 @@
""" screens.mainmenu - Screen for mainmenu"""
-from randon import randrange
+from random import randrange
from text import TextPage
def mainmenu_screen(selection):
""" Screen where current selection is flashing """
- color = randrange(0xe0,0xf0)
+ active = randrange(0xe0,0xf0)
+ inactive = 0xe8
return TextPage(
f" Slice it off!!\n"
- f"\n"
- f"{color if selection == 0 else '\xe8'}New Game"
- f"{color if selection == 0 else '\xe8'}High Scores\n"
- f"{color if selection == 0 else '\xe8'}Instructions\n"
- f"{color if selection == 0 else '\xe8'}Quit, Why?",
+ f"\n\n"
+ f"{chr(active if selection == 0 else inactive)}"
+ f"New Game\n\n"
+ f"{chr(active if selection == 1 else inactive)}"
+ f"High Scores\n\n"
+ f"{chr(active if selection == 2 else inactive)}"
+ f"Instructions\n\n"
+ f"{chr(active if selection == 3 else inactive)}"
+ f"Quit, Why?",
font = '8x8',
size = (16_000, 16_000),
pos = (24_000, 32_000) )