2015-12-29 29 views
-2

我在一個遊戲中工作,並得到一個奇怪的錯誤,其中實例變量沒有正確保存(據我所知)。當我嘗試從RunGame實例訪問self.game時,會發生這種情況。順便說一下這些類是在不同的模塊中,並且我不會顯示所有的代碼。遊戲運行良好,但切換級別時,它只是崩潰在非屬性變量上獲取屬性錯誤

class RunGame(object): 

    def __init__(self): 
     self.makeTk() 
     self.currentLevel = 0 
     self.bricksLayout = [   #for debugging: a layout of bricks. 3d array with each level 
      [ 
        [0,1,0,1,1,1,1,1], 
        [0,0,0,0,4,1,1,1], 
        [4,4,4,4,4,4,4,4] 
       ], 

       [ 
        [4,0,0,1,0,1,0,1], 
        [0,0,0,0,0,1,0,1], 
        [4,4,4,4,4,4,4,4] 
       ] 

      ] 

     self.game = GameInstance(self.bricksLayout,self) 
     self.game.run = False  #this does nothing for some reason, no error though 

    def switchLevel(self): 
     print("switching level") 
     self.game.run = False  #calling this will give no attribute error 

     #self.game.clearCanvas() 
     #self.game.canvas.destroy() 
     self.currentLevel+=1 
     #self.game = Game(self.bricksLayout,self) 

    def makeTk(self): 
     self.root = Tk() 
     self.root.title("PrakeOut") 
     self.screen_width = self.root.winfo_screenwidth(); #monitor size 
     self.screen_height = self.root.winfo_screenheight(); 
     #root.wm_attributes("-fullscreen",True)  #this gives fullscreen, but removes the menu 
     self.root.resizable(0,0) #cant be resized 
     self.root.configure(background="Black")  #bg color of root windw 
     #root.geometry("%ix%i+0+0" % (SCREEN_WIDTH, SCREEN_HEIGHT)) #maximizes screen. First two are width and height, other two are x1 and y1 
     #root.wm_attributes("-topmost",1); #places window in foreground 

run = RunGame() 

class GameInstance(object): 


    def __init__(self, bricksLayout,gameHandler): 
     self.bricksLayout = bricksLayout 
     self.gameHandler = gameHandler 
     self.run = True  #game loop 

     self.makeCanvas() 

     self.brickList = [] #the list of all da created bricks 
     self.makeBricks() 
     self.makeBallAndPaddle() 

     self.gameLoop() 
     self.root.mainloop(); #needs to run main loop otherwise windows doesnt show 

它給出了這樣的錯誤:

文件「C:\用戶\博倫\桌面\ Python項目\突圍\ PythonApplication1 \ PythonApplication1 \ GameHandler。 py「,第30行,在switchLevel self.game.run = False #calling這將不會給屬性錯誤 AttributeError:'RunGame'對象沒有屬性'遊戲' 按任意鍵繼續。 。 。

+0

你的代碼張貼不能給你說的錯誤,因爲它有其他錯誤。 –

+0

這非常有幫助。 –

回答

0

我們不能使用您的代碼來重現問題,但在您的代碼中有一個非常明顯的邏輯錯誤。問題在於,在使用它們之前,您的任何類都不能被完全創建。

GameInstance看看:因爲你在初始化中調用mainloop,該類永遠不會完全實例根窗口已創建後,然後銷燬到。因此,任何嘗試使用此實例的其他類都將與未完全形成的實例一起工作。

同樣,與GameRunner您正在創建一個GameInstance的實例。因爲GameInstance調用mainloop,所以在創建GameInstance之前無法完成GameRunner對象的創建,並且GameInstance不會在窗口銷燬之前完成。

第一步是從GameInstance.__init__中刪除對mainloop的呼叫。相反,將其轉移到GameRunner完全創建後可調用的方法中。

您還應該將兩個對象的創建分爲兩個步驟。您的主要邏輯將如下所示./我不確定是應該先創建跑步者還是先創建遊戲,但根據您如何定義類,任何一種方式都是可接受的。

game = GameInstance(...) 
runner = GameRunner(..., game) 
runner.run() 

在上面,runner.run()將是這個樣子:

class GameRunner(...): 
    def run(self): 
     self.game.run() 

...和game.run應該是這樣的:

class GameInstance(...): 
    def run(self): 
     self.gameLoop() 
     self.root.mainloop()