Hopefully item checking is complete

This commit is contained in:
Matyáš Caras 2022-04-06 09:42:47 +02:00 committed by GitHub
parent 111e918c93
commit 997475aef1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 11 deletions

View file

@ -21,12 +21,15 @@ game: # here goes all the game logic
beer: beer:
description: "Order beer" description: "Order beer"
text: "You order some &ebeer" text: "You order some &ebeer"
add_inventory: "Beer" # add something to inventory add_item: "Beer" # add something to inventory
actions: actions:
- do_something - do_something
nothing: nothing:
description: "Do nothing" description: "Do nothing"
text: "You sit and wait..." text: "You sit and wait..."
actions:
- drink
- leave
do_something: do_something:
description: "Continue" description: "Continue"
text: "You start to feel bored." text: "You start to feel bored."

View file

@ -70,13 +70,22 @@ class Game: # the game class keeps information about the loaded game
def print_text(self): # Prints out the current prompt def print_text(self): # Prints out the current prompt
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,
# add item to inventory
item = self.nodes[self.current]['add_item']
self.inventory.append(item)
print(self.inventory)
system("clear||cls")
print(f"{self.lang['acquire'].replace('$item',f'{Fore.CYAN}{item}{Fore.RESET}')}")
sleep(3)
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"]) # find the animated text
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)+"}","") # remove the animated text from the text prompt
if("actions" in self.nodes[self.current].keys()): if("actions" in self.nodes[self.current].keys()):
actions_desc = [] actions_desc = [] # has descriptions of text prompts, so that we don't need to find them in MenuManager
need_item = [] need_item = [] # helps implement a check for needing an item
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"])
@ -88,15 +97,16 @@ class Game: # the game class keeps information about the loaded game
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 = ""
if((element == None for element in need_item) is False): if(all(element == None for element in need_item) is False):
# we need to check if user has item # we need to check if user has item
m = HasItemDialogue(self.nodes[self.current]["actions"],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)
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: else:
m = MenuManager(self.nodes[self.current]["actions"],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 "add_item" in self.nodes[self.current]: # if there is an add_inventory key in the node,
# add item to inventory
self.inventory.append(self.nodes[self.current]["add_inventory"])
self.current = self.nodes[self.current]["actions"][sel] self.current = self.nodes[self.current]["actions"][sel]
self.save.currentPrompt = self.current # save the current prompt self.save.currentPrompt = self.current # save the current prompt
self.print_text() self.print_text()

View file

@ -9,6 +9,7 @@ quit: 'Vypnout'
quitting: 'Vypínám' quitting: 'Vypínám'
continue: 'Pokračovat' continue: 'Pokračovat'
new_game: 'Nová hra' new_game: 'Nová hra'
acquire: 'Získal jsi $item'
lang: 'Jazyk' lang: 'Jazyk'
back: 'Zpět' back: 'Zpět'

View file

@ -9,6 +9,7 @@ quit: 'Quit'
quitting: 'Quitting' quitting: 'Quitting'
continue: 'Continue' continue: 'Continue'
new_game: 'New game' new_game: 'New game'
acquire: 'You acquired $item'
lang: 'Language' lang: 'Language'
back: 'Back' back: 'Back'

View file

@ -35,6 +35,7 @@ class MenuManager:
self.show_menu() self.show_menu()
def show_menu(self): def show_menu(self):
system("cls||clear")
print(self.additional) print(self.additional)
for selection in self.selections: for selection in self.selections:
if(self.selected == self.selections.index(selection)): if(self.selected == self.selections.index(selection)):
@ -47,17 +48,26 @@ class HasItemDialogue(MenuManager):
Custom handler for dialogue, that requires to check if the user has an item Custom handler for dialogue, that requires to check if the user has an item
''' '''
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")
self.inventory = inv self.inventory = inv
self.need_items = need_item self.need_items = need_item
super().__init__(selections, additional) super().__init__(selections, additional)
def show_menu(self): def show_menu(self):
print(self.additional) print(self.additional)
for i,selection in enumerate(self.selections): for i,selection in enumerate(self.selections):
if(self.need_items[i] != None and self.need_items[i] not in self.inv): if(self.need_items[i] != None and all(element not in self.inventory for element in self.need_items[i])):
c = ""
for i,item in enumerate(self.need_items[i]):
if item not in self.inventory:
if i == len(self.need_items)-self.need_items.count(None)-1: # last item, don't add a comma
c+=f"{item} "
else:
c+=f"{item}, "
# user does not have the needed item # user does not have the needed item
if(self.selected == i): if(self.selected == i):
print(f"{Fore.RED}-> {Fore.CYAN}{selection}{Fore.RESET} (Need '{self.need_items[i]}')") print(f"{Fore.RED}-> {Fore.CYAN}{selection}{Fore.RESET} (Need {c})")
else: else:
print(f" {Fore.CYAN}{selection}{Fore.RESET}") print(f" {Fore.CYAN}{selection}{Fore.RESET}")
else: else:
@ -66,3 +76,5 @@ class HasItemDialogue(MenuManager):
print(f"{Fore.RED}->{Fore.RESET} {selection}") print(f"{Fore.RED}->{Fore.RESET} {selection}")
else: else:
print(f" {selection}") print(f" {selection}")
def make_selection(self) -> int:
keyboard.remove_all_hotkeys()