2

So, I have this problem I'm working on, and if someone could point me in the right direction, I'd be so grateful. Let me set it up for you.

I have 1 Python file/module named rooms filled with classes, like so:

class Intro(object):

    def __init__(self):
         # Setting up some variables here

    def description(self):
        print "Here is the description of the room you are in"

Which is all cool and stuff right? Then on another file/module, named Engine. I have this:

import rooms

class Engine(object):

    def __init__(self, first_class):
        self.lvl = first_class

    def play_it(self):
        next_lvl = self.lvl

        while True:
            # Get the class we're in & run its description
            next_lvl.description() # it works.
            # But how do I get the next class when this class is done?

See what I would like to happen is based on the user's decision within each room/level class, the engine call upon a new class w/ its description function/attribute. Does that make sense? Or is there another way I should be thinking about this? Im ass-backwards sometimes. Thanks.

1
  • Take a look at the State Pattern... Commented May 12, 2012 at 18:09

1 Answer 1

2

Delegate the choice of which class to use next into the rooms. Eg

class Intro(object):

    def __init__(self):
         # Setting up some variables here

    def description(self):
        print "Here is the description of the room you are in"

    def north(self):
        return Dungeon()

    def west(self):
        return Outside()

    #etc

So when the player says "go west" in the Intro room, the Engine calls room.west() and the Intro room returns the Outside room.

Hopefully that is enough of a hint! I expect you'll want to make your own design.

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.