diff --git a/squirrelbattle/display/display_manager.py b/squirrelbattle/display/display_manager.py index 9d3b1dc..6680f3f 100644 --- a/squirrelbattle/display/display_manager.py +++ b/squirrelbattle/display/display_manager.py @@ -73,15 +73,13 @@ 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.playerinventorydisplay.refresh(self.rows // 10, - self.cols // 2, - 8 * self.rows // 10, - 2 * self.cols // 5) + 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) + self.storeinventorydisplay.refresh( + self.rows // 10, self.cols // 2, + 8 * self.rows // 10, 2 * self.cols // 5) elif self.game.state == GameMode.MAINMENU: self.mainmenudisplay.refresh(0, 0, self.rows, self.cols) elif self.game.state == GameMode.SETTINGS: diff --git a/squirrelbattle/display/menudisplay.py b/squirrelbattle/display/menudisplay.py index 3efd44c..58b7fdf 100644 --- a/squirrelbattle/display/menudisplay.py +++ b/squirrelbattle/display/menudisplay.py @@ -110,10 +110,11 @@ class MainMenuDisplay(Display): class InventoryDisplay(MenuDisplay): - message : str + message: str + def update_pad(self) -> None: - self.addstr(self.pad, 0, (self.width - len(self.message)) // 2, self.message, - curses.A_BOLD | curses.A_ITALIC) + self.addstr(self.pad, 0, (self.width - len(self.message)) // 2, + self.message, curses.A_BOLD | curses.A_ITALIC) for i, item in enumerate(self.menu.values): rep = self.pack[item.name.upper()] selection = f"[{rep}]" if i == self.menu.position else f" {rep} " @@ -128,8 +129,10 @@ class InventoryDisplay(MenuDisplay): def trueheight(self) -> int: return 2 + super().trueheight + class PlayerInventoryDisplay(InventoryDisplay): message = _("== INVENTORY ==") + class StoreInventoryDisplay(InventoryDisplay): message = _("== STALL ==") diff --git a/squirrelbattle/entities/friendly.py b/squirrelbattle/entities/friendly.py index 4c973d2..221707a 100644 --- a/squirrelbattle/entities/friendly.py +++ b/squirrelbattle/entities/friendly.py @@ -5,13 +5,13 @@ from .items import Item from random import choice -class Merchant(FriendlyEntity) : +class Merchant(FriendlyEntity): """ The class for merchants in the dungeon """ inventory = list hazel = int - + def keys(self) -> list: """ Returns a friendly entitie's specific attributes @@ -26,21 +26,21 @@ class Merchant(FriendlyEntity) : for i in range(5): 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, and allow the player to buy/sell objects """ # TODO return _("I don't sell any squirrel") - -class Sunflower(FriendlyEntity) : + + +class Sunflower(FriendlyEntity): """ A friendly sunflower """ dialogue_option = [_("Flower power!!"), _("The sun is warm today")] - + def __init__(self, maxhealth: int = 15, *args, **kwargs) -> None: - super().__init__(name="sunflower", - maxhealth=maxhealth, *args, **kwargs) + super().__init__(name="sunflower", maxhealth=maxhealth, *args, **kwargs) diff --git a/squirrelbattle/entities/items.py b/squirrelbattle/entities/items.py index f448c0d..1c41cf1 100644 --- a/squirrelbattle/entities/items.py +++ b/squirrelbattle/entities/items.py @@ -147,7 +147,8 @@ class Bomb(Item): d["exploding"] = self.exploding d["damage"] = self.damage return d - + + class Weapon(Item): """ Non-throwable items that improve player damage @@ -166,14 +167,15 @@ class Weapon(Item): d["damage"] = self.damage return d -class Sword(Weapon) : + +class Sword(Weapon): """ A basic weapon """ def __init__(self, name: str = "sword", *args, **kwargs): - super().__init__(name = name, *args, **kwargs) + super().__init__(name=name, *args, **kwargs) self.name = name - + class BodySnatchPotion(Item): """ diff --git a/squirrelbattle/entities/player.py b/squirrelbattle/entities/player.py index c344f15..dce6839 100644 --- a/squirrelbattle/entities/player.py +++ b/squirrelbattle/entities/player.py @@ -15,7 +15,7 @@ class Player(FightingEntity): max_xp: int = 10 inventory: list 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, strength: int = 5, intelligence: int = 1, charisma: int = 1, @@ -91,7 +91,7 @@ class Player(FightingEntity): elif entity.is_item(): entity.hold(self) 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)) if entity.dead: self.add_xp(randint(3, 7)) @@ -124,12 +124,12 @@ class Player(FightingEntity): queue.append((new_y, new_x)) self.paths = predecessors - def add_to_inventory(self, obj : Any) -> None : + def add_to_inventory(self, obj: Any) -> None: """ Adds an object to inventory """ self.inventory.append(obj) - + def save_state(self) -> dict: """ Saves the state of the entity into a dictionary diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 10780cf..39ddac1 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -117,30 +117,35 @@ class Game: self.state = GameMode.INVENTORY elif key == KeyValues.SPACE: self.state = GameMode.MAINMENU - elif key == KeyValues.T : - keykey = self.screen.getkey() - keykey = KeyValues.translate_key(keykey, self.settings) - if keykey == KeyValues.UP: - xp = self.player.x - yp = self.player.y-1 - elif keykey == KeyValues.DOWN: - xp = self.player.x - yp = self.player.y+1 - elif keykey == KeyValues.LEFT: - xp = self.player.x-1 - yp = self.player.y - elif keykey == KeyValues.RIGHT: - xp = self.player.x+1 - yp = self.player.y - 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) - self.logs.add_message(msg) - if entity.is_merchant() : - self.state = GameMode.STORE - self.store_menu.update_merchant(entity) - + elif key == KeyValues.T: + self.handle_friendly_entity_chat() + + def handle_friendly_entity_chat(self) -> None: + keykey = self.screen.getkey() + keykey = KeyValues.translate_key(keykey, self.settings) + if keykey == KeyValues.UP: + xp = self.player.x + yp = self.player.y - 1 + elif keykey == KeyValues.DOWN: + xp = self.player.x + yp = self.player.y + 1 + elif keykey == KeyValues.LEFT: + xp = self.player.x - 1 + yp = self.player.y + elif keykey == KeyValues.RIGHT: + xp = self.player.x + 1 + yp = self.player.y + else: + return + 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) + 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: """ @@ -164,12 +169,12 @@ class Game: self.inventory_menu.position = min(self.inventory_menu.position, len(self.inventory_menu.values) - 1) - + def handle_key_pressed_store(self, key: KeyValues) -> None: """ In a store menu, we can buy items or close the menu. """ - if key == KeyValues.SPACE : + if key == KeyValues.SPACE: self.state = GameMode.PLAY elif key == KeyValues.UP: self.store_menu.go_up() @@ -180,8 +185,7 @@ class Game: self.player.add_to_inventory(self.store_menu.validate()) # Ensure that the cursor has a good position self.store_menu.position = min(self.store_menu.position, - len(self.store_menu.values) - - 1) + len(self.store_menu.values) - 1) def handle_key_pressed_main_menu(self, key: KeyValues) -> None: """ diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 1c5e8b9..1984465 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -81,14 +81,15 @@ class Map: return 0 <= y < self.height and 0 <= x < self.width and \ self.tiles[y][x].can_walk() and \ not any(entity.x == x and entity.y == y for entity in self.entities) - + 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 \ - any(entity.x == x and entity.y == y and \ - entity.is_friendly() for entity in self.entities) + any(entity.x == x and entity.y == y and entity.is_friendly() + for entity in self.entities) @staticmethod def load(filename: str) -> "Map": @@ -348,9 +349,9 @@ class Entity: from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart 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,Merchant] + from squirrelbattle.entities.friendly import Merchant, Sunflower + return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear, + Sunflower, Tiger, Merchant] @staticmethod def get_all_entity_classes_in_a_dict() -> dict: @@ -360,7 +361,7 @@ class Entity: from squirrelbattle.entities.player import Player from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, \ TeddyBear - from squirrelbattle.entities.friendly import Merchant,Sunflower + from squirrelbattle.entities.friendly import Merchant, Sunflower from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart return { "Tiger": Tiger, @@ -462,17 +463,19 @@ class FightingEntity(Entity): d[name] = getattr(self, name) return d + class FriendlyEntity(FightingEntity): """ Friendly entities are living entities which do not attack the player """ - dialogue_option : list + dialogue_option: list - def talk_to(self, player : Any) -> str : - a = randint(0,len(self.dialogue_option)-1) - return "The "+self.name+" said : "+self.dialogue_option[a] + def talk_to(self, player: Any) -> str: + a = randint(0, len(self.dialogue_option) - 1) + 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 """ diff --git a/squirrelbattle/menus.py b/squirrelbattle/menus.py index 6404997..a826ecd 100644 --- a/squirrelbattle/menus.py +++ b/squirrelbattle/menus.py @@ -129,7 +129,8 @@ class InventoryMenu(Menu): def values(self) -> list: return self.player.inventory -class StoreMenu(Menu) : + +class StoreMenu(Menu): merchant: Merchant def update_merchant(self, merchant: Merchant) -> None: @@ -138,4 +139,3 @@ class StoreMenu(Menu) : @property def values(self) -> list: return self.merchant.inventory -