diff --git a/__main__.py b/__main__.py index c2f4a8c..e820bf5 100644 --- a/__main__.py +++ b/__main__.py @@ -9,8 +9,9 @@ def main(): exit(1) else: game = load(argv[1]) + if(game is None): + exit(1) game.printme() if __name__ == "__main__": - main() - + main() \ No newline at end of file diff --git a/example.yml b/example.yml index a565366..d3163ef 100644 --- a/example.yml +++ b/example.yml @@ -4,7 +4,7 @@ meta: # make sure every key is in lowercase 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 - text: "You arrive to a small tavern in the middle of nowhere.\nYou are greeted with a non-welcoming look on the faces of all the customers." # here is the text, which gets printed + text: "&bYou arrive to a small tavern in the middle of nowhere.\nYou are greeted with a non-welcoming look on the faces of all the customers." # here is the text, which gets printed actions: # here you add a list of actions that are inside of `game`, the user can select them - wave - sit diff --git a/game.py b/game.py index 4e0398e..9902603 100644 --- a/game.py +++ b/game.py @@ -3,20 +3,20 @@ from yaml.loader import SafeLoader from colorama import Fore, Back, Style class Game: - def __init__(self,name:str,author:str,game:dict): - self.name = name - self.author = author + def __init__(self,data:dict): + self.name = data["meta"]["name"] + self.author = data["meta"]["creator"] self.current = "start" self.nodes = {} - for k in game: - self.nodes.update({k:game[k]}) + for k in data["game"]: + self.nodes.update({k:data["game"][k]}) - def make_selection(self,selection): - if(selection is not int or selection >= len(self.nodes[self.current][selection]) or selection < 0): + def make_selection(self,selection:int) -> bool: + if(type(selection) != int or selection >= len(self.nodes[self.current]["actions"]) or selection < 0): print("Invalid selection") return False else: - self.current = self.nodes[self.current][selection] + self.current = self.nodes[self.current]["actions"][selection] return True @@ -27,26 +27,34 @@ class Game: print(self.nodes[self.current]["text"]) print("") ostring = "" - for i,option in enumerate(self.nodes[self.current]["actions"]): - ostring+=f"{i} - {self.nodes[option]['description']}\n" - print(ostring) - sel = input("Make a selection (number): ") - isWrong = self.make_selection(sel) - while isWrong == False: + if("actions" in self.nodes[self.current].keys()): + for i,option in enumerate(self.nodes[self.current]["actions"]): + ostring+=f"{i} - {self.nodes[option]['description']}\n" + print(ostring) sel = input("Make a selection (number): ") - isWrong = self.make_selection(sel) - self.printme() + isWrong = self.make_selection(int(sel)) + while isWrong == False: + sel = input("Make a selection (number): ") + isWrong = self.make_selection(sel) + self.printme() def parse_colors(self,text:str) -> str: ''' Used to convert color codes in string to colors from the colorama lib ''' - return text.replace("&b",Fore.CYAN) + newText = text.replace("&b",Fore.CYAN).replace("\n",Fore.RESET + "\n") # replace color codes and newlines with colorama + newText += Fore.RESET # reset color at the end of the text + return newText def load(file_path): '''Loads the game from a YAML file to a Game class''' - with open(file_path) as f: - data = yaml.load(f,Loader=SafeLoader) - g = Game(data["meta"]["name"],data["meta"]["creator"],data["game"]) - return g \ No newline at end of file + try: + with open(file_path) as f: + data = yaml.load(f,Loader=SafeLoader) + g = Game(data) + return g + except Exception as e: + print("An exception has occured while loading the game from the YAML file:") + print(e) + return None \ No newline at end of file