user884542
user884542

Reputation:

"Moving" from one class to another in Python

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.

Upvotes: 2

Views: 2209

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54107

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.

Upvotes: 2

Related Questions