Skip to content

Instantly share code, notes, and snippets.

@mirnovov

mirnovov/dai.py Secret

Created Oct 28, 2020
Embed
What would you like to do?
DAI - A very small one-file Python interactive fiction module with a very simplistic set of features.
# __ __ _
# | \/ \| |
# | | | | | |
# |__/|/ \|_|
#
# by novov
title = 'No title'
author = 'None'
tracking = True
index = [] #A list of all rooms. doesn't show actual object names, but still works as intended
history = [] #All rooms visited.
textfunc = 'p_text'
source = None
pgcount = 0
class pg:
def __init__(self,name,text = 'There is no text added yet',func = None,track = True,view = True):
global index
index.append(self)
self.name = name
self.text = text
self.func = func
self.track = track
self.view = view
self.links = []
self.objects = []
def link(self,label,dest = 'p_empty',keyword = None,view = True):
self.links.append([label,dest,keyword,view,None])
def start(*places,dest = 'startpg'):
global currentpg
for x in places:
try: globals().update(x)
except: pass
currentpg = eval(dest)
return run()
def run():
global currentpg, allpg, index, history, allfunc, textfunc, pgcount, source, glldiin
if currentpg.track is True:
pgcount += 1
history.append(currentpg)
for x in [allpg.func, currentpg.func]:
if type(x) is str: p_getfunc(x)
elif type(x) is list:
for y in x: p_getfunc(y)
if currentpg.view is True:
for x in p_getfunc(textfunc): print(x)
while True:
prompt = input('> ')
for x in currentpg.links + allpg.links:
if x[1] =='p_history' and tracking is False: continue
elif prompt == x[4]:
source = x
try: currentpg = eval(x[1])
except TypeError: currentpg = x[1]
except NameError: currentpg = p_empty
return run()
else: print('input not valid')
def p_getfunc(name):
if '(' not in name: name += '()'
return eval(name)
def p_text():
global currentpg, allpg, title, author
output = []
output.append('======= {:} | {:} by {:} ======='.format(currentpg.name,title,author))
output.append(currentpg.text)
for x, y in enumerate(currentpg.links + allpg.links):
if y[2] is None: y[4] = str(x+1)
else: y[4] = y[2]
if y[3] is True: output.append('[{:}] {:}'.format(y[4],y[0]))
return output
allpg = pg(' ')
allpg.link(' ',dest='p_history',keyword ='history',view = False)
allpg.link(' ',dest='p_exitbay', keyword = 'exit', view = False)
p_empty = pg('Room not found',text='The author failed to add this room',track = False)
p_empty.link('Return to the last page',dest='history[-1]')
p_history = pg('History',text='Pages you have visited previously:',func='p_histlinks',track = False)
p_exitbay = pg(' ',func='p_exit(0)',track = False, view = False)
def p_histlinks():
global history, p_history
p_history.links = []
for x in history:
p_history.link(x.name,dest=x)
def p_exit(code):
raise SystemExit(code)
from dai import *
title = 'The Lobby'
author = 'novov'
lobby = pg('Lobby')
lobby.text = 'You happen to be in a fancy lobby. To your left and right are doors.'
lobby.link('Enter the door on your left',dest='green')
lobby.link('Enter the door on your right',dest='red')
green = pg('Green Room')
green.text='You are now in a room padded wall-to ceiling with green cushioning. There is no furniture here.'
green.link('Go back',dest='lobby')
start(globals(),dest='lobby')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment