Add showing inventory

This commit is contained in:
Matyáš Caras 2022-04-06 10:03:21 +02:00 committed by GitHub
parent 997475aef1
commit 7361882831
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 17 deletions

View file

@ -19,7 +19,7 @@ def lang():
data = yaml.load(f,Loader=SafeLoader)
return data
def main(): # TODO: Maybe a menu for available text games?
def main():
l = lang()
init()
if(not isdir("./games")):
@ -41,7 +41,8 @@ def main(): # TODO: Maybe a menu for available text games?
print(l['no_games'])
else:
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']}")
games[m.selected].main_menu()

View file

@ -21,6 +21,5 @@ class AsciiAnimation:
data = yaml.load(c,Loader=yaml.SafeLoader)
if(data["speed"] != None):
self.speed = data["speed"]
pass
with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list
self.frames.append(f.read())

View file

@ -1,3 +1,4 @@
import enum
import yaml
from yaml.loader import SafeLoader
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,
# add item to inventory
item = self.nodes[self.current]['add_item']
if item not in self.inventory:
self.inventory.append(item)
print(self.inventory)
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"])
else:
need_item.append(None)
except:
except Exception:
print(f"{Back.RED}{Fore.WHITE}{self.lang['no_action'].replace('$action',option)}{Fore.RESET}")
exit(1)
m = ""
actions_desc.extend([self.lang['inventory'],self.lang['quit']])
if(all(element == None for element in need_item) is False):
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)
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
m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item)
else:
m = MenuManager(actions_desc,self.parse_colors(self.nodes[self.current]["text"]))
sel = m.selected
self.current = self.nodes[self.current]["actions"][sel]
if(sel == len(actions_desc)-2): # show inventory
self.show_inventory()
elif (sel == len(actions_desc)-1): # Save & quit
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()
else:
print(self.parse_colors(self.nodes[self.current]["text"]))
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
animation = AsciiAnimation()
animation.load_ascii(animid)

View file

@ -9,8 +9,12 @@ quit: 'Vypnout'
quitting: 'Vypínám'
continue: 'Pokračovat'
new_game: 'Nová hra'
inventory: 'Zobrazit inventář'
acquire: 'Získal jsi $item'
inside_inv: "VÁŠ INVENTÁŘ"
return: "Vrátit se"
lang: 'Jazyk'
back: 'Zpět'
lang_en: 'Angličtina'

View file

@ -9,8 +9,12 @@ quit: 'Quit'
quitting: 'Quitting'
continue: 'Continue'
new_game: 'New game'
inventory: 'Show inventory'
acquire: 'You acquired $item'
inside_inv: "YOUR INVENTORY"
return: "Return"
lang: 'Language'
back: 'Back'
lang_en: 'English'

View file

@ -5,7 +5,6 @@ class SaveManager: # manages save and configuration files
def __init__(self):
self.id = "" # game ID
self.currentPrompt = "" # Current prompt
self.lang = "" # Selected language
self.inventory = [] # Items in inventory
def load(self):
@ -18,6 +17,6 @@ class SaveManager: # manages save and configuration files
return False
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:
yaml.dump(data,f)