import sys import pprint world = { # these are the rooms in the world "rooms": { "dungeon": { "description":"A cold dark room with a wooden hatch and a ladder leading up to it.", "exits": {"up": "guard_room"}, "objects":{"carrot":"a fresh orange carrot"}, }, "guard_room": { "description":"A small stone chamber with a candle burning on an empty desk. The room smells of tallow and straw.", "objects": {"quill":"An old-fashioned quill pen", "parchment":"An oddly-stained parchment paper."}, "exits" : {"down":"dungeon", "north":"stone_corridor"} }, "stone_corridor": { "description":"A dank, narrow chamber of coarse rock. Thick veins of nitre run down the walls. This way is blocked.", "objects" : {"torch":"A charred torch"}, "exits" : {"south":"guard_room"} } }, # this defines how the game will start off each time "start_room": "dungeon", "start_inventory" : {"hood":"A worn hood made of coarse, greyish fabric."}, } inventorydict = { } def userinput(): #works response = raw_input("\nPlease enter your response ") examineinput(response) def examineinput(response): if response == "quit": #works print "Thank you for playing!" sys.exit() if response == "inventory": #works if bool(inventorydict) == False: print "You are carrying:\nnothing" else: print "You are carrying: " pprint.pprint(inventorydict) if response.startswith("examine"): #works s = response.split() if s[1] in inventorydict.keys(): print inventorydict[s[1]] elif s[1] in world["rooms"][currentroom]["objects"]: print world["rooms"][currentroom]["objects"][s[1]] else: print "I don't see a %s here" % s[1] if response == "look": #works print world["rooms"][currentroom]["description"] obj = world["rooms"][currentroom]["objects"] for i in obj: i print "There is a %s here." % i if response.startswith("take"): s = response.split() if s[1] in world["rooms"][currentroom]["objects"]: a = world["rooms"][currentroom]["objects"] inventorydict.update(a) print "You take the %s " % s[1] else: print "I don't see a %s here" % s[1] if response.startswith("drop"): s = response.split() if s[1] in inventorydict.keys(): inventorydict.pop(s[1], None) print "You drop the %s " % s[1] if response == ("help"): print "List of commands: examine [object], look, take [object], drop [object], inventory" userinput() def addinventory(k,v): inventorydict[k] = v def startinventory(): #gets starting inventory from worlds dict start_inv = world["start_inventory"] for key, value in start_inv.iteritems(): addinventory(key,value) def startgame(): start_room = world["start_room"] #gets the start room global currentroom currentroom = start_room start_room_desc = world["rooms"][start_room]["description"] #gets start room description start_obj = world["rooms"][start_room]["objects"] for i in start_obj: i print start_room_desc print "There is a %s here." % i startgame() startinventory() userinput()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question