Komentáře

This commit is contained in:
Matyáš Caras 2022-05-04 14:26:32 +02:00 committed by GitHub
parent 7dd83c7ca0
commit 4696cfc621
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 113 additions and 108 deletions

View file

@ -1,15 +1,12 @@
![spaghetti code](https://img.shields.io/badge/spaghetti%20code-certified-success) [![wakatime](https://wakatime.com/badge/user/17178fab-a33c-430f-a764-7b3f26c7b966/project/cd3b8d2e-460d-4a8f-952b-dc3c1b869158.svg)](https://wakatime.com/badge/user/17178fab-a33c-430f-a764-7b3f26c7b966/project/cd3b8d2e-460d-4a8f-952b-dc3c1b869158) [![CodeFactor](https://www.codefactor.io/repository/github/hernikplays/texty/badge)](https://www.codefactor.io/repository/github/hernikplays/texty) ![spaghetti code](https://img.shields.io/badge/spaghetti%20code-certified-success) [![wakatime](https://wakatime.com/badge/user/17178fab-a33c-430f-a764-7b3f26c7b966/project/cd3b8d2e-460d-4a8f-952b-dc3c1b869158.svg)](https://wakatime.com/badge/user/17178fab-a33c-430f-a764-7b3f26c7b966/project/cd3b8d2e-460d-4a8f-952b-dc3c1b869158) [![CodeFactor](https://www.codefactor.io/repository/github/hernikplays/texty/badge)](https://www.codefactor.io/repository/github/hernikplays/texty)
# texty <div align="center">
An extremely simple text-adventure game ~~engine(?)~~ player <h1>texty</h1>
## What is this? "Jednoduchý" přehrávač textových RPG her
I was bored </div>
## How do I make a game ## Co to je
Check out the `example.yml` file, all games are in the `YAML` format. Ročníková práce
## How do I use this ## Jak se s tím pracuje
Download/clone the repo and run the `__main__.py` file. You need to have a valid game in a `games` folder. TODO
## TODO
[Here](https://github.com/hernikplays/texty/projects/1)

View file

@ -13,16 +13,16 @@ class AsciiAnimation:
Loads art from .asc file Loads art from .asc file
""" """
with TemporaryDirectory() as tmpdir: # we enter a temporary directory with TemporaryDirectory() as tmpdir: # Vytvoříme dočasnou složku
with ZipFile(f"./assets/{name}.asc","r") as z: # extract the asc file with ZipFile(f"./assets/{name}.asc","r") as z: # Extrahujeme asc soubor
z.extractall(f"{tmpdir}/ascii/{name}") z.extractall(f"{tmpdir}/ascii/{name}")
for f in listdir(f"{tmpdir}/ascii/{name}"): # read all the files for f in listdir(f"{tmpdir}/ascii/{name}"): # Přečte soubory
if f == "config.yml": if f == "config.yml":
with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as c: with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as c:
data = yaml.load(c,Loader=yaml.SafeLoader) data = yaml.load(c,Loader=yaml.SafeLoader)
if(data["speed"] != None): if(data["speed"] != None):
self.speed = data["speed"] self.speed = data["speed"]
with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # Přidá všechny snímky do seznamu
self.frames.append(f.read()) self.frames.append(f.read())
def play(self): def play(self):

View file

@ -12,10 +12,10 @@ class FightHandler:
self.selected = 0 self.selected = 0
self.rebind() self.rebind()
self.name = name self.name = name
self.max = hp # starting ENEMY HP self.max = hp # životy nepřítele
self.hp = self.max # current ENEMY HP self.hp = self.max # AKTUÁLNÍ životy nepřítele
self.enemyDef = defense # ENEMY DEF self.enemyDef = defense # Obrana nepřítele
self.my = 30 # starting PLAYER HP TODO: maybe make this a variable self.my = 30 # životy hráče TODO: maybe make this a variable
self.attacks = attacks self.attacks = attacks
self.img = img self.img = img
self.lang = lang self.lang = lang
@ -73,7 +73,7 @@ class FightHandler:
keyboard.add_hotkey("down",self.down) keyboard.add_hotkey("down",self.down)
keyboard.add_hotkey("enter",self.make_selection) keyboard.add_hotkey("enter",self.make_selection)
def show_inventory(self): # Basically `Game` show_inventory def show_inventory(self): # Zobrazuje inventář TODO: Možná taky equipovat?
system("cls||clear") system("cls||clear")
if len(self.inventory) == 0: if len(self.inventory) == 0:
FightMenu([self.lang["return"]],f" {self.lang['inside_inv']} \n") FightMenu([self.lang["return"]],f" {self.lang['inside_inv']} \n")
@ -81,36 +81,36 @@ class FightHandler:
s = "" s = ""
for i,item in enumerate(self.inventory): for i,item in enumerate(self.inventory):
if type(item) is not str: if type(item) is not str:
if(i == len(self.inventory)): # last item if(i == len(self.inventory)):
s += f"- {item.name}" s += f"- {item.name}"
else: else:
s += f"- {item.name}\n" s += f"- {item.name}\n"
else: else:
if(i == len(self.inventory)): # last item if(i == len(self.inventory)):
s += f"- {item}" s += f"- {item}"
else: else:
s += f"- {item}\n" s += f"- {item}\n"
FightMenu([self.lang["return"]],f" {self.lang['inside_inv']} \n{s}") FightMenu([self.lang["return"]],f" {self.lang['inside_inv']} \n{s}")
def attack(self): def attack(self): # Provede útok vypočítáním ze statů útoku a obrany
p = randrange(len(self.attacks)) p = randrange(len(self.attacks))
name = list(self.attacks[p].keys())[0] name = list(self.attacks[p].keys())[0]
enemyAtk = self.attacks[p][name]["atk"] enemyAtk = self.attacks[p][name]["atk"]
enemyDef = self.enemyDef enemyDef = self.enemyDef
playerAtk = self.equipped["weapon"].attack playerAtk = self.equipped["weapon"].attack
playerDef = self.equipped["armor"].defense playerDef = self.equipped["armor"].defense
self.hp -= playerAtk - enemyDef # enemy takes damage self.hp -= playerAtk - enemyDef # zásah nepříteli
self.my -= enemyAtk - playerDef # player takes damage self.my -= enemyAtk - playerDef # zásah hráči
self.message = f"{self.lang['enemydmg'].replace('$atk',str(playerAtk - enemyDef)).replace('$name',self.name)}\n{self.lang['playerdmg'].replace('$atk',str(enemyAtk - playerDef)).replace('$name',self.attacks[p][name]['name'])}" self.message = f"{self.lang['enemydmg'].replace('$atk',str(playerAtk - enemyDef)).replace('$name',self.name)}\n{self.lang['playerdmg'].replace('$atk',str(enemyAtk - playerDef)).replace('$name',self.attacks[p][name]['name'])}" # Změnit zprávu
def defend(self): def defend(self):
self.message = self.lang["defended"] self.message = self.lang["defended"]
class FightMenu(MenuManager): class FightMenu(MenuManager): # Upravené menu, které nemá input na konci, protože to jinak buguje
def __init__(self,selections:list,additional:str): def __init__(self,selections:list,additional:str):
self.selected = 0 # current selection self.selected = 0
self.selections = selections # available selections self.selections = selections
self.additional = additional # additional text to display above the menu self.additional = additional
keyboard.add_hotkey("up",self.up) keyboard.add_hotkey("up",self.up)
keyboard.add_hotkey("down",self.down) keyboard.add_hotkey("down",self.down)
keyboard.add_hotkey("enter",self.make_selection) keyboard.add_hotkey("enter",self.make_selection)

View file

@ -11,56 +11,56 @@ from .fight import *
from time import sleep from time import sleep
from os import system from os import system
class Game: # the game class keeps information about the loaded game class Game: # Hlavní třída, uchovává údaje o hře
def __init__(self,data:dict,lang): def __init__(self,data:dict,lang):
self.name = data["meta"]["name"] # Game name self.name = data["meta"]["name"] # Název hry
self.author = data["meta"]["creator"] # Game creator self.author = data["meta"]["creator"] # Název tvůrce
self.current = "start" # Current prompt self.current = "start" # Aktuální node
self.nodes = {} # All nodes self.nodes = {} # Seznam všech
self.inventory = [] # Player's inventory self.inventory = [] # Hráčův inventář
self.id = data["meta"]["id"] # Game ID self.id = data["meta"]["id"] # "Unikátní" ID hry
self.lang = lang # Language strings self.lang = lang # Řetězce pro vybraný jazyk
self.save = SaveManager(self.id,self.lang) # saving self.save = SaveManager(self.id,self.lang) # Systém ukládání
self.equipped = {"weapon":None,"armor":None} # Items equipped by player self.equipped = {"weapon":None,"armor":None} # Předměty vybavené hráčem
self.enemies = {} # Enemies self.enemies = {} # Seznam všech nepřátel
if "equippable" in data["meta"].keys(): if "equippable" in data["meta"].keys():
self.equippable = [] # Items that can be equipped by player self.equippable = [] # Předměty, které si hráč může vybavit
for item in data["meta"]["equippable"]: for item in data["meta"]["equippable"]:
name = list(item.keys())[0] name = list(item.keys())[0]
if "def" in item[name].keys() and "atk" in item[name].keys(): if "def" in item[name].keys() and "atk" in item[name].keys():
self.equippable.append(Item(name,item[name]["atk"],item[name]["def"])) self.equippable.append(Item(item[name]["name"],item[name]["atk"],item[name]["def"]))
elif "def" in item[name].keys(): elif "def" in item[name].keys():
self.equippable.append(Item(name=name,defense=item[name]["def"])) self.equippable.append(Item(name=item[name]["name"],defense=item[name]["def"]))
elif "atk" in item[name].keys(): elif "atk" in item[name].keys():
self.equippable.append(Item(name,item[name]["atk"])) self.equippable.append(Item(item[name]["name"],item[name]["atk"]))
if("starter" in item[name].keys()): # if starter, equip and add to inventory if("starter" in item[name].keys()): # Pokud je starter, přidáme hráčí na začátku do inventáře
if item[name]["starter"]: if item[name]["starter"]:
i = next((x for x in self.equippable if x.name == list(item.keys())[0])) i = next((x for x in self.equippable if x.name == list(item.keys())[0]))
self.inventory.append(i) self.inventory.append(i)
self.equipped[i.type] = i self.equipped[i.type] = i
if "enemies" in data["meta"].keys(): if "enemies" in data["meta"].keys():
# Load enemies # Načte nepřátele
for en in data["meta"]["enemies"]: for en in data["meta"]["enemies"]:
name = list(en.keys())[0] name = list(en.keys())[0]
self.enemies[name] = {"name":en["name"],"hp":en["hp"],"attacks":en["attacks"],"def":en["def"]} self.enemies[name] = {"name":en["name"],"hp":en["hp"],"attacks":en["attacks"],"def":en["def"]}
for k in data["game"]: for k in data["game"]: # načte všechny nody
self.nodes.update({k:data["game"][k]}) self.nodes.update({k:data["game"][k]})
def main_menu(self): # displays the main menu def main_menu(self): # Zobrazí hlavní menu
l = self.save.load() l = self.save.load()
if not l: if not l:
# New game # V případě nové hry
m = MenuManager([self.lang['start'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}") m = MenuManager([self.lang['start'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}")
selection = m.selected selection = m.selected
system("cls||clear") system("cls||clear")
if(selection == 0): # start new game if(selection == 0): # Začít
self.print_text() self.print_text()
elif(selection == 1): elif(selection == 1): # Nastavení
self.settings_menu() self.settings_menu()
elif(selection == 2): elif(selection == 2): # Vypnout
print(self.lang['quitting']) print(self.lang['quitting'])
exit() exit()
else: # Display continue else: # V případě uložené hry zobrazí "Pokračovat"
m = MenuManager([self.lang['continue'],self.lang['new_game'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}") m = MenuManager([self.lang['continue'],self.lang['new_game'],self.lang['options'],self.lang['quit']],f"{self.name}\n{self.lang['game_by'].replace('$author',self.author)}")
selection = m.selected selection = m.selected
system("cls||clear") system("cls||clear")
@ -76,7 +76,7 @@ class Game: # the game class keeps information about the loaded game
print(self.lang['quitting']) print(self.lang['quitting'])
exit() exit()
def settings_menu(self): # displays the settings menu def settings_menu(self): # Zobrazí nastavení
m = MenuManager([self.lang['lang'],self.lang['back']],self.lang['options']) m = MenuManager([self.lang['lang'],self.lang['back']],self.lang['options'])
selection = m.selected selection = m.selected
if(selection == 0): if(selection == 0):
@ -93,11 +93,13 @@ class Game: # the game class keeps information about the loaded game
else: else:
self.main_menu() self.main_menu()
def print_text(self): # Prints out the current prompt def print_text(self): # Zobrazí hráči aktuální node
system("cls||clear") system("cls||clear")
if "add_item" in self.nodes[self.current].keys(): # if there is an add_inventory key in the node, if "add_item" in self.nodes[self.current].keys(): # V případě, že máme přidat hráči věc do inventáře
# add item to inventory
item = self.nodes[self.current]['add_item'] item = self.nodes[self.current]['add_item']
for i in self.equippable:
if i.name == item: # Pokud lze vybavit, změnit na instanci třídy Item
item = i
if item not in self.inventory: if item not in self.inventory:
self.inventory.append(item) self.inventory.append(item)
print(self.inventory) print(self.inventory)
@ -105,13 +107,13 @@ class Game: # the game class keeps information about the loaded game
print(f"{self.lang['acquire'].replace('$item',f'{Fore.CYAN}{item}{Fore.RESET}')}") print(f"{self.lang['acquire'].replace('$item',f'{Fore.CYAN}{item}{Fore.RESET}')}")
sleep(3) sleep(3)
system("clear||cls") system("clear||cls")
animated = re.search(r"(?!{).+(?=})",self.nodes[self.current]["text"]) # find the animated text animated = re.search(r"(?!{).+(?=})",self.nodes[self.current]["text"]) # Hledá kód pro vložení animovaného textu
if(animated != None): if(animated != None):
self.print_animated(animated.group(0)) self.print_animated(animated.group(0))
self.nodes[self.current]["text"] = self.nodes[self.current]["text"].replace("{"+animated.group(0)+"}","") # remove the animated text from the text prompt self.nodes[self.current]["text"] = self.nodes[self.current]["text"].replace("{"+animated.group(0)+"}","") # Odstraní kód z textu
if("actions" in self.nodes[self.current].keys()): if("actions" in self.nodes[self.current].keys()):
actions_desc = [] # has descriptions of text prompts, so that we don't need to find them in MenuManager actions_desc = [] # uchovává text nodu, abychom jej nemuseli hledat v MenuManager
need_item = [] # helps implement a check for needing an item need_item = [] # pomáhá implementovat kontrolu potřebného předmětu
for option in self.nodes[self.current]["actions"]: for option in self.nodes[self.current]["actions"]:
try: try:
actions_desc.append(self.nodes[option]["description"]) actions_desc.append(self.nodes[option]["description"])
@ -124,45 +126,45 @@ class Game: # the game class keeps information about the loaded game
exit(1) exit(1)
m = "" m = ""
actions_desc.extend([self.lang['inventory'],self.lang['quit']]) actions_desc.extend([self.lang['inventory'],self.lang['quit']])
if(all(element == None for element in need_item) is False): if(all(element == None for element in need_item) is False): # Pokud platí, musíme zkontrolovat, jestli hráč má předmět
need_item.extend([None, None]) need_item.extend([None, None])
# we need to check if user has item
m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item) m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item)
while need_item[m.selected] != None and all(element not in self.inventory for element in need_item[m.selected]): # until user selects an available prompt, re-prompt again while need_item[m.selected] != None and all(element not in self.inventory for element in need_item[m.selected]): # Opakovat, dokud uživatel nevybere platný výběr
m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item) m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item)
if m.selected <= len(actions_desc)-3 and "has_item" in self.nodes[self.nodes[self.current]["actions"][m.selected]].keys(): if m.selected <= len(actions_desc)-3 and "has_item" in self.nodes[self.nodes[self.current]["actions"][m.selected]].keys():
for item in need_item[m.selected]: for item in need_item[m.selected]:
self.inventory.remove(item) self.inventory.remove(item)
elif "fight" in self.nodes[self.current].keys(): elif "fight" in self.nodes[self.current].keys():
# Initiate a fight # Spustí boj
enemy = self.enemies[self.nodes[self.current]["fight"]] # TODO: Complete after fight actions enemy = self.enemies[self.nodes[self.current]["fight"]] # Získá info o nepříteli
m = FightHandler(self.nodes[self.current]["text"],enemy["name"],enemy["hp"],enemy["def"],enemy["attacks"],self.lang,self.equipped,self.inventory) m = FightHandler(self.nodes[self.current]["text"],enemy["name"],enemy["hp"],enemy["def"],enemy["attacks"],self.lang,self.equipped,self.inventory)
input() input()
while m.hp > 0 and m.my > 0: while m.hp > 0 and m.my > 0:
m.show() m.show()
m.rebind() # rebind due to MenuManager in show_inventory m.rebind() # Znovu nastavuje klávesy, kvůli MenuManageru uvnitř show_inventory, který je maže
input() input()
system("cls||clear") system("cls||clear")
keyboard.remove_all_hotkeys() keyboard.remove_all_hotkeys()
if m.hp < 1: if m.hp < 1:
# Enemy defeated # Nepřítel byl poražen
print(self.lang["defeated"].replace("$enemy",enemy["name"])) print(self.lang["defeated"].replace("$enemy",enemy["name"]))
sleep(5) sleep(3)
self.current = self.nodes[self.current]["actions"][0] # move to the first action self.current = self.nodes[self.current]["actions"][0] # Přesune na první akci
self.print_text() self.print_text()
return
else: else:
# Player defeated # Hráč byl poražen TODO: Otestovat
print(self.lang["defeat"].replace("$enemy",enemy["name"])) print(self.lang["defeat"].replace("$enemy",enemy["name"]))
sleep(5) sleep(3)
self.print_text() self.print_text()
return return
else: else:
m = MenuManager(actions_desc,self.parse_colors(self.nodes[self.current]["text"])) m = MenuManager(actions_desc,self.parse_colors(self.nodes[self.current]["text"]))
sel = m.selected sel = m.selected
if(sel == len(actions_desc)-2): # show inventory if(sel == len(actions_desc)-2): # Zobrazit inventář
self.show_inventory() self.show_inventory()
elif (sel == len(actions_desc)-1): # Save & quit elif (sel == len(actions_desc)-1): # Uložit a ukončit
self.save.currentPrompt = self.current # save the current prompt self.save.currentPrompt = self.current
self.save.inventory = self.inventory self.save.inventory = self.inventory
self.save.save() self.save.save()
exit(0) exit(0)
@ -173,37 +175,42 @@ class Game: # the game class keeps information about the loaded game
print(self.parse_colors(self.nodes[self.current]["text"])) print(self.parse_colors(self.nodes[self.current]["text"]))
print("") print("")
def show_inventory(self): def show_inventory(self): # Zobrazí hráčův inventář
if len(self.inventory) == 0: if len(self.inventory) == 0:
MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n") MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n")
else: else:
s = "" s = ""
op = [self.lang["return"]]
for i,item in enumerate(self.inventory): for i,item in enumerate(self.inventory):
if type(item) is Item: if type(item) is Item: # Pokud je předmět třídy Item, zobrazit zda-li je vybaven nebo ne
if(i == len(self.inventory)): # last item if self.equipped["weapon"] == item or self.equipped["armor"] == item:
s += f"- {item.name}" op.append(f"- {item.name} | {self.lang['equipped']}")
else: else:
s += f"- {item.name}\n" op.append(f"- {item.name}")
else: else:
if(i == len(self.inventory)): # last item if(i == len(self.inventory)): # poslední, nepřidávat newline
s += f"- {item}" s += f"- {item}"
else: else:
s += f"- {item}\n" s += f"- {item}\n"
MenuManager([self.lang["return"]],f" {self.lang['inside_inv']} \n{s}") m = MenuManager(op,f" {self.lang['inside_inv']} \n{s}")
if(m.selected != len(op)-1):
# Vybavit
i = op[m.selected]
self.equipped[i.type] = i
self.print_text() self.print_text()
def print_animated(self,animid): # prints the first found occurence of an ascii animation def print_animated(self,animid): # Zobrazí animaci
animation = AsciiAnimation() animation = AsciiAnimation()
animation.load_ascii(animid) animation.load_ascii(animid)
animation.play() animation.play()
print() print()
def parse_colors(self,text:str) -> str: # Converts color codes into terminal colors def parse_colors(self,text:str) -> str: # Převádí kód na barvy v terminálu
newText = text.replace("&b",Fore.CYAN).replace("&c",Fore.RED).replace("&e", Fore.YELLOW).replace("&a",Fore.GREEN).replace("&9",Fore.BLUE).replace("&r",Fore.RESET).replace("&f",Fore.WHITE).replace("&5",Fore.MAGENTA).replace("\n",Fore.RESET + "\n") # replace color codes and newlines with colorama newText = text.replace("&b",Fore.CYAN).replace("&c",Fore.RED).replace("&e", Fore.YELLOW).replace("&a",Fore.GREEN).replace("&9",Fore.BLUE).replace("&r",Fore.RESET).replace("&f",Fore.WHITE).replace("&5",Fore.MAGENTA).replace("\n",Fore.RESET + "\n") # replace color codes and newlines with colorama
newText += Fore.RESET # reset color at the end of the text newText += Fore.RESET # resetovat na konci
return newText return newText
def load(file_path,lang): # starts to load the game from YAML def load(file_path,lang): # Načte hru z YAML souboru
try: try:
with open(file_path) as f: with open(file_path) as f:
data = yaml.load(f,Loader=SafeLoader) data = yaml.load(f,Loader=SafeLoader)
@ -212,4 +219,4 @@ def load(file_path,lang): # starts to load the game from YAML
except Exception as e: except Exception as e:
print(f"{Back.RED}{Fore.WHITE}ERROR{Fore.RESET}{Back.RESET}") print(f"{Back.RED}{Fore.WHITE}ERROR{Fore.RESET}{Back.RESET}")
print(e) print(e)
return None return None

