2012-05-12 57 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

看看[狀態模式](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房間。

希望這是足夠的提示!我希望你會想做出自己的設計。

相關問題