[問題] AttributeError

看板Python作者 (PatheticApathy)時間6年前 (2019/05/06 21:54), 編輯推噓1(211)
留言4則, 4人參與, 6年前最新討論串1/1
如題 目前正在練習寫一個太空船打隕石的小遊戲 以下是我main 裡面的程式碼,用來確認碰撞的 for i in range(len(game_objects)): for j in range(i+1, len(game_objects)): obj_1 = game_objects[i] obj_2 = game_objects[j] if not obj_1.dead and not obj_2.dead: if obj_1.collides_with(obj_2): obj_1.handle_collision_with(obj_2) obj_2.handle_collision_with(obj_1) for to_remove in [obj for obj in game_objects if obj.dead]: to_remove.delete() game_objects.remove(to_remove) 然後object class的裡的__init__() 如下: class PhysicalObject(pyglet.sprite.Sprite): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dead = False self.velocity_x, self.velocity_y = 0.0, 0.0 明明已經有在object 的class 設定了dead, 但當我跑程式時, 卻跑出 AttributeError: 'Sprite' object has no attribute 'dead' 請問各位大大有沒有人知道是哪裡出錯了? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.223.22.251 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1557150895.A.089.html

05/06 22:42, 6年前 , 1F
print出來看看
05/06 22:42, 1F

05/06 22:55, 6年前 , 2F
他不就說了Sprite
05/06 22:55, 2F

05/06 23:47, 6年前 , 3F
看過C++括弧少一邊的error嗎?
05/06 23:47, 3F

05/07 00:55, 6年前 , 4F
'Sprite' object has no attribute 'dead'
05/07 00:55, 4F
文章代碼(AID): #1Sq3ol29 (Python)
文章代碼(AID): #1Sq3ol29 (Python)