This commit is contained in:
Yohann D'ANELLO 2020-12-07 21:22:06 +01:00
parent b24cc1877f
commit 57fab7db51
8 changed files with 80 additions and 70 deletions

View File

@ -73,15 +73,13 @@ class DisplayManager:
self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5) self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5)
self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1) self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1)
if self.game.state == GameMode.INVENTORY: if self.game.state == GameMode.INVENTORY:
self.playerinventorydisplay.refresh(self.rows // 10, self.playerinventorydisplay.refresh(
self.cols // 2, self.rows // 10, self.cols // 2,
8 * self.rows // 10, 8 * self.rows // 10, 2 * self.cols // 5)
2 * self.cols // 5)
elif self.game.state == GameMode.STORE: elif self.game.state == GameMode.STORE:
self.storeinventorydisplay.refresh(self.rows // 10, self.storeinventorydisplay.refresh(
self.cols // 2, self.rows // 10, self.cols // 2,
8 * self.rows // 10, 8 * self.rows // 10, 2 * self.cols // 5)
2 * self.cols // 5)
elif self.game.state == GameMode.MAINMENU: elif self.game.state == GameMode.MAINMENU:
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols) self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
elif self.game.state == GameMode.SETTINGS: elif self.game.state == GameMode.SETTINGS:

View File

@ -110,10 +110,11 @@ class MainMenuDisplay(Display):
class InventoryDisplay(MenuDisplay): class InventoryDisplay(MenuDisplay):
message : str message: str
def update_pad(self) -> None: def update_pad(self) -> None:
self.addstr(self.pad, 0, (self.width - len(self.message)) // 2, self.message, self.addstr(self.pad, 0, (self.width - len(self.message)) // 2,
curses.A_BOLD | curses.A_ITALIC) self.message, curses.A_BOLD | curses.A_ITALIC)
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):
rep = self.pack[item.name.upper()] rep = self.pack[item.name.upper()]
selection = f"[{rep}]" if i == self.menu.position else f" {rep} " selection = f"[{rep}]" if i == self.menu.position else f" {rep} "
@ -128,8 +129,10 @@ class InventoryDisplay(MenuDisplay):
def trueheight(self) -> int: def trueheight(self) -> int:
return 2 + super().trueheight return 2 + super().trueheight
class PlayerInventoryDisplay(InventoryDisplay): class PlayerInventoryDisplay(InventoryDisplay):
message = _("== INVENTORY ==") message = _("== INVENTORY ==")
class StoreInventoryDisplay(InventoryDisplay): class StoreInventoryDisplay(InventoryDisplay):
message = _("== STALL ==") message = _("== STALL ==")

View File

@ -5,7 +5,7 @@ from .items import Item
from random import choice from random import choice
class Merchant(FriendlyEntity) : class Merchant(FriendlyEntity):
""" """
The class for merchants in the dungeon The class for merchants in the dungeon
""" """
@ -26,7 +26,7 @@ class Merchant(FriendlyEntity) :
for i in range(5): for i in range(5):
self.inventory.append(choice(Item.get_all_items())()) self.inventory.append(choice(Item.get_all_items())())
def talk_to(self, player : Player) -> str: def talk_to(self, player: Player) -> str:
""" """
This function is used to open the merchant's inventory in a menu, This function is used to open the merchant's inventory in a menu,
and allow the player to buy/sell objects and allow the player to buy/sell objects
@ -34,7 +34,8 @@ class Merchant(FriendlyEntity) :
# TODO # TODO
return _("I don't sell any squirrel") return _("I don't sell any squirrel")
class Sunflower(FriendlyEntity) :
class Sunflower(FriendlyEntity):
""" """
A friendly sunflower A friendly sunflower
""" """
@ -42,5 +43,4 @@ class Sunflower(FriendlyEntity) :
def __init__(self, maxhealth: int = 15, def __init__(self, maxhealth: int = 15,
*args, **kwargs) -> None: *args, **kwargs) -> None:
super().__init__(name="sunflower", super().__init__(name="sunflower", maxhealth=maxhealth, *args, **kwargs)
maxhealth=maxhealth, *args, **kwargs)

View File

