我有这个课:
class Comet(pygame.sprite.Sprite):
def ___init___(self):
super().__init__()
self.image = pygame.Surface((100,250))
self.image.fill(cometL)
self.rect = self.image.get_rect()
self.rect.y = 800
self.rect.x = 100
def update(self):
self.rect.y -= 1+2*speed
def reset(self):
self.rect.x = randint(0,700)
self.rect.y = randint(1000,2000)
我尝试在这里使用它:
comet1 = Comet()
comet1.rect.x = 100
comet1.rect.y = 800
comets.add(comet1)
但是当我这样做的时候我得到这个错误:
文件“”,第36行,位于comet1.rect.x = 100
AttributeError:“ comet”对象没有属性“ rect”
即使上课了(我想我对上课还是有点陌生的)
您对的定义__init__
是错误的。您使用了三个下划线(___init___
),因此在实例化类时不会调用此函数。
这意味着self.rect = self.image.get_rect()
未调用该行,因此没有rect
属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句