Fixes and improvements

This commit is contained in:
Matyáš Caras 2021-11-26 22:13:30 +01:00
parent b9f7abb29e
commit d0cc24c193
3 changed files with 33 additions and 24 deletions

View file

@ -9,8 +9,9 @@ def main():
exit(1) exit(1)
else: else:
game = load(argv[1]) game = load(argv[1])
if(game is None):
exit(1)
game.printme() game.printme()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -4,7 +4,7 @@ meta: # make sure every key is in lowercase
game: # here goes all the game logic 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 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 actions: # here you add a list of actions that are inside of `game`, the user can select them
- wave - wave
- sit - sit

30
game.py
View file

@ -3,20 +3,20 @@ from yaml.loader import SafeLoader
from colorama import Fore, Back, Style from colorama import Fore, Back, Style
class Game: class Game:
def __init__(self,name:str,author:str,game:dict): def __init__(self,data:dict):
self.name = name self.name = data["meta"]["name"]
self.author = author self.author = data["meta"]["creator"]
self.current = "start" self.current = "start"
self.nodes = {} self.nodes = {}
for k in game: for k in data["game"]:
self.nodes.update({k:game[k]}) self.nodes.update({k:data["game"][k]})
def make_selection(self,selection): def make_selection(self,selection:int) -> bool:
if(selection is not int or selection >= len(self.nodes[self.current][selection]) or selection < 0): if(type(selection) != int or selection >= len(self.nodes[self.current]["actions"]) or selection < 0):
print("Invalid selection") print("Invalid selection")
return False return False
else: else:
self.current = self.nodes[self.current][selection] self.current = self.nodes[self.current]["actions"][selection]
return True return True
@ -27,11 +27,12 @@ class Game:
print(self.nodes[self.current]["text"]) print(self.nodes[self.current]["text"])
print("") print("")
ostring = "" ostring = ""
if("actions" in self.nodes[self.current].keys()):
for i,option in enumerate(self.nodes[self.current]["actions"]): for i,option in enumerate(self.nodes[self.current]["actions"]):
ostring+=f"{i} - {self.nodes[option]['description']}\n" ostring+=f"{i} - {self.nodes[option]['description']}\n"
print(ostring) print(ostring)
sel = input("Make a selection (number): ") sel = input("Make a selection (number): ")
isWrong = self.make_selection(sel) isWrong = self.make_selection(int(sel))
while isWrong == False: while isWrong == False:
sel = input("Make a selection (number): ") sel = input("Make a selection (number): ")
isWrong = self.make_selection(sel) isWrong = self.make_selection(sel)
@ -41,12 +42,19 @@ class Game:
''' '''
Used to convert color codes in string to colors from the colorama lib 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): def load(file_path):
'''Loads the game from a YAML file to a Game class''' '''Loads the game from a YAML file to a Game class'''
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)
g = Game(data["meta"]["name"],data["meta"]["creator"],data["game"]) g = Game(data)
return g return g
except Exception as e:
print("An exception has occured while loading the game from the YAML file:")
print(e)
return None