Working on inventory

This commit is contained in:
Matyáš Caras 2022-01-26 16:07:31 +01:00
parent b1573d8b4a
commit 5cf11374f4
3 changed files with 38 additions and 17 deletions

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: "{welcome}&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 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
@ -14,4 +14,14 @@ game: # here goes all the game logic
sit: # if no `actions` are supplied, the game exits sit: # if no `actions` are supplied, the game exits
description: "Sit down" description: "Sit down"
text: "You quietly sit down and check the menu." text: "You quietly sit down and check the menu."
actions:
- beer
- nothing
beer:
description: "Order beer"
text: "You order some &ebeer"
add_inventory: "Beer" # add something to inventory
nothing:
description: "Do nothing"
text: "You sit and wait..."

View file

@ -1,7 +1,7 @@
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from zipfile import ZipFile from zipfile import ZipFile
from os import listdir from os import listdir
import yaml
class AsciiAnimation: class AsciiAnimation:
def __init__(self) -> None: def __init__(self) -> None:
self.frames = [] self.frames = []
@ -16,8 +16,11 @@ class AsciiAnimation:
with ZipFile(f"./assets/{name}.asc","r") as z: # extract the asc file with ZipFile(f"./assets/{name}.asc","r") as z: # extract the asc file
z.extractall(f"{tmpdir}/ascii/{name}") z.extractall(f"{tmpdir}/ascii/{name}")
for f in listdir(f"{tmpdir}/ascii/{name}"): # read all the files for f in listdir(f"{tmpdir}/ascii/{name}"): # read all the files
if f.endswith("yaml"): if f == "config.yml":
# TODO: load asc config file with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as c:
data = yaml.load(c,Loader=yaml.SafeLoader)
if(data["speed"] != None):
self.speed = data["speed"]
pass pass
with open(f"{tmpdir}/ascii/{name}/{f}") as f: # add all frames into list with open(f"{tmpdir}/ascii/{name}/{f}",encoding="utf-8") as f: # add all frames into list
self.frames.append(f.read()) self.frames.append(f.read())

View file

@ -12,16 +12,24 @@ class Game:
self.author = data["meta"]["creator"] self.author = data["meta"]["creator"]
self.current = "start" self.current = "start"
self.nodes = {} self.nodes = {}
self.inventory = []
for k in data["game"]: for k in data["game"]:
self.nodes.update({k:data["game"][k]}) self.nodes.update({k:data["game"][k]})
def make_selection(self,selection:int) -> bool: def make_selection(self) -> int:
if(type(selection) != int or selection >= len(self.nodes[self.current]["actions"]) or selection < 0): y = False
print("Invalid selection") selection = 0
return False # TODO: Check for "has_item"
else: while y == False:
self.current = self.nodes[self.current]["actions"][selection] try:
return True selection = int(input("Make a selection (number): "))
except ValueError:
print("Not a number selection")
if(selection >= len(self.nodes[self.current]["actions"]) or selection < 0):
print("Invalid selection")
else:
y = True
return selection
def print_text(self): def print_text(self):
''' '''
@ -38,11 +46,11 @@ class Game:
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 = self.make_selection()
isWrong = self.make_selection(int(sel)) if(self.nodes[self.current]["add_inventory"] is not None):
while isWrong == False: # add item to inventory
sel = input("Make a selection (number): ") self.inventory.append(self.nodes[self.current]["add_inventory"])
isWrong = self.make_selection(sel) self.current = self.nodes[self.current]["actions"][sel]
self.print_text() self.print_text()
def print_animated(self,animid): def print_animated(self,animid):