出现类似错误AttributeError: ‘MySprite‘ object has no attribute ‘image‘的解决方法

出现这种错误的原因其实就是没有给pygame.sprite.Sprite这个类的image属性赋上有用的值,因为对于pygame.sprite.Sprite,最重要的属性就是rect属性和image属性,因此当我们要扩展pygame.sprite.Sprite这个类的时候,可以在其__init__方法中这样做:

def __init__(self, image_file):
	self.image = pygame.image.load(image_file).convert_alpha()
    self.rect = self.image.get_rect()

即根据传来的图片文件参数,将rect属性和image属性设置为有效的值,这样之后就不会出现AttributeError: ‘MySprite’ object has no attribute 'image’这类的错误了

你可能感兴趣的:(Pygame,pygame,python)