#!/usr/bin/python # TYPE:game # DESC:Skeleton of a nifty text-adventure game. # AUTHOR: ShadeTheif import string import textwrap # Command Lists Initialization C_syno = { 'yes' : ['y', 'yes', 'yep', 'positive', 'affirmative', 'of course', 'yeah', 'sure', 'ja'], 'no' : ['no', 'nope', 'negative', 'no way', 'nah', 'nein'], 'quit' : ['quit', 'exit'], 'load' : ['load'], 'save' : ['save'], 'help' : ['help', 'help me!', '?!'], 'look' : ['look', 'l'], 'make' : ['make'], 'inspect' : ['inspect', 'examine', 'investigate'], 'use' : ['use'], 'open' : ['open'], 'close' : ['close'], 'unlock' : ['unlock'], 'lock' : ['lock'], 'n' : ['n', 'north'], 's' : ['s', 'south'], 'e' : ['e', 'east'], 'w' : ['w', 'west'], 'u' : ['u', 'up'], 'd' : ['d', 'down'], 'in' : ['in', 'enter'], 'out' : ['out'], '' : [''], } C_system = C_syno['quit'] + C_syno['load'] + C_syno['save'] + C_syno['help'] + C_syno['look'] + C_syno['make'] C_user = C_syno['inspect'] + C_syno['use'] + C_syno['open'] + C_syno['close'] + C_syno['unlock'] + C_syno['lock'] C_navigation = C_syno['n'] + C_syno['s'] + C_syno['e'] + C_syno['w'] + C_syno['u'] + C_syno['d'] + C_syno['in'] + C_syno['out'] C_standard = C_navigation + C_system # In-Play Variable Initialization player = {'room' : [0,0,0], 'position':'standing'} L_rooms = { (-1,0,0): {'name':'The Kitchen', 'exits':['e'], 'desc':'You are ' + player['position'] + ' in a sunny kitchen in front of a wide picture window. The sunlight shining in from the window illuminates a freshly baked pan of muffins, perched on top of an old white kerosene stove. To the east an open doorway leads into the den.'}, (0,0,0): {'name':'The Den', 'exits':['e','w'], 'desc':'You are ' + player['position'] + ' facing an open window that looks out on the sunny pastures to the south of the house. To your left is an unlit fireplace with a large portrait of an old couple on the mantle. You can tell that someone has been baking by the scents wafting in from the open kitchen door on your right. Behind you is well-used armchair and a tall dusty bookcase.'}, (1,0,0): {'name':'The Fireplace', 'exits':['w','u'], 'desc':'You are crouched inside the fireplace. Looking out into the room you can see through a doorway into the kitchen.'}, (1,0,1): {'name':'The Chimney', 'exits':['d'], 'desc':'You are wedged a few yards up the chimney, with your back against the soot coated interior and your feet braced against the opposite side. Below, you can see light from the fireplace opening shining against the back wall, and above you is a small square of bright blue sky visible through the vent'}, } def get_command(messageString='', validAnswers=C_standard, errorString='Huh?'): while 1: if messageString == '': command = string.lower(str(raw_input('> '))) else: command = string.lower(str(raw_input(messageString + '\n> '))) if (command in validAnswers) or (validAnswers == 'any'): return command else: print errorString def make(): k = get_command('Key:', player.keys()) v = raw_input('Value:\n> ') player[k] = v def look(room): print textwrap.fill(L_rooms[tuple(room)]['desc']) + "\nExits: " + str(L_rooms[tuple(room)]['exits']) def define(command): for k,v in C_syno.iteritems(): if command in v: return k def navigation(command, room=list(player['room'])): if command in L_rooms[tuple(room)]['exits']: if command == 'n': room[1] = room[1] + 1 elif command == 's': room[1] = room[1] - 1 elif command == 'e': room[0] = room[0] + 1 elif command == 'w': room[0] = room[0] - 1 elif command == 'u': room[2] = room[2] + 1 elif command == 'd': room[2] = room[2] - 1 look(room) return room else: print "There is no exit in that direction." return room print 'Welcome to Crawl...' # Main Loop while 1: command = define(get_command(validAnswers = C_standard)) if command == 'quit': command = define(get_command('Are you sure you want to quit?', C_syno['yes'] + C_syno['no'], 'Please enter "yes" or "no."')) if command == 'yes': break elif command in C_navigation: print 'nav' player['room'] = navigation(command) elif command == 'look': look(player['room']) elif command == 'make': make() # Testing Feedback print 'command = ' + str(command) print 'player = ' + str(player)