2014-11-05 66 views
0

新手問題。有人可以幫我理解下面發生的事情嗎?python多實例化並傳遞值

這一切都是從學習Python硬盤的方式ex43 - http://learnpythonthehardway.org/book/ex43.html

如何類引擎知道第一個「場景」列表是從類地圖? 該交接發生在哪裏?

我也不確定引擎和Map類如何通信。我發現它在底部實例化,但是有可能有實例化對象(a_map),然後再次實例化該對象(通過a_game)?

實施例:

a_map = Map('1') 
a_game = Engine(a_map) 

以下是完整的代碼。

class Engine(object): 

    def __init__(self, scene_map): 
     self.scene_map = scene_map 

    def play(self): 
     current_scene = self.scene_map.opening_scene() 
     last_scene = self.scene_map.next_scene('2') 


     while current_scene != last_scene: 
      next_scene_name = current_scene.enter() 
      current_scene = self.scene_map.next_scene(next_scene_name) 


class FristLevel(object): 

    def enter(self): 
    pass 

class SecondLevel(object): 

    def enter(self): 
    pass 

class Map(object): 

    scenes = {'1' : FristLevel(), 
    '2' : SecondLevel()} 

    def __init__(self, start_scene): 
     self.start_scene = start_scene 


    def next_scene(self, scene_name): 
     pass 

    def opening_scene(self): 
     pass 


a_map = Map('1') 
a_game = Engine(a_map) 
a_game.play() 

回答

1

我不完全確定你的問題是什麼,但我會盡量爲你細分一下代碼。 。 。

a_map = Map('1') 

這名a_map產生的Map並存儲一個新的實例。

a_game = Engine(a_map) 

這名a_game創建的Engine並存儲一個新的實例。請注意,輸入參數是a_map。在這種情況下,Map實例將被傳遞到Engine.__init__並存儲爲scene_map屬性。在上面的例子,如果你寫:

a_game.scene_map is a_map 

結果將是True因爲它們是同一個Map實例兩個名字。

+0

好的,這有幫助,我很困惑如何變量a_game可以調用地圖場景列表中的不同項目。 – cDemeke 2014-11-05 14:17:53

+0

因此,如果我想從引擎中調用Map.scenes列表中的其他項目,我該怎麼做? – cDemeke 2014-11-05 14:19:14