summaryrefslogtreecommitdiff
path: root/src/sliceitoff/mainmenu/mainmenu.py
blob: 551e7b54b811a0a83c87adcbff7c04ee133f9063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
""" mainmenu.mainmenu - Let's user choose """
from enum import IntEnum
import pygame

from screens import mainmenu_screen
from display import Scaling

from game.anykey import anykey

MOUSE_TRESHOLD = 100

class MenuItems(IntEnum):
    """ 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
        self.mousey = 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.MOUSEBUTTONDOWN and event.button <= 3:
                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)
            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.empty()
        self.add(mainmenu_screen(self.selection))