From 4055c92353dc98f94ad328422b6b2ce71bf9a583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Tue, 2 Nov 2021 17:40:54 +0100 Subject: [PATCH] Base --- .gitignore | 2 ++ README.md | 17 ++++++++++++++++- __main__.py | 14 ++++++++++++++ example.yml | 17 +++++++++++++++++ loader.py | 37 +++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 __main__.py create mode 100644 example.yml create mode 100644 loader.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index c0c219b..a3ca28d 100644 --- a/.gitignore +++ b/.gitignore @@ -122,3 +122,5 @@ dmypy.json # Pyre type checker .pyre/ + +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 61f5687..dfd60ef 100644 --- a/README.md +++ b/README.md @@ -1 +1,16 @@ -texty +# texty +An extremely simple text-adventure game engine(?) + +## What is this? +I was bored + +## How do I make a game +Check out the `example.yml` file, all games are in the `YAML` format. + +## How do I use this +Download/clone the repo and run the `__main__.py` file with the path to a game in `YAML` format as the first argument. + +## TODO +- Colors +- Catching errors while parsing YAML +- Maybe a welcome screen instead of the need to enter the path as argument \ No newline at end of file diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..ce7bf14 --- /dev/null +++ b/__main__.py @@ -0,0 +1,14 @@ +from sys import argv +from loader import * + +def main(): + if len(argv)<2: + print("You need to specify a path to a YAML file") + exit(1) + else: + game = load(argv[1]) + game.printme() + +if __name__ == "__main__": + main() + diff --git a/example.yml b/example.yml new file mode 100644 index 0000000..a565366 --- /dev/null +++ b/example.yml @@ -0,0 +1,17 @@ +meta: # make sure every key is in lowercase + name: "My Text Adventure" # Game name + creator: "hernik" # Your name as a creator + +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 + actions: # here you add a list of actions that are inside of `game`, the user can select them + - wave + - sit + wave: + description: "Wave" # this appears in the selection box, if no description is supplied + text: "You wave at the customers to signal your arrival. They all ignore you and look away." + sit: # if no `actions` are supplied, the game exits + description: "Sit down" + text: "You quietly sit down and check the menu." + diff --git a/loader.py b/loader.py new file mode 100644 index 0000000..01ba816 --- /dev/null +++ b/loader.py @@ -0,0 +1,37 @@ +import yaml +from yaml.loader import SafeLoader + +class Game: + def __init__(self,name:str,author:str,game:dict): + self.name = name + self.author = author + self.current = "start" + self.nodes = {} + for k in game: + self.nodes.update({k:game[k]}) + + def make_selection(self,selection): + if(selection >= len(self.nodes[self.current][selection]) or selection < 0): + print("Invalid selection") + else: + self.current = self.nodes[self.current][selection] + + + def printme(self): + ''' + Used to print out the current prompt with the options + ''' + 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): ") + +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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ef9e486 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pyyaml==6.0 \ No newline at end of file