AttributeError:'class'对象没有属性'rect' [英] AttributeError: 'class' object has no attribute 'rect'
问题描述
我上了这堂课:
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)
但是当我这样做时,我会收到此错误:
But when i do I get this error:
文件",第36行,在comet1.rect.x = 100
File "", line 36, in comet1.rect.x = 100
AttributeError:"comet"对象没有属性"rect"
AttributeError: 'comet' object has no attribute 'rect'
即使上课了(我想我对上课还是有点陌生的)
Even though the class does (I think, i'm kinda new to classes)
推荐答案
您对 __ init __
的定义错误.您使用了三个下划线( ___ init ___
),因此在实例化类时不会调用此函数.
Your definition for __init__
is wrong. You used three underscores (___init___
), so this function is not called during instantiation of the class.
这意味着不会调用 self.rect = self.image.get_rect()
行,因此没有 rect
属性.
This means that the line self.rect = self.image.get_rect()
isn't called and so there is no rect
attribute.
这篇关于AttributeError:'class'对象没有属性'rect'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!