From 1cb78c4b8a04d825d1cb506bd917e6330998bd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Tue, 19 Apr 2022 12:36:05 +0200 Subject: [PATCH] Work on equipping items --- __main__.py | 5 +++-- games/example.yml | 7 +++++++ lib/fight.py | 41 ++++++++++++++++++++++++++++++++++++++++- lib/game.py | 47 +++++++++++++++++++++++++++++++++++++---------- lib/lang/en.yml | 3 +++ 5 files changed, 90 insertions(+), 13 deletions(-) diff --git a/__main__.py b/__main__.py index 57a9151..ba64666 100644 --- a/__main__.py +++ b/__main__.py @@ -33,7 +33,7 @@ def main(): g = load(f"./games/{file}",l) games.append(g) except Exception as e: - print(f"{Back.RED}{Fore.RED}{l['error_loading']} {file}:") + print(f"{Back.RED}{Fore.WHITE}{l['error_loading']} {file}{Fore.RESET}{Back.RESET}:") print(e) # PRINT OUT GAME SELECT @@ -42,7 +42,8 @@ def main(): else: names = [] for n in games: - names.append(n.name) + if(n is not None): + names.append(n.name) m = MenuManager(names,f" TEXTY \n{l['available']}") games[m.selected].main_menu() diff --git a/games/example.yml b/games/example.yml index 8728446..5d8ef9d 100644 --- a/games/example.yml +++ b/games/example.yml @@ -2,6 +2,13 @@ meta: # make sure every key is in lowercase name: "My Text Adventure" # Game name id: "examplegame" # An original game ID (please try to be original) creator: "hernik" # Your name as a creator + equippable: + - iron_sword: + atk: 3 + starter: true + - chainmail: + def: 2 + starter: true game: # here goes all the game logic start: # the starting point always HAS to be named "start" (lowercase), the order and name of the rest does not matter diff --git a/lib/fight.py b/lib/fight.py index c1a198d..7852d17 100644 --- a/lib/fight.py +++ b/lib/fight.py @@ -1,21 +1,60 @@ import math + +from lib.game import Item from .ascii import * +from colorama import Fore +import keyboard class FightHandler: - def __init__(self,name:str,hp:int,attacks:list,img:str="") -> None: + def __init__(self,message:str,name:str,hp:int,attacks:list,lang:dict,eq:Item,img:str="") -> None: + self.selected = 0 + keyboard.add_hotkey("up",self.up) + keyboard.add_hotkey("down",self.down) + keyboard.add_hotkey("enter",self.attack) self.name = name self.max = hp # starting HP self.hp = self.max # current HP self.attacks = attacks self.img = img + self.lang = lang + self.message = message + self.equipped = eq + input() + + def up(self): + if self.selected == 0: + self.selected = 2 + else: + self.selected -= 1 + system("cls||clear") + self.show() + + def down(self): + if self.selected == 2: + self.selected = 0 + else: + self.selected += 1 + system("cls||clear") + self.show() def show(self): p = math.trunc(self.hp/self.max*10) h = "🟥"*p if str(p).endswith(".5"): h += "◾" + print(self.message) print(f"{self.name} {h} {self.hp}/{self.max}") if self.img != "": anim = AsciiAnimation() anim.load_ascii(self.img) anim.play() + s = [self.lang["attack"],self.lang["defend"],self.lang["inventory"]] + for selection in s: + if(self.selected == self.selections.index(selection)): + print(f"{Fore.RED}⚔{Fore.RESET} {selection}") + else: + print(f" {selection}") + + def attack(self): + self.hp + input() diff --git a/lib/game.py b/lib/game.py index fc6e591..4572e2c 100644 --- a/lib/game.py +++ b/lib/game.py @@ -11,13 +11,30 @@ from os import system class Game: # the game class keeps information about the loaded game def __init__(self,data:dict): - self.name = data["meta"]["name"] - self.author = data["meta"]["creator"] - self.current = "start" - self.nodes = {} - self.inventory = [] - self.id = data["meta"]["id"] - self.save = SaveManager(self.id) + self.name = data["meta"]["name"] # Game name + self.author = data["meta"]["creator"] # Game creator + self.current = "start" # Current prompt + self.nodes = {} # All nodes + self.inventory = [] # Player's inventory + self.id = data["meta"]["id"] # Game ID + self.save = SaveManager(self.id) # saving + self.equipped = {"weapon":None,"armor":None} # Items equipped by player + if "equippable" in data["meta"].keys(): + self.equippable = [] # Items that can be equipped by player + for item in data["meta"]["equippable"]: + name = list(item.keys())[0] + if "def" in item[name].keys() and "atk" in item[name].keys(): + self.equippable.append(Item(name,item[name]["atk"],item[name]["def"])) + elif "def" in item[name].keys(): + self.equippable.append(Item(name=name,defense=item[name]["def"])) + elif "atk" in item[name].keys(): + self.equippable.append(Item(name,item[name]["atk"])) + if("starter" in item[name].keys()): # if starter, equip and add to inventory + if item[name]["starter"]: + i = next((x for x in self.equippable if x.name == list(item.keys())[0])) + self.inventory.append(i) + self.equipped[i.type] = i + for k in data["game"]: self.nodes.update({k:data["game"][k]}) @@ -128,7 +145,7 @@ class Game: # the game class keeps information about the loaded game def show_inventory(self): if len(self.inventory) == 0: - MenuManager([self.lang["return"]],f" YOUR INVENTORY \n") + MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n") else: s = "" for i,item in enumerate(self.inventory): @@ -136,7 +153,7 @@ class Game: # the game class keeps information about the loaded game s += f"- {item}" else: s += f"- {item}\n" - MenuManager([self.lang["return"]],f" YOUR INVENTORY \n{s}") + MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n{s}") self.print_text() def print_animated(self,animid): # prints the first found occurence of an ascii animation @@ -158,6 +175,16 @@ def load(file_path,lang): # starts to load the game from YAML g.lang = lang return g except Exception as e: - print(f"{Back.RED}{Fore.WHITE}{g.lang['error_loading']}{Fore.RESET}{Back.RESET}") + print(f"{Back.RED}{Fore.WHITE}ERROR{Fore.RESET}{Back.RESET}") print(e) return None + +class Item: + def __init__(self,name:str,attack:int = 0,defense:int = 0) -> None: + self.name = name + if attack == 0 and defense > 0: + self.type = "armor" + else: + self.type = "weapon" + self.attack = attack + self.defense = defense \ No newline at end of file diff --git a/lib/lang/en.yml b/lib/lang/en.yml index faa1529..7646e0d 100644 --- a/lib/lang/en.yml +++ b/lib/lang/en.yml @@ -24,5 +24,8 @@ selection: 'Make a selection (number):' not_number: 'Not a number selection' invalid: 'Invalid selection' +attack: "Attack" +defend: "Defend" + error_loading: 'An exception has occured while loading the game from the YAML file' no_action: 'Error: No action "$action" found in the game file.' \ No newline at end of file