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

View File

@ -8,22 +8,22 @@ class MenuDisplay(Display):
def __init__(self, *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):
self.menu = menu
self.values = menu.values
self.trueheight = len(menu.values)
self.truewidth = max(map(len,self.values))
self.values = [ str(a) for a in menu.values ]
self.trueheight = len(self.values)
self.truewidth = max([len(a) for a in self.values])
#Menu values are printed in pad
self.pad = self.newpad(self.trueheight,self.truewidth+1)
for i in range(self.trueheight-1) :
self.pad.addstr(i,0," " + self.values[i])
self.pad = self.newpad(self.trueheight,self.truewidth+2)
for i in range(self.trueheight) :
self.pad.addstr(i,0," " + self.values[i])
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(self.menu.position,0,">") #set a marker on the selected line
@ -36,17 +36,25 @@ class MenuDisplay(Display):
#Menu box
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(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.height + self.y,
self.width + self.x)
self.update_pad()
self.pad.refresh(cornery, 0, self.y+1, self.x+1,
self.height-2 + self.y,
self.width-2 + self.x)
self.pad.refresh(cornery, 0, self.y+1, self.x+2,
self.height-1 + self.y,
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):
def __init__(self, menu : MainMenu, *args) :
@ -54,17 +62,18 @@ class MainMenuDisplay(Display):
self.menu = menu
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.menudisplay = MenuDisplay(self.screen)
self.menudisplay = MenuDisplay(self.screen, self.pack)
self.menudisplay.update_menu(self.menu)
def dislpay(self) -> None:
def display(self) -> None:
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.refresh(0,0,self.y,self.x,self.width,self.height)
menuy, menux = len(self.title)+8, self.width//2-15//2-1
self.menudisplay.refresh(menuy, menux, min(15, self.rows-menuy), min(15,self.cols-menux))
self.pad.refresh(0,0,self.y,self.x,self.height,self.width)
menuwidth = min(self.menudisplay.preferred_width, self.width)
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.
"""
self.state = GameMode.PLAY
self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu()
self.settings = Settings()
self.settings.load_settings()
@ -46,7 +46,7 @@ class Game:
Create a new game on the screen.
"""
# 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.currentx = 6
self.player = Player()