pygame.sprite.Sprite.kill()无法正常工作。为什么?

时间:2014-09-22 03:55:38

标签: python class pygame sprite

所以,我在游戏中使用这个类:

class Magazine(pygame.sprite.Sprite):
    def __init__(self, image, shooter):
        self.image = image
        self.rect = image.get_rect()
        self.rect.x = shooter.rect.x
        self.rect.y = shooter.rect.y - shooter.image.get_height()

    def update(self):
        self.rect.y -= 5
        if self.rect.y <= 0 - self.image.get_height():
            self.kill()

在我创建这个类之后,我创建了该类的一个项目。然后我调用了它的更新功能:

magazine = Magazine(magazineImage, forry)
magazine.update()

出于某种原因,我收到了这个错误:

Traceback (most recent call last):
  File "main.py", line 106, in <module>
    main()
  File "main.py", line 78, in main
    projectileObject.update()
  File "/Users/number1son100/Desktop/Famous Monsters Game/gameobjects.py", line 117, in update
    self.kill()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sprite.py", line 174, in kill
    for c in self.__g.keys():
AttributeError: 'Magazine' object has no attribute '_Sprite__g'

1 个答案:

答案 0 :(得分:1)

好的,所以你需要做的就是,如果要使用sprite类附带的函数,首先必须插入:

super(spritename, self).init()

因此,您需要插入:

super(Magazine, self).init()

进入你的init函数。