Work in progress on printing a store menu. Its buggy though

This commit is contained in:
eichhornchen 2020-12-07 20:54:53 +01:00
parent d39427f978
commit 8540a8f354
7 changed files with 68 additions and 34 deletions

View File

@ -7,7 +7,7 @@ from squirrelbattle.display.mapdisplay import MapDisplay
from squirrelbattle.display.messagedisplay import MessageDisplay
from squirrelbattle.display.statsdisplay import StatsDisplay
from squirrelbattle.display.menudisplay import MainMenuDisplay, \
InventoryDisplay, SettingsMenuDisplay
PlayerInventoryDisplay, StoreInventoryDisplay, SettingsMenuDisplay
from squirrelbattle.display.logsdisplay import LogsDisplay
from squirrelbattle.display.texturepack import TexturePack
from typing import Any
@ -24,7 +24,8 @@ class DisplayManager:
self.mapdisplay = MapDisplay(screen, pack)
self.statsdisplay = StatsDisplay(screen, pack)
self.logsdisplay = LogsDisplay(screen, pack)
self.inventorydisplay = InventoryDisplay(screen, pack)
self.playerinventorydisplay = PlayerInventoryDisplay(screen, pack)
self.storeinventorydisplay = StoreInventoryDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
screen, pack)
self.settingsmenudisplay = SettingsMenuDisplay(screen, pack)
@ -47,14 +48,18 @@ class DisplayManager:
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay.update_map(self.game.map)
self.statsdisplay.update_player(self.game.player)
self.inventorydisplay.update_menu(self.game.inventory_menu)
self.game.inventory_menu.update_player(self.game.player)
self.game.store_menu.update_merchant(self.game.player)
self.playerinventorydisplay.update_menu(self.game.inventory_menu)
self.storeinventorydisplay.update_menu(self.game.store_menu)
self.settingsmenudisplay.update_menu(self.game.settings_menu)
self.logsdisplay.update_logs(self.game.logs)
self.messagedisplay.update_message(self.game.message)
def refresh(self) -> None:
if self.game.state == GameMode.PLAY \
or self.game.state == GameMode.INVENTORY:
or self.game.state == GameMode.INVENTORY \
or self.game.state == GameMode.STORE :
# The map pad has already the good size
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5,
self.mapdisplay.pack.tile_width
@ -68,7 +73,12 @@ class DisplayManager:
self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5)
self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1)
if self.game.state == GameMode.INVENTORY:
self.inventorydisplay.refresh(self.rows // 10,
self.playerinventorydisplay.refresh(self.rows // 10,
self.cols // 2,
8 * self.rows // 10,
2 * self.cols // 5)
elif self.game.state == GameMode.STORE:
self.storeinventorydisplay.refresh(self.rows // 10,
self.cols // 2,
8 * self.rows // 10,
2 * self.cols // 5)

View File

@ -110,8 +110,8 @@ class MainMenuDisplay(Display):
class InventoryDisplay(MenuDisplay):
message : str
def update_pad(self) -> None:
message = _("== INVENTORY ==")
self.addstr(self.pad, 0, (self.width - len(message)) // 2, message,
curses.A_BOLD | curses.A_ITALIC)
for i, item in enumerate(self.menu.values):
@ -127,3 +127,9 @@ class InventoryDisplay(MenuDisplay):
@property
def trueheight(self) -> int:
return 2 + super().trueheight
class PlayerInventoryDisplay(InventoryDisplay):
message = _("== INVENTORY ==")
class StoreInventoryDisplay(InventoryDisplay):
message = _("== STALL ==")

View File

@ -1,13 +1,14 @@
from ..interfaces import FriendlyEntity
from ..translations import gettext as _
from .player import Player
from .items import Item
from random import choice
class Merchant(FriendlyEntity) :
"""
The class for merchants in the dungeon
"""
name = "Merchant"
inventory = list
hazel = int
@ -17,10 +18,13 @@ class Merchant(FriendlyEntity) :
"""
return ["maxhealth", "health", "inventory", "hazel"]
def __init__(self, inventory : list, hazel : int = 75):
super().__init__()
self.inventory = inventory
def __init__(self, name : str = "merchant", hazel : int = 75):
super().__init__(name = name)
self.hazel = hazel
self.name = name
self.inventory = []
for i in range(5) :
self.inventory.append(choice(Item.get_all_items())())
def talk_to(self, player : Player) -> str:
"""

View File

@ -60,6 +60,9 @@ class Item(Entity):
d["held"] = self.held
return d
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Weapon, Sword]
class Heart(Item):
"""
@ -166,9 +169,9 @@ class Sword(Weapon) :
"""
A basic weapon
"""
def __init__(self, name: int, *args, **kwargs):
super().__init__(*args, **kwargs)
self.name = "sword"
def __init__(self, name: str = "sword", *args, **kwargs):
super().__init__(name = name, *args, **kwargs)
self.name = name
class BodySnatchPotion(Item):

View File

@ -41,6 +41,7 @@ class Game:
self.settings_menu = menus.SettingsMenu()
self.settings_menu.update_values(self.settings)
self.inventory_menu = menus.InventoryMenu()
self.store_menu = menus.StoreMenu()
self.logs = Logs()
self.message = None
@ -92,6 +93,8 @@ class Game:
self.handle_key_pressed_main_menu(key)
elif self.state == GameMode.SETTINGS:
self.settings_menu.handle_key_pressed(key, raw_key, self)
elif self.state == GameMode.STORE:
self.handle_key_pressed_store(key)
self.display_actions(DisplayActions.REFRESH)
def handle_key_pressed_play(self, key: KeyValues) -> None:
@ -138,6 +141,7 @@ class Game:
self.logs.add_message(msg)
if entity.is_merchant() :
self.state = GameMode.STORE
self.store_menu.update_merchant(entity)
def handle_key_pressed_inventory(self, key: KeyValues) -> None:
@ -163,27 +167,22 @@ class Game:
len(self.inventory_menu.values)
- 1)
def handle_key_pressed_inventory(self, key: KeyValues) -> None:
def handle_key_pressed_store(self, key: KeyValues) -> None:
"""
In the inventory menu, we can interact with items or close the menu.
In a store menu, we can buy items or close the menu.
"""
if key == KeyValues.SPACE or key == KeyValues.INVENTORY:
if key == KeyValues.SPACE :
self.state = GameMode.PLAY
elif key == KeyValues.UP:
self.inventory_menu.go_up()
self.store_menu.go_up()
elif key == KeyValues.DOWN:
self.inventory_menu.go_down()
if self.inventory_menu.values and not self.player.dead:
if key == KeyValues.USE:
self.inventory_menu.validate().use()
elif key == KeyValues.EQUIP:
self.inventory_menu.validate().equip()
elif key == KeyValues.DROP:
self.inventory_menu.validate().drop()
self.store_menu.go_down()
if self.store_menu.values and not self.player.dead:
if key == KeyValues.ENTER:
self.player.add_to_inventory(self.store_menu.validate())
# Ensure that the cursor has a good position
self.inventory_menu.position = min(self.inventory_menu.position,
len(self.inventory_menu.values)
self.store_menu.position = min(self.store_menu.position,
len(self.store_menu.values)
- 1)
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:

View File

@ -135,7 +135,7 @@ class Map:
def spawn_random_entities(self, count: int) -> None:
"""
Put randomly {count} hedgehogs on the map, where it is available.
Put randomly {count} entities on the map, where it is available.
"""
for ignored in range(count):
y, x = 0, 0
@ -333,6 +333,7 @@ class Entity:
"""
Is this entity a merchant?
"""
from squirrelbattle.entities.friendly import Merchant
return isinstance(self, Merchant)
@property
@ -340,7 +341,7 @@ class Entity:
return _(self.name.replace("_", " "))
@staticmethod
def get_all_entity_classes():
def get_all_entity_classes() -> list:
"""
Returns all entities subclasses
"""
@ -348,9 +349,8 @@ class Entity:
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
Rabbit, TeddyBear
from squirrelbattle.entities.friendly import Merchant,Sunflower
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear,Sunflower,Tiger]
return [BodySnatchPotion, Bomb, Heart, Hedgehog,
Rabbit, TeddyBear, Tiger]
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear, \
Sunflower,Tiger,Merchant]
@staticmethod
def get_all_entity_classes_in_a_dict() -> dict:

View File

@ -6,6 +6,7 @@ from typing import Any, Optional
from .display.texturepack import TexturePack
from .entities.player import Player
from .entities.friendly import Merchant
from .enums import GameMode, KeyValues, DisplayActions
from .settings import Settings
from .translations import gettext as _, Translator
@ -127,3 +128,14 @@ class InventoryMenu(Menu):
@property
def values(self) -> list:
return self.player.inventory
class StoreMenu(Menu) :
merchant: Merchant
def update_merchant(self, merchant: Merchant) -> None:
self.merchant = merchant
@property
def values(self) -> list:
return self.merchant.inventory