View file

@ -1,9 +1,9 @@
class Item: class Item: # Reprezentuje vybavitelný předmět
def __init__(self,name:str,attack:int = 0,defense:int = 0) -> None: def __init__(self,name:str,attack:int = 0,defense:int = 0) -> None:
self.name = name self.name = name # Název, jak je zobrazován hráči
if attack == 0 and defense > 0: if attack == 0 and defense > 0: # Nastaví typ předmětu
self.type = "armor" self.type = "armor"
else: else:
self.type = "weapon" self.type = "weapon"
self.attack = attack self.attack = attack # Stat útoku
self.defense = defense self.defense = defense # Stat obrany

View file

@ -14,6 +14,7 @@ acquire: 'Získal jsi $item'
inside_inv: "VÁŠ INVENTÁŘ" inside_inv: "VÁŠ INVENTÁŘ"
return: "Vrátit se" return: "Vrátit se"
equipped: "VYBAVENO"
lang: 'Jazyk' lang: 'Jazyk'
back: 'Zpět' back: 'Zpět'

View file

@ -14,6 +14,7 @@ acquire: 'You acquired $item'
inside_inv: "YOUR INVENTORY" inside_inv: "YOUR INVENTORY"
return: "Return" return: "Return"
equipped: "EQUIPPED"
lang: 'Language' lang: 'Language'
back: 'Back' back: 'Back'

