AttributeError: 'Obstacle' object has no attribute 'image'

Viewed 21

I tried to draw obstacle on screen but got an error in return and now im stuck :/

Here is my obstacle class :

class Obstacle(pygame.sprite.Sprite):
    def __init__(self,type):
        super().__init__()
        if type == 'fly':
            self.snail_1 = pygame.image.load("graphics\\snail1.png").convert_alpha()
            self.snail_2 = pygame.image.load("graphics\\snail2.png").convert_alpha()
            self.snail_frames = [self.snail_1,self.snail_2]
            self.snail_index = 0
        else:
            self.fly_1 = pygame.image.load("graphics\\fly1.png").convert_alpha()
            self.fly_2 = pygame.image.load("graphics\\fly2.png").convert_alpha()
            self.fly_frames = [self.fly_1, self.fly_2]
            self.fly_index = 0
            self.rect = self.fly_1.get_rect(midbottom = (200,210))

Here is sprite.Group:

obstacle_group = pygame.sprite.Group()
obstacle_group.add(Obstacle('fly'))

and here is my method to draw:

obstacle_group.draw(screen)

What i'm doing wrong here ?

Traceback (most recent call last):
 File "C:\Users\Marcin\PycharmProjects\tutorial\main.py", line 105, in <module>
   obstacle_group.draw(screen)
 File "C:\Users\Marcin\PycharmProjects\tutorial\venv\lib\site-packages\pygame\sprite.py", line 552, in draw
  zip(sprites, surface.blits((spr.image, spr.rect) for spr in sprites))
 File "C:\Users\Marcin\PycharmProjects\tutorial\venv\lib\site-packages\pygame\sprite.py", line 552, in <genexpr>
  zip(sprites, surface.blits((spr.image, spr.rect) for spr in sprites))
AttributeError: 'Obstacle' object has no attribute 'image'

Process finished with exit code 1

1 Answers

pygame.sprite.Group.draw() and pygame.sprite.Group.update() is a method which is provided by pygame.sprite.Group. It uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects — you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

Therefore the pygame.sprite.Sprite must have an image and a rect attribute which can be used to draw the object. e.g.:

class Obstacle(pygame.sprite.Sprite):
    def __init__(self,type):
        super().__init__()
        if type == 'fly':
            self.snail_1 = pygame.image.load("graphics\\snail1.png").convert_alpha()
            self.snail_2 = pygame.image.load("graphics\\snail2.png").convert_alpha()
            self.snail_frames = [self.snail_1,self.snail_2]
            self.snail_index = 0
            self.image = self.snail_1
            self.rect = self.image.get_rect(midbottom = (200,210))
        else:
            self.fly_1 = pygame.image.load("graphics\\fly1.png").convert_alpha()
            self.fly_2 = pygame.image.load("graphics\\fly2.png").convert_alpha()
            self.fly_frames = [self.fly_1, self.fly_2]
            self.fly_index = 0
            slef.image = self.fly_1
            self.rect = self.image.get_rect(midbottom = (200,210))
Related