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