PyGame attribute error after creating second instance of certain objects

Viewed 19

I can create one instance of this class, and it functions fine. However whenever a second instance is attempted to be created, even if the first object has been deleted, it returns an error. This is also odd because there is a class identical to this one, which can have as many instances created as needed with no errors.

Object is being made the exact same way both times.

class GOAL(pygame.sprite.Sprite):
    def __init__(self,spawnx,spawny):
        super().__init__()
        self.image = goal
        self.surf = pygame.Surface(self.image.get_rect().size)
        self.rect = self.image.get_rect(topleft = (spawnx,spawny))

Class initiation code.

goal = GOAL((lineCount*100),(characterCount*100))
all_sprites.add(goal)

Class creation code.

Traceback (most recent call last):
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 975, in <module>
    mainmenu()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 689, in mainmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 732, in playmenu
    entity.clicked()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 540, in clicked
    self.sendfunction()
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 768, in level1
    levelgeneration(levelToGenerate,all_sprites,enemies,walls,playerGroup)
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 903, in levelgeneration
    goal = GOAL((lineCount*100),(characterCount*100))
  File "C:\Users\User\Documents\python\pygame\simian shenanigans\simian shenanigans.py", line 638, in __init__
    self.surf = pygame.Surface(self.image.get_rect().size)
AttributeError: 'GOAL' object has no attribute 'get_rect'

Error code.

Please ask if any other code required to be shown. Whole thing is 1000 lines and not all relevant so I have tried to include necessary code.

1 Answers

It looks like you're setting self.image to be goal, which is an instance of GOAL, assuming thats the same goal as in the class initiation code. However, even though GOAL is a pygame.sprite.sprite, that class doesn't have a get_rect function!

I think you want to reference the pygame.Surface instead, according to the example here:

class Block(pygame.sprite.Sprite):

    def __init__(self, color, width, height):
       # Call the parent class (Sprite) constructor
       pygame.sprite.Sprite.__init__(self)

       # Create an image of the block, and fill it with a color.
       # This could also be an image loaded from the disk.
 This--->self.image = pygame.Surface([width, height])
       self.image.fill(color)

       self.rect = self.image.get_rect()
Related