@ -148,6 +148,7 @@ class Bomb(Item):
d["damage"] = self.damage d["damage"] = self.damage
return d return d
class Weapon(Item): class Weapon(Item):
""" """
Non-throwable items that improve player damage Non-throwable items that improve player damage
@ -166,12 +167,13 @@ class Weapon(Item):
d["damage"] = self.damage d["damage"] = self.damage
return d return d
class Sword(Weapon) :
class Sword(Weapon):
""" """
A basic weapon A basic weapon
""" """
def __init__(self, name: str = "sword", *args, **kwargs): def __init__(self, name: str = "sword", *args, **kwargs):
super().__init__(name = name, *args, **kwargs) super().__init__(name=name, *args, **kwargs)
self.name = name self.name = name

View File

@ -15,7 +15,7 @@ class Player(FightingEntity):
max_xp: int = 10 max_xp: int = 10
inventory: list inventory: list
paths: Dict[Tuple[int, int], Tuple[int, int]] paths: Dict[Tuple[int, int], Tuple[int, int]]
hazel: int #It is the currency of this game hazel: int # It is the currency of this game
def __init__(self, name: str = "player", maxhealth: int = 20, def __init__(self, name: str = "player", maxhealth: int = 20,
strength: int = 5, intelligence: int = 1, charisma: int = 1, strength: int = 5, intelligence: int = 1, charisma: int = 1,
@ -91,7 +91,7 @@ class Player(FightingEntity):
elif entity.is_item(): elif entity.is_item():
entity.hold(self) entity.hold(self)
elif entity.is_friendly(): elif entity.is_friendly():
# self.map.logs.add_message(entity.talk_to(self)) # self.map.logs.add_message(entity.talk_to(self))
self.map.logs.add_message(self.hit(entity)) self.map.logs.add_message(self.hit(entity))
if entity.dead: if entity.dead:
self.add_xp(randint(3, 7)) self.add_xp(randint(3, 7))
@ -124,7 +124,7 @@ class Player(FightingEntity):
queue.append((new_y, new_x)) queue.append((new_y, new_x))
self.paths = predecessors self.paths = predecessors
def add_to_inventory(self, obj : Any) -> None : def add_to_inventory(self, obj: Any) -> None:
""" """
Adds an object to inventory Adds an object to inventory
""" """

View File

@ -117,31 +117,36 @@ class Game:
self.state = GameMode.INVENTORY self.state = GameMode.INVENTORY
elif key == KeyValues.SPACE: elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
elif key == KeyValues.T : elif key == KeyValues.T:
self.handle_friendly_entity_chat()
def handle_friendly_entity_chat(self) -> None:
keykey = self.screen.getkey() keykey = self.screen.getkey()
keykey = KeyValues.translate_key(keykey, self.settings) keykey = KeyValues.translate_key(keykey, self.settings)
if keykey == KeyValues.UP: if keykey == KeyValues.UP:
xp = self.player.x xp = self.player.x
yp = self.player.y-1 yp = self.player.y - 1
elif keykey == KeyValues.DOWN: elif keykey == KeyValues.DOWN:
xp = self.player.x xp = self.player.x
yp = self.player.y+1 yp = self.player.y + 1
elif keykey == KeyValues.LEFT: elif keykey == KeyValues.LEFT:
xp = self.player.x-1 xp = self.player.x - 1
yp = self.player.y yp = self.player.y
elif keykey == KeyValues.RIGHT: elif keykey == KeyValues.RIGHT:
xp = self.player.x+1 xp = self.player.x + 1
yp = self.player.y yp = self.player.y
if self.map.entity_is_present(yp, xp) : else:
for entity in self.map.entities : return
if entity.is_friendly() and entity.x == xp and entity.y == yp : if self.map.entity_is_present(yp, xp):
for entity in self.map.entities:
if entity.is_friendly() and entity.x == xp and \
entity.y == yp:
msg = entity.talk_to(self.player) msg = entity.talk_to(self.player)
self.logs.add_message(msg) self.logs.add_message(msg)
if entity.is_merchant() : if entity.is_merchant():
self.state = GameMode.STORE self.state = GameMode.STORE
self.store_menu.update_merchant(entity) self.store_menu.update_merchant(entity)
def handle_key_pressed_inventory(self, key: KeyValues) -> None: def handle_key_pressed_inventory(self, key: KeyValues) -> None:
""" """
In the inventory menu, we can interact with items or close the menu. In the inventory menu, we can interact with items or close the menu.
@ -169,7 +174,7 @@ class Game:
""" """
In a store menu, we can buy items or close the menu. In a store menu, we can buy items or close the menu.
""" """
if key == KeyValues.SPACE : if key == KeyValues.SPACE:
self.state = GameMode.PLAY self.state = GameMode.PLAY
elif key == KeyValues.UP: elif key == KeyValues.UP:
self.store_menu.go_up() self.store_menu.go_up()
@ -180,8 +185,7 @@ class Game:
self.player.add_to_inventory(self.store_menu.validate()) self.player.add_to_inventory(self.store_menu.validate())
# Ensure that the cursor has a good position # Ensure that the cursor has a good position
self.store_menu.position = min(self.store_menu.position, self.store_menu.position = min(self.store_menu.position,
len(self.store_menu.values) len(self.store_menu.values) - 1)
- 1)
def handle_key_pressed_main_menu(self, key: KeyValues) -> None: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
""" """

View File

@ -84,11 +84,12 @@ class Map:
def entity_is_present(self, y: int, x: int) -> bool: def entity_is_present(self, y: int, x: int) -> bool:
""" """
Indicates that the tile at the coordinates (y, x) contains a killable entity Indicates that the tile at the coordinates (y, x) contains a killable
entity
""" """
return 0 <= y < self.height and 0 <= x < self.width and \ return 0 <= y < self.height and 0 <= x < self.width and \
any(entity.x == x and entity.y == y and \ any(entity.x == x and entity.y == y and entity.is_friendly()
entity.is_friendly() for entity in self.entities) for entity in self.entities)
@staticmethod @staticmethod
def load(filename: str) -> "Map": def load(filename: str) -> "Map":
@ -348,9 +349,9 @@ class Entity:
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \ from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
Rabbit, TeddyBear Rabbit, TeddyBear
from squirrelbattle.entities.friendly import Merchant,Sunflower from squirrelbattle.entities.friendly import Merchant, Sunflower
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear, \ return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear,
Sunflower,Tiger,Merchant] Sunflower, Tiger, Merchant]
@staticmethod @staticmethod
def get_all_entity_classes_in_a_dict() -> dict: def get_all_entity_classes_in_a_dict() -> dict:
@ -360,7 +361,7 @@ class Entity:
from squirrelbattle.entities.player import Player from squirrelbattle.entities.player import Player
from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \ from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \
TeddyBear TeddyBear
from squirrelbattle.entities.friendly import Merchant,Sunflower from squirrelbattle.entities.friendly import Merchant, Sunflower
from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart
return { return {
"Tiger": Tiger, "Tiger": Tiger,
@ -462,17 +463,19 @@ class FightingEntity(Entity):
d[name] = getattr(self, name) d[name] = getattr(self, name)
return d return d
class FriendlyEntity(FightingEntity): class FriendlyEntity(FightingEntity):
""" """
Friendly entities are living entities which do not attack the player Friendly entities are living entities which do not attack the player
""" """
dialogue_option : list dialogue_option: list
def talk_to(self, player : Any) -> str : def talk_to(self, player: Any) -> str:
a = randint(0,len(self.dialogue_option)-1) a = randint(0, len(self.dialogue_option) - 1)
return "The "+self.name+" said : "+self.dialogue_option[a] return "The " + self.translated_name \
+ " said : " + self.dialogue_option[a]
def keys(self) -> list : def keys(self) -> list:
""" """
Returns a friendly entity's specific attributes Returns a friendly entity's specific attributes
""" """

View File

@ -129,7 +129,8 @@ class InventoryMenu(Menu):
def values(self) -> list: def values(self) -> list:
return self.player.inventory return self.player.inventory
class StoreMenu(Menu) :
class StoreMenu(Menu):
merchant: Merchant merchant: Merchant
def update_merchant(self, merchant: Merchant) -> None: def update_merchant(self, merchant: Merchant) -> None:
@ -138,4 +139,3 @@ class StoreMenu(Menu) :
@property @property
def values(self) -> list: def values(self) -> list:
return self.merchant.inventory return self.merchant.inventory