user1615699
user1615699

Reputation: 3

pygame image AttributeError

I am trying to create a pacman game with pygame but I have run into a few problems. It it saying that the 'Food' has no image.

This is my pacman game [code redacted].

The problem is this area here something is wrong and it tells me that food has no attribute image

class Food(pygame.sprite.Sprite):
    def __init__(self,x,y,color):
        pygame.sprite.Sprite.__init__(self)

        pygame.image = pygame.Surface([7,7])
        self.image.fill(color)

        self.rect = self.image.get_rect()
        self.rect.top = y
        self.rect.left = x
    def update (self,player):
        collidePlayer = pygame.sprite.spritecollide(self,player,False)
        if collidePlayer:
            food.remove

Upvotes: 0

Views: 983

Answers (1)

DSM
DSM

Reputation: 353009

Removing all the irrelevant bits, do you see the difference between the following Sprite subclasses' __init__ methods?

class Wall(pygame.sprite.Sprite): 

    def __init__(self,x,y,width,height, color):#For when the walls are set up later
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([width, height]) # <-------------
        self.image.fill(color)

class player(pygame.sprite.Sprite):

    def __init__ (self,x,y):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([13,13])  # <-------------
        self.image.fill(white) 

class Food(pygame.sprite.Sprite)
    def __init__(self,x,y,color):
        pygame.sprite.Sprite.__init__(self)

        pygame.image = pygame.Surface([7,7])  # <----- this one is different!
        self.image.fill(color)

The reason you're getting an error saying that self doesn't have an image attribute is because you didn't set self.image, you stored the image in the pygame module itself.

PS: The lines which look like

        food.remove

seem suspicious to me. If remove is a method, it'd be called by food.remove(), and food.remove wouldn't do anything.

Upvotes: 1

Related Questions