Add showing inventory
This commit is contained in:
parent
997475aef1
commit
7361882831
6 changed files with 47 additions and 17 deletions
|
@ -19,7 +19,7 @@ def lang():
|
||||||
data = yaml.load(f,Loader=SafeLoader)
|
data = yaml.load(f,Loader=SafeLoader)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def main(): # TODO: Maybe a menu for available text games?
|
def main():
|
||||||
l = lang()
|
l = lang()
|
||||||
init()
|
init()
|
||||||
if(not isdir("./games")):
|
if(not isdir("./games")):
|
||||||
|
@ -41,7 +41,8 @@ def main(): # TODO: Maybe a menu for available text games?
|
||||||
print(l['no_games'])
|
print(l['no_games'])
|
||||||
else:
|
else:
|
||||||
names = []
|
names = []
|
||||||
for n in games: names.append(n.name)
|
for n in games:
|
||||||
|
names.append(n.name)
|
||||||
m = MenuManager(names,f" TEXTY \n{l['available']}")
|
m = MenuManager(names,f" TEXTY \n{l['available']}")
|
||||||
games[m.selected].main_menu()
|
games[m.selected].main_menu()
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,5 @@ class AsciiAnimation:
|
||||||
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"]
|
||||||
pass
|
|
||||||
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: # add all frames into list
|
||||||
self.frames.append(f.read())
|
self.frames.append(f.read())
|
31
lib/game.py
31
lib/game.py
|
@ -1,3 +1,4 @@
|
||||||
|
import enum
|
||||||
import yaml
|
import yaml
|
||||||
from yaml.loader import SafeLoader
|
from yaml.loader import SafeLoader
|
||||||
from colorama import Fore, Back
|
from colorama import Fore, Back
|
||||||
|
@ -73,6 +74,7 @@ class Game: # the game class keeps information about the loaded game
|
||||||
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(): # if there is an add_inventory key in the node,
|
||||||
# add item to inventory
|
# add item to inventory
|
||||||
item = self.nodes[self.current]['add_item']
|
item = self.nodes[self.current]['add_item']
|
||||||
|
if item not in self.inventory:
|
||||||
self.inventory.append(item)
|
self.inventory.append(item)
|
||||||
print(self.inventory)
|
print(self.inventory)
|
||||||
system("clear||cls")
|
system("clear||cls")
|
||||||
|
@ -93,27 +95,48 @@ class Game: # the game class keeps information about the loaded game
|
||||||
need_item.append(self.nodes[option]["has_item"])
|
need_item.append(self.nodes[option]["has_item"])
|
||||||
else:
|
else:
|
||||||
need_item.append(None)
|
need_item.append(None)
|
||||||
except:
|
except Exception:
|
||||||
print(f"{Back.RED}{Fore.WHITE}{self.lang['no_action'].replace('$action',option)}{Fore.RESET}")
|
print(f"{Back.RED}{Fore.WHITE}{self.lang['no_action'].replace('$action',option)}{Fore.RESET}")
|
||||||
exit(1)
|
exit(1)
|
||||||
m = ""
|
m = ""
|
||||||
|
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):
|
||||||
|
need_item.extend([None, None])
|
||||||
# we need to check if user has item
|
# 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)
|
||||||
print(self.inventory)
|
# TODO: Remove item from inventory after using it?
|
||||||
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]): # until user selects an available prompt, re-prompt again
|
||||||
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)
|
||||||
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
|
||||||
self.current = self.nodes[self.current]["actions"][sel]
|
self.show_inventory()
|
||||||
|
elif (sel == len(actions_desc)-1): # Save & quit
|
||||||
self.save.currentPrompt = self.current # save the current prompt
|
self.save.currentPrompt = self.current # save the current prompt
|
||||||
|
self.save.inventory = self.inventory
|
||||||
|
self.save.save()
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
|
self.current = self.nodes[self.current]["actions"][sel]
|
||||||
self.print_text()
|
self.print_text()
|
||||||
else:
|
else:
|
||||||
print(self.parse_colors(self.nodes[self.current]["text"]))
|
print(self.parse_colors(self.nodes[self.current]["text"]))
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
def show_inventory(self):
|
||||||
|
if len(self.inventory) == 0:
|
||||||
|
MenuManager([self.lang["return"]],f" YOUR INVENTORY \n")
|
||||||
|
else:
|
||||||
|
s = ""
|
||||||
|
for i,item in enumerate(self.inventory):
|
||||||
|
if(i == len(self.inventory)): # last item
|
||||||
|
s += f"- {item}"
|
||||||
|
else:
|
||||||
|
s += f"- {item}\n"
|
||||||
|
MenuManager([self.lang["return"]],f" YOUR INVENTORY \n{s}")
|
||||||
|
self.print_text()
|
||||||
|
|
||||||
def print_animated(self,animid): # prints the first found occurence of an ascii animation
|
def print_animated(self,animid): # prints the first found occurence of an ascii animation
|
||||||
animation = AsciiAnimation()
|
animation = AsciiAnimation()
|
||||||
animation.load_ascii(animid)
|
animation.load_ascii(animid)
|
||||||
|
|
|
@ -9,8 +9,12 @@ quit: 'Vypnout'
|
||||||
quitting: 'Vypínám'
|
quitting: 'Vypínám'
|
||||||
continue: 'Pokračovat'
|
continue: 'Pokračovat'
|
||||||
new_game: 'Nová hra'
|
new_game: 'Nová hra'
|
||||||
|
inventory: 'Zobrazit inventář'
|
||||||
acquire: 'Získal jsi $item'
|
acquire: 'Získal jsi $item'
|
||||||
|
|
||||||
|
inside_inv: "VÁŠ INVENTÁŘ"
|
||||||
|
return: "Vrátit se"
|
||||||
|
|
||||||
lang: 'Jazyk'
|
lang: 'Jazyk'
|
||||||
back: 'Zpět'
|
back: 'Zpět'
|
||||||
lang_en: 'Angličtina'
|
lang_en: 'Angličtina'
|
||||||
|
|
|
@ -9,8 +9,12 @@ quit: 'Quit'
|
||||||
quitting: 'Quitting'
|
quitting: 'Quitting'
|
||||||
continue: 'Continue'
|
continue: 'Continue'
|
||||||
new_game: 'New game'
|
new_game: 'New game'
|
||||||
|
inventory: 'Show inventory'
|
||||||
acquire: 'You acquired $item'
|
acquire: 'You acquired $item'
|
||||||
|
|
||||||
|
inside_inv: "YOUR INVENTORY"
|
||||||
|
return: "Return"
|
||||||
|
|
||||||
lang: 'Language'
|
lang: 'Language'
|
||||||
back: 'Back'
|
back: 'Back'
|
||||||
lang_en: 'English'
|
lang_en: 'English'
|
||||||
|
|
|
@ -5,7 +5,6 @@ class SaveManager: # manages save and configuration files
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.id = "" # game ID
|
self.id = "" # game ID
|
||||||
self.currentPrompt = "" # Current prompt
|
self.currentPrompt = "" # Current prompt
|
||||||
self.lang = "" # Selected language
|
|
||||||
self.inventory = [] # Items in inventory
|
self.inventory = [] # Items in inventory
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
@ -18,6 +17,6 @@ class SaveManager: # manages save and configuration files
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
data = {"id":self.id,"currentPrompt":self.currentPrompt,"inventory":self.inventory,"lang":self.lang}
|
data = {"id":self.id,"currentPrompt":self.currentPrompt,"inventory":self.inventory}
|
||||||
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:
|
||||||
yaml.dump(data,f)
|
yaml.dump(data,f)
|
Reference in a new issue