Added main menu when launching the game, and fixed menus

This commit is contained in:
Nicolas Margulies 2020-11-10 19:40:59 +01:00
parent 17530f386c
commit 1a1b906c8e
5 changed files with 43 additions and 27 deletions

View File

@ -1,9 +1,10 @@
import curses import curses
from dungeonbattle.display.mapdisplay import MapDisplay from dungeonbattle.display.mapdisplay import MapDisplay
from dungeonbattle.display.statsdisplay import StatsDisplay from dungeonbattle.display.statsdisplay import StatsDisplay
from dungeonbattle.display.menudisplay import MainMenuDisplay
from dungeonbattle.display.texturepack import TexturePack from dungeonbattle.display.texturepack import TexturePack
from typing import Any from typing import Any
from dungeonbattle.game import Game from dungeonbattle.game import Game, GameMode
class DisplayManager: class DisplayManager:
@ -11,9 +12,11 @@ class DisplayManager:
def __init__(self, screen: Any, g: Game): def __init__(self, screen: Any, g: Game):
self.game = g self.game = g
self.screen = screen self.screen = screen
self.mapdisplay = MapDisplay(screen, self.game.settings.TEXTURE_PACK) pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.statsdisplay = StatsDisplay(screen, self.game.settings.TEXTURE_PACK) self.mapdisplay = MapDisplay(screen, pack)
self.displays = [self.statsdisplay, self.mapdisplay] self.statsdisplay = StatsDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu, screen, pack)
self.displays = [self.statsdisplay, self.mapdisplay, self.mainmenudisplay]
self.update_game_components() self.update_game_components()
def update_game_components(self): def update_game_components(self):
@ -23,8 +26,12 @@ class DisplayManager:
self.statsdisplay.update_player(self.game.player) self.statsdisplay.update_player(self.game.player)
def refresh(self) -> None: def refresh(self) -> None:
if self.game.state == GameMode.PLAY:
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.cols) self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.cols)
self.statsdisplay.refresh(self.rows*4//5, 0, self.rows//5, self.cols) self.statsdisplay.refresh(self.rows*4//5, 0, self.rows//5, self.cols)
if self.game.state == GameMode.MAINMENU:
self.mainmenudisplay.refresh(0,0,self.rows, self.cols)
# self.menudisplay.refresh(self.position) # self.menudisplay.refresh(self.position)
def ensure_resized(self, *pads) -> bool: def ensure_resized(self, *pads) -> bool:

View File

@ -8,22 +8,22 @@ class MenuDisplay(Display):
def __init__(self, *args) : def __init__(self, *args) :
super().__init__(*args) super().__init__(*args)
self.menubox = self.newpad(self.height,self.width) self.menubox = self.newpad(self.rows,self.cols)
def update_menu(self, menu: Menu): def update_menu(self, menu: Menu):
self.menu = menu self.menu = menu
self.values = menu.values self.values = [ str(a) for a in menu.values ]
self.trueheight = len(menu.values) self.trueheight = len(self.values)
self.truewidth = max(map(len,self.values)) self.truewidth = max([len(a) for a in self.values])
#Menu values are printed in pad #Menu values are printed in pad
self.pad = self.newpad(self.trueheight,self.truewidth+1) self.pad = self.newpad(self.trueheight,self.truewidth+2)
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])
def update_pad(self) -> None: def update_pad(self) -> None:
for i in range(self.trueheight) : for i in range(self.trueheight):
self.pad.addstr(i,0," ") self.pad.addstr(i,0," ")
self.pad.addstr(self.menu.position,0,">") #set a marker on the selected line self.pad.addstr(self.menu.position,0,">") #set a marker on the selected line
@ -36,17 +36,25 @@ class MenuDisplay(Display):
#Menu box #Menu box
self.menubox.addstr(0,0,""+""*(self.width-2)+"") self.menubox.addstr(0,0,""+""*(self.width-2)+"")
for i in range(1,self.height-2) : for i in range(1,self.height-1) :
self.menubox.addstr(i,0,""+" "*(self.width-2)+"") self.menubox.addstr(i,0,""+" "*(self.width-2)+"")
self.menubox.addstr(self.height-2, 0, ""+""*(self.width-2)+"") self.menubox.addstr(self.height-1, 0, ""+""*(self.width-2)+"")
self.menubox.refresh(0, 0, self.y, self.x, self.menubox.refresh(0, 0, self.y, self.x,
self.height + self.y, self.height + self.y,
self.width + self.x) self.width + self.x)
self.update_pad() self.update_pad()
self.pad.refresh(cornery, 0, self.y+1, self.x+1, self.pad.refresh(cornery, 0, self.y+1, self.x+2,
self.height-2 + self.y, self.height-1 + self.y,
self.width-2 + self.x) self.width-1 + self.x)
@property
def preferred_width(self) -> int:
return self.truewidth + 6
@property
def preferred_height(self) -> int:
return self.trueheight + 2
class MainMenuDisplay(Display): class MainMenuDisplay(Display):
def __init__(self, menu : MainMenu, *args) : def __init__(self, menu : MainMenu, *args) :
@ -54,17 +62,18 @@ class MainMenuDisplay(Display):
self.menu = menu self.menu = menu
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
with open("ascii_art.txt", "r") as file: with open("resources/ascii_art.txt", "r") as file:
self.title = file.read().split("\n") self.title = file.read().split("\n")
self.menudisplay = MenuDisplay(self.screen) self.menudisplay = MenuDisplay(self.screen, self.pack)
self.menudisplay.update_menu(self.menu) self.menudisplay.update_menu(self.menu)
def dislpay(self) -> None: def display(self) -> None:
for i in range(len(self.title)) : for i in range(len(self.title)) :
self.pad.addstr(4+i,self.width//2-len(self.title[0])//2-1,self.title[i]) self.pad.addstr(4+i,self.width//2-len(self.title[0])//2-1,self.title[i])
self.pad.refresh(0,0,self.y,self.x,self.width,self.height) self.pad.refresh(0,0,self.y,self.x,self.height,self.width)
menuy, menux = len(self.title)+8, self.width//2-15//2-1 menuwidth = min(self.menudisplay.preferred_width, self.width)
self.menudisplay.refresh(menuy, menux, min(15, self.rows-menuy), min(15,self.cols-menux)) menuy, menux = len(self.title)+8, self.width//2-menuwidth//2-1
self.menudisplay.refresh(menuy, menux, min(self.menudisplay.preferred_height, self.height-menuy), menuwidth)

View File

@ -35,7 +35,7 @@ class Game:
""" """
Init the game. Init the game.
""" """
self.state = GameMode.PLAY self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu() self.main_menu = menus.MainMenu()
self.settings = Settings() self.settings = Settings()
self.settings.load_settings() self.settings.load_settings()
@ -46,7 +46,7 @@ class Game:
Create a new game on the screen. Create a new game on the screen.
""" """
# TODO generate a new map procedurally # TODO generate a new map procedurally
self.map = Map.load("example_map.txt") self.map = Map.load("resources/example_map.txt")
self.map.currenty = 1 self.map.currenty = 1
self.map.currentx = 6 self.map.currentx = 6
self.player = Player() self.player = Player()