Back to menu by pressing space button

This commit is contained in:
Yohann D'ANELLO 2020-11-08 23:26:54 +01:00
parent 7e92086601
commit 259bde81a4
2 changed files with 52 additions and 26 deletions

View File

@ -1,6 +1,7 @@
import sys
from typing import Any
from .display.display import Display
from .display.mapdisplay import MapDisplay
from .display.menudisplay import MenuDisplay
from .display.texturepack import TexturePack
@ -24,9 +25,16 @@ class KeyValues(Enum):
LEFT = auto()
RIGHT = auto()
ENTER = auto()
SPACE = auto()
class Game:
map: Map
player: Player
menu_display: MenuDisplay
map_display: MapDisplay
current_display: Display
def __init__(self) -> None:
self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu()
@ -36,13 +44,13 @@ class Game:
def new_game(self, screen: Any) -> None:
# TODO generate a new map procedurally
self.m = Map.load("example_map.txt")
self.map = Map.load("example_map.txt")
self.player = Player()
self.player.move(1, 6)
self.m.add_entity(self.player)
self.map.add_entity(self.player)
self.menu_display = MenuDisplay(screen, self.main_menu, 0, 0)
self.map_display = MapDisplay(
screen, self.m, self.player,
screen, self.map, self.player,
TexturePack.get_pack(self.settings.TEXTURE_PACK))
self.current_display = self.menu_display
@ -75,29 +83,47 @@ class Game:
return KeyValues.UP
elif key == self.settings.KEY_ENTER:
return KeyValues.ENTER
elif key == ' ':
return KeyValues.SPACE
def handle_key_pressed(self, key: KeyValues) -> None:
if self.state == GameMode.PLAY:
if key == KeyValues.UP:
self.player.move_up()
if key == KeyValues.DOWN:
self.player.move_down()
if key == KeyValues.LEFT:
self.player.move_left()
if key == KeyValues.RIGHT:
self.player.move_right()
if self.state == GameMode.MAINMENU:
if key == KeyValues.DOWN:
self.main_menu.go_down()
if key == KeyValues.UP:
self.main_menu.go_up()
if key == KeyValues.ENTER:
option = self.main_menu.validate()
if option == menus.MainMenuValues.START:
self.state = GameMode.PLAY
self.current_display = self.map_display
elif option == menus.MainMenuValues.SETTINGS:
self.state = GameMode.SETTINGS
elif option == menus.MainMenuValues.EXIT:
sys.exit(0)
self.handle_key_pressed_play(key)
elif self.state == GameMode.MAINMENU:
self.handle_key_pressed_main_menu(key)
elif self.state == GameMode.SETTINGS:
self.handle_key_pressed_settings(key)
self.current_display.refresh()
def handle_key_pressed_play(self, key: KeyValues) -> None:
if key == KeyValues.UP:
self.player.move_up()
elif key == KeyValues.DOWN:
self.player.move_down()
elif key == KeyValues.LEFT:
self.player.move_left()
elif key == KeyValues.RIGHT:
self.player.move_right()
elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU
self.current_display = self.menu_display
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
if key == KeyValues.DOWN:
self.main_menu.go_down()
if key == KeyValues.UP:
self.main_menu.go_up()
if key == KeyValues.ENTER:
option = self.main_menu.validate()
if option == menus.MainMenuValues.START:
self.state = GameMode.PLAY
self.current_display = self.map_display
elif option == menus.MainMenuValues.SETTINGS:
self.state = GameMode.SETTINGS
elif option == menus.MainMenuValues.EXIT:
sys.exit(0)
def handle_key_pressed_settings(self, key: KeyValues) -> None:
if key == KeyValues.SPACE:
self.state = GameMode.MAINMENU
self.current_display = self.menu_display

View File

@ -1,4 +1,4 @@
from enum import Enum, auto
from enum import Enum
from typing import Any