View file

@ -3,12 +3,12 @@ import keyboard
from colorama import Fore from colorama import Fore
class MenuManager: class MenuManager:
''' '''
Creates text menus controllable with arrow keys Vytváří navigovatelná textová menu
''' '''
def __init__(self,selections:list,additional:str): def __init__(self,selections:list,additional:str):
self.selected = 0 # current selection self.selected = 0 # aktuální výběr
self.selections = selections # available selections self.selections = selections # Dostupné možnosti
self.additional = additional # additional text to display above the menu self.additional = additional # Text, který se zobrazí nad menu
keyboard.add_hotkey("up",self.up) keyboard.add_hotkey("up",self.up)
keyboard.add_hotkey("down",self.down) keyboard.add_hotkey("down",self.down)
keyboard.add_hotkey("enter",self.make_selection) keyboard.add_hotkey("enter",self.make_selection)
@ -45,7 +45,7 @@ class MenuManager:
class HasItemDialogue(MenuManager): class HasItemDialogue(MenuManager):
''' '''
Custom handler for dialogue, that requires to check if the user has an item Odvozená třída pro kontrolu, zda-li hráč předmět
''' '''
def __init__(self, selections: list, additional: str,inv:list,need_item:list): def __init__(self, selections: list, additional: str,inv:list,need_item:list):
system("cls||clear") system("cls||clear")

