86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
import sys
|
|
from enum import Enum
|
|
from typing import Any
|
|
|
|
from .enums import GameMode, KeyValues
|
|
from .settings import Settings
|
|
|
|
|
|
class Menu:
|
|
values: list
|
|
|
|
def __init__(self):
|
|
self.position = 0
|
|
|
|
def go_up(self) -> None:
|
|
self.position = max(0, self.position - 1)
|
|
|
|
def go_down(self) -> None:
|
|
self.position = min(len(self.values) - 1, self.position + 1)
|
|
|
|
def validate(self) -> Any:
|
|
return self.values[self.position]
|
|
|
|
|
|
class MainMenuValues(Enum):
|
|
START = 'Jouer'
|
|
SETTINGS = 'Paramètres'
|
|
EXIT = 'Quitter'
|
|
|
|
def __str__(self):
|
|
return self.value
|
|
|
|
|
|
class MainMenu(Menu):
|
|
values = [e for e in MainMenuValues]
|
|
|
|
def handle_key_pressed(self, key: KeyValues, game: Any) -> None:
|
|
"""
|
|
In the main menu, we can navigate through options.
|
|
"""
|
|
if key == KeyValues.DOWN:
|
|
self.go_down()
|
|
if key == KeyValues.UP:
|
|
self.go_up()
|
|
if key == KeyValues.ENTER:
|
|
option = self.validate()
|
|
if option == MainMenuValues.START:
|
|
game.state = GameMode.PLAY
|
|
elif option == MainMenuValues.SETTINGS:
|
|
game.state = GameMode.SETTINGS
|
|
elif option == MainMenuValues.EXIT:
|
|
sys.exit(0)
|
|
|
|
|
|
class SettingsMenu(Menu):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def update_values(self, settings: Settings) -> None:
|
|
s = settings.dumps_to_string()
|
|
self.values = s[6:-2].replace("\"", "").split(",\n ")
|
|
|
|
def handle_key_pressed(self, key: KeyValues, settings: Settings) -> None:
|
|
"""
|
|
For now, in the settings mode, we can only go backwards.
|
|
"""
|
|
if key == KeyValues.SPACE:
|
|
self.state = GameMode.MAINMENU
|
|
if key == KeyValues.DOWN:
|
|
self.go_down()
|
|
if key == KeyValues.UP:
|
|
self.go_up()
|
|
if key == KeyValues.ENTER:
|
|
option = self.validate().split(": ")[0]
|
|
if option != "TEXTURE_PACK":
|
|
newkey = screen.getkey()
|
|
self.settings.__setattr__(option, newkey)
|
|
self.settings.write_settings()
|
|
self.settings_menu.update_values(self.settings)
|
|
|
|
|
|
class ArbitraryMenu(Menu):
|
|
def __init__(self, values: list):
|
|
super().__init__()
|
|
self.values = values
|