Gingerbread Man
Dark Magus
I made a text adventure game! However, I did read ahead a little - I put dictionaries inside dictionaries, which I never said you could do, and I did a bit of error handling. Here is the source code, and attached is the file it is stored in:
WAH???? - You may say this. I'll be writing an explaination to the entire program, in the hope that you can glean some info from the program.
box.txt is the program. To run it:
1) go to the command prompt (a.k.a. MSDOS) of your computer
2) Type in cd C:\Path\to\where\you\saved\the\file\
3) Type in python box.txt
There are many bugs with it, but nothing that will cause problems. The only real problem is that it may have problems figuring out what you are referring to. As a general guide -
1) mess around.
2) Type 'smell', 'taste', 'touch', 'look', or 'listen' in order to do those things to the place you have just found yourself in.
Lets just say when you typed 'touch' it printed:
in order to smell the sides you would type:
> smell sides
3) Anything in capitols, is something you can smell, taste, look at, touch or listen to.
4) type everything in lowercase.
Code:
#THE BOX
#Python program by Steven Thurlow
#Property of the Civfanatics.com forums
#The menu function
#make sure every menu entry is unique otherwise it
#will show the index of the first entry it appears in
def menu(list, question):
for entry in list:
print list.index(entry) + 1,
print ") " + entry
return input(question) - 1
#The input processing function
def command(input):
output = []
print ""
text = raw_input(input + " ") + " "
space = 0
letter = 0
count = 0
while count < len(text):
if text[count] == " ":
output.append(text[space:count])
space = count + 1
count = count + 1
end = output[0]
del output[0]
output.append(end)
return output
#Define default actions
actions = ['touch','listen','look','smell','taste','use','take','goto', 'leave', 'exit']
#Define the objects in the room
objects = {}
#The space's touch
objects['touch'] = \
"In the confined space you find YOURSELF. You feel a cold \n\
metal ROOF, a rickety FLOOR, a FRONT section, a BACK\n\
section, and two SIDES."
#The space's listen
objects['listen'] = \
"You hear a rumble towards the front of your confines, but it sounds\n\
distant, so you cannot learn much from it."
#The space's look
objects['look'] = \
"you can see absolutely nothing - it is pitch black. You need a \n\
source of light"
#The space's smell
objects['smell'] = \
"You can smell nothing but YOURSELF."
#The space's taste
objects['taste'] = \
"A myriad of disgusting flavours are found throughout your confines."
#Quit the game
objects['exit'] = \
"You quit the game while still stuck in the confines you found yourself in."
#yourself
objects['yourself'] = {}
objects['yourself']['touch'] = \
"You are (quite obviously) a man. You are also slightly obese."
objects['yourself']['listen'] = \
"Normal bodily functions are still occuring."
objects['yourself']['look'] = \
"You cannot see yourself."
objects['yourself']['smell'] = \
"You reek of ALCOHOL"
objects['yourself']['taste'] = \
"You taste like ALCOHOL, with a smidge of jam, with a creamy texture.\n\
You want more."
#alcohol
objects['yourself']['alcohol'] = {}
objects['yourself']['alcohol']['touch'] = \
"Your shirt appears to be soaked in the alcohol"
objects['yourself']['alcohol']['listen'] = \
"Sounds like alcohol, as usual"
objects['yourself']['alcohol']['look'] = \
"Remember? You cant see yet!"
objects['yourself']['alcohol']['smell'] = \
"The sweet aromas of a 1997 vintage, dry sparkling wine, probably \n\
grown organically in the Barossa Valley, reminds you of many fine \n\
days ago."
objects['yourself']['alcohol']['taste'] = \
"It wets your dry mouth with the flavours that it's smell suggests."
#roof
objects['roof'] = {}
objects['roof']['touch'] = \
"The roof is of cold metal, with a spot of RUST."
objects['roof']['listen'] = \
"The roof seems to be rattling"
objects['roof']['look'] = \
"You cannot see the roof."
objects['roof']['smell'] = \
"The roof smells a bit damp."
objects['roof']['taste'] = \
"Now why would you that?"
objects['roof']['rust'] = {}
objects['roof']['rust']['touch'] = \
"The rust pokes through and water trickles from the hole you made.\n\
Have a nice drink."
objects['roof']['rust']['listen'] = \
"Your ear gets damp rust all over it."
objects['roof']['rust']['look'] = \
"You cannot see the roof. You need a soure of light."
objects['roof']['rust']['smell'] = \
"The rust smells damp."
objects['roof']['rust']['taste'] = \
"Mmmm, precious, filthy, water!"
#floor
objects['floor'] = {}
objects['floor']['touch'] = \
"The floor is covered in CARPET. Not much else makes sense at the\n\
moment"
objects['floor']['listen'] = \
"The floor has a rumble to it."
objects['floor']['look'] = \
"It is pitch black. You cannot see the floor."
objects['floor']['smell'] = \
"The floor smells of your own odour."
objects['floor']['taste'] = \
"That was stupid. Why did you do that?"
#carpet
objects['floor']['carpet'] = {}
objects['floor']['carpet']['touch'] = \
"The carpet is of synthetic material. It is very worn, and could be\n\
peeled back if you were able to manouvre inside the space you are in."
objects['floor']['carpet']['listen'] = \
"The carpet prickles your ears as you try to listen to it."
objects['floor']['carpet']['look'] = \
"If you haven't realised by now, it is pitch black. You cannot see!"
objects['floor']['carpet']['smell'] = \
"The carpet smells musty and old."
objects['floor']['carpet']['taste'] = \
"You lick the carpet, and feel it pricle your tounge. ouch!"
#front
objects['front'] = {}
objects['front']['touch'] = \
"The front consists of two PANELS"
objects['front']['listen'] = \
"You can attach no sound related to the front section."
objects['front']['look'] = \
"Gee, you are dull. It is pitch black, and you cannot see."
objects['front']['smell'] = \
"You can smell something through a gap in the front section, but you\n\
can't narrow it down."
objects['front']['taste'] = \
"I expected more from you. You lick the front, and taste your own\n\
stupidity."
#panels
objects['front']['panels'] = {}
objects['front']['panels']['touch'] = \
"The panels move quite a bit, but would require a strong force to\n\
to dislodge them."
objects['front']['panels']['listen'] = \
"Your futile efforts of listening to an inanimate panel prove, rather,\n\
futile."
objects['front']['panels']['look'] = \
"Maybe you should open your eyes and see that it is pitch black inside\n\
your confines."
objects['front']['panels']['smell'] = \
"The panels smell like the panels they are."
objects['front']['panels']['taste'] = \
"What is your obsession with tasting unusual objects?"
#back
objects['back'] = {}
objects['back']['touch'] = \
"The back is of cold metal. There are WIRES running out from each\n\
corner"
objects['back']['listen'] = \
"Apart from a whir of air and the occasional thump, You do not hear\n\
much from the back"
objects['back']['look'] = \
"How much of a dimwit are you? You cannot see, it is too dark."
objects['back']['smell'] = \
"You attach no smell with the back."
objects['back']['taste'] = \
"It doesn't taste very nice. A little like cold metal, you could say."
#wires
objects['back']['wires'] = {}
objects['back']['wires']['touch'] = \
"The wires in each corner seem to attach to GLOBEs of some sort,\n\
which are built into the back wall."
objects['back']['wires']['listen'] = \
"It is a little hard to listen to electricity, but you still try.\n\
and fail."
objects['back']['wires']['look'] = \
"You cannot see the wires."
objects['back']['wires']['smell'] = \
"You can smell the plastic insullation on the wires."
objects['back']['wires']['taste'] = \
"Your tounge gets a nasty zap when you press the wires to them."
#sides
objects['sides'] = {}
objects['sides']['touch'] = \
"There is nothing interesting about the sides of your confines."
objects['sides']['listen'] = \
"You feel a low rumble."
objects['sides']['look'] = \
"You cannot see."
objects['sides']['smell'] = \
"The sides smell strongly of oil. You can't tell by the smell, what\n\
type it is."
objects['sides']['taste'] = \
"The sides taste of engine oil. Your tounge feels awful now."
#Introduction
print \
"Welcome to 'The Box', a text adventure game written in Python by\n\
Steven Thurlow.\n\n\
* * * * *\n\
You don't know how, you don't know why, but you wake up, and find\n\
yourself confined inside a box of some sort. The aim is to find\n\
out where you are.\n\n\
You can make the following commands, to any of the objects you see,\n\
or just type them in for a generic description of the area:\n\
touch\n\
listen\n\
look\n\
smell\n\
taste\n\n\
You may also type exit at any time, by typing \'exit\'.\n\n\
This game is still in development, and as such is a little difficult\n\
to use. It understands context now, however. The context is whatever\n\
information was posted last."
location = objects
loop = 1
lastinput = []
while loop == 1:
noprint = 0
location = objects
input = command(">")
if input [-1] not in actions:
print "Sorry, I don't understand the command \'" + input[-1] + "\'."
continue
if input[0] == "exit":
print objects['exit']
loop = 0
noprint = 1
break
newcommand = lastinput
for word in input:
newcommand.append(word)
if len(input) <= 1:
newcommand = input
try:
for entry in newcommand:
location = location[entry]
except:
for entry in input:
try:
location = location[entry]
except:
print "Sorry, I don't recognise the object \'" + entry + "\'."
noprint = 1
break
if noprint == 0:
print location
lastinput = input[:-1]
box.txt is the program. To run it:
1) go to the command prompt (a.k.a. MSDOS) of your computer
2) Type in cd C:\Path\to\where\you\saved\the\file\
3) Type in python box.txt
There are many bugs with it, but nothing that will cause problems. The only real problem is that it may have problems figuring out what you are referring to. As a general guide -
1) mess around.
2) Type 'smell', 'taste', 'touch', 'look', or 'listen' in order to do those things to the place you have just found yourself in.
Lets just say when you typed 'touch' it printed:
Code:
In the confined space you find YOURSELF. You feel a cold
metal ROOF, a rickety FLOOR, a FRONT section, a BACK
section, and two SIDES.
> smell sides
3) Anything in capitols, is something you can smell, taste, look at, touch or listen to.
4) type everything in lowercase.