亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不明白應該從父類繼承的這個 AttributeError 的原因

不明白應該從父類繼承的這個 AttributeError 的原因

繁花不似錦 2023-06-20 14:22:04
我對 OOP 和 Pygame 比較陌生,一直在嘗試編寫塔防游戲,但遇到了障礙。我不斷收到此錯誤:AttributeError: type object 'Game' has no attribute 'path'我曾嘗試閱讀有相同問題的人的帖子,但似乎沒有任何修復對我有用。我試圖在我的游戲中有幾個不同的級別,它們具有不同的背景,因此具有不同的路徑。我試圖通過創建一個父Game類然后為每個級別創建一個子類來做到這一點。(我程序中的每個類都有不同的 .py 文件。)理想情況下,每個關卡子類都有自己的path屬性,該屬性將覆蓋path游戲類中的屬性。然后將其path傳遞到我的Enemy班級,其中有使敵人跟隨路徑的代碼。我可以通過將self.path(在我的游戲類中)放在構造函數之上并將其定義為path. 但是在這樣做時,我無法或不知道如何覆蓋子類中的屬性。此外,在我的敵人類中,我試圖通過將游戲類放在較低的位置來規避與游戲類進行循環導入的問題,我認為這可能與它有關,但是,我不確定。如果是這種情況,是否有更好的方法讓我的敵人班級訪問該路徑?這是我的關卡選擇文件的相關代碼:# If button is pressed then execute its corresponding functionif event.type == pygame.MOUSEBUTTONDOWN:    # If level 1 button is pressed then instantiate Level 1    if level1.buttonPress(pos):        level1_class = Level1(self.screen)        # Runs the main game loop for the instantiated level        level1_class.run()這是我班級的相關代碼Enemy:import pygamelightGreen = (0, 255, 0)red = (200, 0, 0)# Creates Enemy classclass Enemy(pygame.sprite.Sprite):    imgs = []    def __init__(self):        pygame.sprite.Sprite.__init__(self)        self.width = 150        self.height = 150        self.max_health = 100        self.health = 100        self.path = [(0, 0)]        self.x = self.path[0][0]        self.y = self.path[0][1]        self.img = None        self.animation = 0        self.speed = 1        self.i = 1        self.pos_check = 0    # Draws the sprite to the screen    def draw(self, screen):        # Only works for the first time it is called        if self.pos_check == 0:            self.pos_check += 1            # Sets starting x and y as the first co-ordinates of the path            from Game import Game            self.x = Game.path[0][0]            self.y = Game.path[0][1]        # Chooses an image from a list of images based on the number of self.animation        self.img = self.imgs[self.animation]        # Draws image        screen.blit(self.img, (self.x - self.width / 2, self.y - self.height / 2))        # Draws health bar        self.draw_health_bar(screen)
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

問題是Game對象永遠不會被實例化——也就是說,只有 的定義,而不是調用函數的Game變量版本“副本” 。Game.__init__()顯然,在調用 Game 初始化程序之前,成員變量game.path不存在(因為它是在 中定義的__init__())。


有兩種解決方法。第一種是使Game對象的成員成為純靜態的:


class Game:

    path = [(-30, 783), (0, 783), (271, 767), (369, 471), (566, 414), (625, 352), (699, 138), (856, 93), (1206, 93), (1400, 46), (1500, 97), (1759, 97), (1784, 311), (1622, 434), (1487, 734), (1670, 789), (1756, 842), (1782, 1016), (1782, 1200)]


    def __init__(self, screen):

        self.path = 

        self.enemies = None

        self.towers = None

這允許Game.path獨立于任何初始化自由訪問。但是看看你班上的其他人,這似乎不是它設計的工作方式。


因此,更好的方法是簡單地實例化一個Game對象:


import Game


...


game = Game()   # Create an instantiated Game object.


...


    # Sets starting x and y as the first co-ordinates of the path

    self.x = game.path[0][0]

    self.y = game.path[0][1]

此處似乎對 Python Object Instantiation 進行了合理的描述。如果您不熟悉面向對象的概念,那么花時間閱讀它可能是值得的。


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號