Thread: Why OOP?
View Single Post
Old Oct 5th, 2007, 11:45 PM   #20
nisim777
Newbie
 
Join Date: May 2005
Posts: 17
Rep Power: 0 nisim777 is on a distinguished road
re:

Here is what I've done with what you have given me, thus far:

class Room(object):
    def __init__(self, description):
        self.description = description
        self.exits = {}

class Player(object):
    def __init__(self, room):
        self.room = room

    def look(self):
        print self.room.description

    def move(self, direction):
        if direction in self.room.exits:
            self.room = self.room.exits[direction]
            me.look()
        else:
            print "You cannot go in that direction."





foyer = Room("You are in the foyer of a house...")
kitchen = Room("You are in a dark kitchen...")

foyer.exits["west"] = kitchen
kitchen.exits["east"] = foyer


me = Player(foyer)

while True:
    choice = raw_input("What would you like to do? ").lower()
    if choice == "look":
        me.look()
    elif choice == "n":
        me.move("north")
    elif choice == "w":
        me.move("west")
    elif choice == "e":
        me.move("east")
    elif choice == "s":
        me.move("south")

I'll keep you posted on my progress. The next thing I am going to work on is an items class so the user can examine certain things in a room.
nisim777 is offline   Reply With Quote