View file

@ -4,29 +4,28 @@ import yaml
from lib.game import Item from lib.game import Item
class SaveManager: # manages save and configuration files class SaveManager: # Spravuje ukládání
def __init__(self,gid:str,lang): def __init__(self,gid:str,lang):
self.id = gid # game ID self.id = gid # ID hry
self.currentPrompt = "" # Current prompt self.currentPrompt = "" # Aktuální node
self.inventory = [] # Items in inventory self.inventory = [] # Předměty v inventáři
self.version = 1 self.version = 1
self.lang = lang self.lang = lang
def load(self): def load(self):
if(path.exists(f"./saves/{self.id}.yml")): if(path.exists(f"./saves/{self.id}.yml")):
with open(f"./saves/{self.id}.yml",encoding="utf-8") as f: with open(f"./saves/{self.id}.yml",encoding="utf-8") as f:
data = yaml.load(f,Loader=yaml.SafeLoader) data = yaml.load(f,Loader=yaml.SafeLoader) # Načteme z YAMLu
self.currentPrompt = data["currentPrompt"] self.currentPrompt = data["currentPrompt"]
if(data["version"] < self.version): if(data["version"] < self.version): # V případě nekompatibility zobrazit varování
system("cls||clear") system("cls||clear")
print(self.lang["no_comp"]) print(self.lang["no_comp"])
sleep(5) sleep(5)
inv = [] inv = []
for item in data["inventory"]: for item in data["inventory"]: # Zpracovat inventář (zvlášť pouze text a zvlášť vybavitelné)
if type(item) is str: if type(item) is str:
inv.append(item) inv.append(item)
else: else:
# Item class
inv.append(Item(item["name"],item["atk"],item["def"])) inv.append(Item(item["name"],item["atk"],item["def"]))
return True return True
return False return False
@ -37,7 +36,7 @@ class SaveManager: # manages save and configuration files
if type(item) is str: if type(item) is str:
inv.append(item) inv.append(item)
else: else:
# Item class # Pro vybavitelné předměty
inv.append({"name":item.name,"atk":item.attack,"def":item.defense}) inv.append({"name":item.name,"atk":item.attack,"def":item.defense})
data = {"id":self.id,"currentPrompt":self.currentPrompt,"inventory":self.inventory,"version":1} data = {"id":self.id,"currentPrompt":self.currentPrompt,"inventory":self.inventory,"version":1}
with open(f"./saves/{self.id}.yml",mode="w",encoding="utf-8") as f: with open(f"./saves/{self.id}.yml",mode="w",encoding="utf-8") as f: