Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericoporto/b47bdf3c5349d50f1c5a to your computer and use it in GitHub Desktop.
Save ericoporto/b47bdf3c5349d50f1c5a to your computer and use it in GitHub Desktop.
This is a unfinished text game I helped a random dude make in stackoverflow. It has nice ideas for text game implementation.
# one nice dude in the internet asked me for help to make this code work
# I leave this code here as a reminder of that nice dude. The content here
# belongs to nice dude, and not me.
import sys, traceback
import json
userQuited = False
def _quit(dummy):
global userQuited
userQuited = True
def _save(dummy):
f=open("game.sav",'w+')
json.dump(states, f)
f.close
def _continue(dummy):
f=open("game.sav",'r+')
states = json.load(f)
f.close
def user(name):
states["player_name"] = name
states = {
"player_health" : 100,
"player_name" : "",
"health_potion" : 0,
"big_wrench" : 0,
"blue_keycard" : 0,
"current_page" : "page1"
}
book = {
"page1": {
"text": """You are in an underwater research facility that has been
attacked by unknown assailants. As the assistant to the head
researcher, you were hired in to assist Dr. Weathers with
the Lightning Sword project. Project Lightning Sword still
remains in large a mystery to you. Although you worked closely
with the doctor you were never allowed to see the big picture.
Right now that's the least of your worries, as chaos ensues around you.
Alarms were triggered a while ago. As you sit in your room,
you hear short bursts of automatic rifle fire and only one
thing is for certain, you have to survive.\n """,
"inputText": "What would you like your player to be named?",
"function": user,
"next": "page2"
},
"page2": {
"text": "Welcome, %(player_name)s please choose what you would like to do",
"inputText": "0 = Quit, 1 = Start New Game, 2 = Continue",
"choices": {
"0" : "Quit",
"1" : "page3",
"2" : "Continue"
}
},
"page3" : {
"text" : """You are standing in your room, door is locked and you're wondering
if you will get out of this alive. The gunfire keeps getting closer and you know that you
can't stay here, you have to make a move and try and get out. Otherwise it's just a matter
of time before they find you and that option doesn't look very promising.\n\n
You are ready to try your escape but first you should try and get any useful
things from your room to use along the way. Your room-office is a bit bigger than a walk in closet,
textile flooring and gray walls encompas your view and the only things in the room are the bed you
are sitting on (not very comfortable, looks more like a prisoners bed), a framed picture on the wall
and your work desk which has barely enough space for your equipment and computer.""",
"inputText": "1 = Walk over to the picture frame, 2 = Walk over to the desk, 3 = Exit the door",
"choices": {
"1" : "page4",
"2" : "page5",
"3" : "page7"
}
},
"Quit" : {
"text": "goodbye!",
"inputText": "",
"function" : _quit
},
"Continue" : {
"text": "welcome back!",
"inputText": "",
"function" : _continue
}
}
def processPage(page):
answer = ""
print(page["text"] % states)
if len(page["inputText"]) > 1 :
answer = raw_input(page["inputText"] % states)
if "function" in page:
page["function"](answer)
if "next" in page:
return page["next"]
if "choices" in page:
for choice in page["choices"]:
if choice == answer:
print page["choices"][choice]
return page["choices"][choice]
return ""
def main():
global userQuited
while(userQuited==False):
states["current_page"] = processPage(book[states["current_page"] ])
if (states["current_page"] != "Quit" and states["current_page"] != "Continue"):
_save("")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment