0

I've been trying to make an open-ended text adventure. I tried to set up nine rooms that you can move around in, but I think the way I did it is not possible in python. Is there any similar way I could do it? A workaround? Here's the code: (By the way I know there is no way for the user to interact yet)

class Room:
    def __init__(self, north, east, south, west, desc):
        self.north = north
        self.east = east
        self.south = south
        self.west = west
        self.desc = desc

loc1 = Room(False, loc2, loc4, False, "You can go east and south")
loc2 = Room(False, loc3, loc5, loc1, "You can go east, south, and west")
loc3 = Room(False, False, loc6, loc2, "You can go west and south")
loc4 = Room(loc1, loc5, loc7, False, "You can go north, east, and south")
loc5 = Room(loc2, loc6, loc8, loc4, "You are in the center room, with paths in all directions")
loc6 = Room(loc3, False, loc9, loc5, "You can go north, west, and south")
loc7 = Room(loc4, loc8, False, False, "You can go north and east")
loc8 = Room(loc5, loc9, False, loc7, "You can go north, east, and west")
loc9 = Room(loc6, False, False, loc8, "You can go north and west")

location = loc5

def Interpret(b):
    global location
    if b == 'help':
        print('To move around, type north, south, east and west. Make sure it\'s all in lowercase, so that the game can understand.')
    elif b == 'north':
        if location.north == False:
            print("You can't go that way.")
        else:
            location = location.north
    elif b == 'east':
        if location.east == False:
            print("You can't go that way.")
        else:
            location = location.eastth
    elif b == 'south':
        if location.south == False:
            print("You can't go that way")
        else:
            location = location.south
    elif b == 'west':
        if location.west == False:
            print("You can't go that way")
        else:
            location = location.west
1
  • Why do you think this is not possible in Python? What problem are you seeing? Commented Dec 27, 2013 at 19:01

1 Answer 1

1

The problem you're seeing is that you can't use a variable in a method call until after it has been declared. Style issues with using a variable per room aside, one way to solve it is to move the setup to a method you can call after all rooms have been created;

class Room:

    def setup(self, north, east, south, west, desc):
        self.north = north
        self.east = east
        self.south = south
        self.west = west
        self.desc = desc

loc1 = Room()
loc2 = Room()
loc3 = Room()
loc4 = Room()
...
loc9 = Room()

loc1.setup(False, loc2, loc4, False, "You can go east and south")
loc2.setup(False, loc3, loc5, loc1, "You can go east, south, and west")
loc3.setup(False, False, loc6, loc2, "You can go west and south")
loc4.setup(loc1, loc5, loc7, False, "You can go north, east, and south")
...
loc9.setup(loc6, False, False, loc8, "You can go north and west")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.