Created
September 7, 2013 13:34
-
-
Save TylerLeite/6475613 to your computer and use it in GitHub Desktop.
Text-based game engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game: | |
def __init__(self): | |
self.flags = '1 0 0 0 0 0 0 0'.split() | |
def check_flags(self, line): | |
flags = line.split()[1].replace('flags:', '') | |
if flags == 'none': | |
return True | |
for f in range(len(flags)): | |
if flags[f] == '1' and self.flags[f] == '0': | |
return False | |
return True | |
def set_flags(self, line): | |
if 'set_flags:' in line: | |
flags = line.split()[2].replace('set_flags:', '') | |
for pos in range(len(flags)): | |
if flags[pos] == '1': | |
self.flags[pos] = '1' | |
def fmt_bold(self, line): | |
words = line.split() | |
if '[b]' in line: | |
for w in range(len(words)): | |
if '[b]' in words[w]: | |
words[w] = '\033[1m' + words[w][3:] + '\033[0m' | |
return ' '.join(words) + '\n' | |
return line | |
def get_text(self, response, filename): | |
if response == '': | |
return 'Don\'t just sit there silently, respond!\n', filename | |
found_block, quit = False, False | |
out, next_file = str(), str() | |
with open(filename) as text: | |
for line in text: | |
if response == 'main' and 'quit' in line and self.check_flags(line): | |
quit = True | |
if '$#!+' in line and 'default' in line and response != 'main' and self.check_flags(line): | |
found_block = True | |
next_file = filename | |
elif '$#!+' in line and response in line and self.check_flags(line): | |
out = str() | |
found_block = True | |
self.set_flags(line) | |
if response != 'main': | |
next_file = line.split()[-1] | |
elif '$#!+' in line and (not response in line or not self.check_flags(line)): | |
found_block = False | |
elif found_block: | |
out += self.fmt_bold(line) | |
if response != 'main': | |
return out, next_file | |
return out, quit | |
def main(self): | |
print self.get_text('main', '1.dlg')[0], | |
next_file = '1.dlg' | |
while True: | |
choice = raw_input('> ').lower() | |
if choice == 'quit' or choice == 'exit' or choice == 'stop': | |
exit(0) | |
prev_file = next_file | |
text, next_file = self.get_text(choice, next_file) | |
print '} %s' % text, | |
if prev_file != next_file: | |
text, quit = self.get_text('main', next_file) | |
print text, | |
if quit: | |
exit(0) | |
Game().main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample dialogue files
1.dlg:
$#!+ flags:none main
You wake up in the middle of the night. With eyes red with fatigue, you look at
the dim display of the alarm clock on your nightstand. It reads 1:34 A.M. You
contemplate going back to [b]sleep, but you know you should [b]get [b]up and get ready
for work.
$#!+ flags:none default
Your mind must still be fuzzy. Do you want to [b]SLEEP or [b]GET [b]UP?
$#!+ flags:none sleep 2.dlg
You fall back into a light sleep.
$#!+ flags:10000000 set_flags:00000001 sleep 2.dlg
You fall back into a deep sleep.
$#!+ flags:none set_flags:00000010 get up 3.dlg
You get up.
2.dlg:
$#!+ flags:none main 3.dlg
After a few hours of peaceful sleep, you are awoken by the ringing of your cell
phone. It's Person. You know it would be stupid not to [b]pick [b]up [b]the [b]phone, but
on the other hand, it's much more appealing to [b]sleep [b]in.
$#!+ flags:none default 3.dlg
Your mind must still be fuzzy. Do you want to [b]PICK [b]UP [b]THE [b]PHONE or [b]SLEEP [b]IN?
$#!+ flags:none pick up the phone 3.dlg
You pick up the phone.
$#!+ flags:none set_flags:01000000 sleep in 3.dlg
The phone keeps ringing.
3.dlg:
$#!+ flags:none main quit
You die of a heart attack.
$#!+ flags:00000001 main quit
Pserson: "This is something really important that I am telling you!"
You get shot and die before Pserson can tell you about the hitman.
$#!+ flags:01000000 main quit
You find it hard to sleep and decide to mute the phone. However by the time you
walk over to your dresser, you are awake enough to answer anyway. You then die.
$#!+ flags:00000010 main quit
You collapse and die.