It's my belief that you're thinking a little too directly, nisim777. You've created some code to build a specific adventure game, when what you really want to do is to take a step back and consider the problem more generally.
You've divided each room into a specific function, but since each room will have many common attributes, I think you'll find that as your program grows more complex, you'll find yourself repeating yourself more and more.
Instead of beginning by building a specific room like a kitchen or a foyer, start by considering the behaviour of a generic room.
class Room:
def __init__(self, description):
self.description = description
self.exits = {}
A class is like a template. We can use this template to create more specific objects:
foyer = Room("You are in the foyer of the house...")
kitchen = Room("You step into a dark kitchen...")
foyer.exits["west"] = kitchen
kitchen.exits["east"] = foyer
In order to interact with these objects, we need a player:
class Player:
def __init__(self, room):
self.room = room
def look(self):
print self.room.description
def move(self, direction):
self.room = self.room.exits[direction]
me = Player(foyer)
Now we have two classes (Room and Player), and three objects (foyer, kitchen and me). The neatest thing about this arrangement is that we can actually play the game via Python's interactive interpreter:
>>> me.look()
You are in the foyer of the house...
>>> me.move("west")
>>> me.look()
You step into a dark kitchen...
>>> me.move("east")
>>> me.look()
You are in the foyer of the house... Okay, so eventually we'd need to create our own command handler (so we could type "look" rather than "me.look()"), but you can see that the functionality is already there. In fact, we can play and debug the game at the same time:
>>> me.move("up")
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
me.move("up")
File "C:/Python25/t.py", line 20, in move
self.room = self.room.exits[direction]
KeyError: 'up' Oops! Looks like we could use some better error handling in the "move" function:
def move(self, direction):
if direction in self.room.exits:
self.room = self.room.exits[direction]
else:
print "You cannot go in that direction!"
Now let's try the interactive interpreter:
>>> me.move("up")
You cannot go in that direction! Much better! Essentially, try representing physical things, such as rooms and items, as Python objects, and try representing commands, such as "look" and "move", as methods.