This commit is contained in:
Matyáš Caras 2021-11-02 17:40:54 +01:00
parent 31115ebbe9
commit 4055c92353
6 changed files with 87 additions and 1 deletions

2
.gitignore vendored
View file

@ -122,3 +122,5 @@ dmypy.json
# Pyre type checker
.pyre/
.vscode

View file

@ -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

14
__main__.py Normal file
View file

@ -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()

17
example.yml Normal file
View file

@ -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."

37
loader.py Normal file
View file

@ -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

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
pyyaml==6.0