Display main menu by default

This commit is contained in:
Yohann D'ANELLO 2020-11-08 22:59:11 +01:00
parent 933385e79d
commit 8ccf8c7b67
4 changed files with 27 additions and 33 deletions

View File

@ -1,25 +1,15 @@
import curses import curses
from typing import Any from typing import Any
from .mapdisplay import MapDisplay
from .texturepack import TexturePack
class Display: class Display:
# game is a game, can't import to avoid circulary includes def __init__(self, screen: Any):
def __init__(self, game: Any, screen: Any):
self.screen = screen self.screen = screen
self.game = game self.rows = curses.LINES if screen else 4
lines = curses.LINES if screen else 4 self.cols = curses.COLS * 4 // 5 if screen else 4
cols = curses.COLS * 4 // 5 if screen else 4
self.map_display = MapDisplay(game.m,
TexturePack.get_pack(
game.settings.TEXTURE_PACK
),
lines, cols, screen is not None)
def refresh(self) -> None: def refresh(self) -> None:
self.map_display.update_pad() raise NotImplementedError
def display(self, y: int, x: int) -> None: def display(self, y: int, x: int) -> None:
self.map_display.display(y, x) raise NotImplementedError

View File

@ -1,25 +1,26 @@
from dungeonbattle.display.display import Display
from dungeonbattle.menus import Menu from dungeonbattle.menus import Menu
from typing import Any from typing import Any
import curses import curses
class MenuDisplay: class MenuDisplay(Display):
def __init__(self, menu: Menu, screen: Any, height: int, width: int, def __init__(self, screen: Any, menu: Menu,
topleftx: int, toplefty: int): topleftx: int, toplefty: int):
self.screen = screen super().__init__(screen)
self.position = menu.position
self.values = menu.values self.values = menu.values
self.width = width self.menu = menu
self.height = height self.width = self.cols
self.height = self.rows
self.trueheight = len(menu.values) self.trueheight = len(menu.values)
self.truewidth = max(map(len, self.values)) self.truewidth = max(len(item.value) for item in menu.values)
self.topleftx = topleftx self.topleftx = topleftx
self.toplefty = toplefty self.toplefty = toplefty
# Menu values are printed in pad # Menu values are printed in pad
self.pad = curses.newpad(self.trueheight, self.truewidth + 1) self.pad = curses.newpad(self.trueheight, self.truewidth + 1)
for i in range(self.trueheight - 1): for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i]) self.pad.addstr(i, 0, " " + self.values[i].value)
# Menu box # Menu box
self.menubox = curses.newpad(self.height, self.width) self.menubox = curses.newpad(self.height, self.width)
@ -35,16 +36,16 @@ class MenuDisplay:
# set a marker in front of the selected line # set a marker in front of the selected line
self.pad.addstr(position, 0, ">") self.pad.addstr(position, 0, ">")
def refresh(self, position: int) -> None: def refresh(self) -> None:
if self.height - 2 >= position - 1: if self.height - 2 >= self.menu.position - 1:
cornery = 0 cornery = 0
elif self.height - 2 >= self.trueheight - position: elif self.height - 2 >= self.trueheight - self.menu.position:
cornery = self.trueheight - self.height + 2 cornery = self.trueheight - self.height + 2
self.menubox.refresh(0, 0, self.toplefty, self.topleftx, self.menubox.refresh(0, 0, self.toplefty, self.topleftx,
self.height + self.toplefty, self.height + self.toplefty,
self.width + self.topleftx) self.width + self.topleftx)
self.update_pad(position) self.update_pad(self.menu.position)
self.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1, self.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1,
self.height - 2 + self.toplefty, self.height - 2 + self.toplefty,
self.width - 2 + self.topleftx) self.width - 2 + self.topleftx)

View File

@ -2,6 +2,7 @@ import sys
from typing import Any from typing import Any
from .display.display import Display from .display.display import Display
from .display.menudisplay import MenuDisplay
from .entities.player import Player from .entities.player import Player
from .interfaces import Map from .interfaces import Map
from .settings import Settings from .settings import Settings
@ -38,7 +39,7 @@ class Game:
self.player = Player() self.player = Player()
self.player.move(1, 6) self.player.move(1, 6)
self.m.add_entity(self.player) self.m.add_entity(self.player)
self.d = Display(self, screen) self.d = MenuDisplay(self, self.main_menu, 0, 0)
@staticmethod @staticmethod
def load_game(filename: str) -> None: def load_game(filename: str) -> None:
@ -49,7 +50,8 @@ class Game:
while True: while True:
screen.clear() screen.clear()
screen.refresh() screen.refresh()
self.d.display(self.player.y, self.player.x) # self.d.display(self.player.y, self.player.x)
self.d.refresh()
key = screen.getkey() key = screen.getkey()
self.handle_key_pressed(self.translate_key(key)) self.handle_key_pressed(self.translate_key(key))
@ -92,3 +94,4 @@ 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.d.refresh()

View File

@ -19,9 +19,9 @@ class Menu:
class MainMenuValues(Enum): class MainMenuValues(Enum):
START = auto() START = 'Jouer'
SETTINGS = auto() SETTINGS = 'Paramètres'
EXIT = auto() EXIT = 'Quitter'
class MainMenu(Menu): class MainMenu(Menu):