2012-05-12 26 views
2

私はこの問題を抱えています。誰かが正しい方向に向けることができれば、とても感謝しています。私はあなたのためにそれを設定しましょう。Pythonのあるクラスから別のクラスへ「移動」

私はそうのようなクラスで満たさ1 Pythonのファイル/モジュールという名前の部屋、持っている:すべてのクールなものの権利である

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" 

を?次に、Engineという名前の別のファイル/モジュール上にあります。私はこれがあります。

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? 

は、私は各部屋/レベルのクラス内でのユーザの判断に基づいて起こるしたいものを参照してください、その説明機能/属性/ wの新しいクラス時にエンジンコール。それは理にかなっていますか?それとも、私はこれについて考えなければならない別の方法がありますか?まあ、後ろ向きです。ありがとう。

+0

[State Pattern](http://en.wikipedia.org/wiki/State_pattern)を見てください... –

答えて

3

次に使用するクラスの選択を委任します。プレイヤーはイントロ室で「西に行く」と言うとき、例えば

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 

だから、エンジンはroom.west()を呼び出し、Intro部屋はOutside部屋を返します。

うまくいけば、それはヒントで十分です!私はあなた自身のデザインをしたいと思うでしょう。

関連する問題