Search code examples
pythonpygamegame-developmentpygame-surface

AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'


I am trying to get pixel perfect collision but I keep running into this error:

Traceback (most recent call last):
  File "D:\Projects\games\venv\lib\site-packages\pygame\sprite.py", line 1528, in collide_circle
    xdistance = left.rect.centerx - right.rect.centerx
AttributeError: 'pygame.mask.Mask' object has no attribute 'rect'

Process finished with exit code 1

I am not sure of what's going wrong. Is it my implementation or logic or maybe both?

My code:


class AlienShooting(pygame.sprite.Sprite):

    def __init__(self, w=640, h=640):

        # init display

        # init game state
        self.spaceship_main = pygame.image.load('spacecraft_1.png').convert_alpha()

        self.alien_spacecraft1 = pygame.image.load('spacecraft_alien.png').convert_alpha()

        self.alien_spacecraft_rec1 = self.alien_spacecraft1.get_rect()

        self.mask1 = pygame.mask.from_surface(self.alien_spacecraft1)

        self.main_spaceship_mask = pygame.mask.from_surface(self.spaceship_main)


    def play_step(self):
        # user input

        # Move the spaceship

        # check if game over
        game_over = False
        if self._is_collision():
            game_over = True

        # Add new alien spaceships

        # Randomly move alien spaceships

        # update ui and clock

        # return game over and score
        return False

    def _is_collision(self):
        if pygame.sprite.collide_circle(self.mask1, self.main_spaceship_mask):
            return True
        return False

It might be because both my main space ship and my alien spaceship are in the same class, but I'm not sure.


Solution

  • See How do I detect collision in pygame? and Pygame mask collision. If you want to use pygame.sprite.collide_circle() or pygame.sprite.collide_mask you have to create pygame.sprite.Sprite objects.
    spaceship_main and alien_spacecraft1 have to be Sprite objects. Sprite objects must have a rect and image attribute and can optionally have a mask attribute.

    e.g.:

    class AlienShooting():
        def __init__(self, w=640, h=640):
    
            self.spaceship_main = pygame.sprite.Sprite()
            self.spaceship_main.image = pygame.image.load('spacecraft_1.png').convert_alpha()
            self.spaceship_main.rect = self.spaceship_main.image.get_rect(center = (140, 140))
            self.spaceship_main.mask = pygame.mask.from_surface(self.spaceship_main.image)
    
            self.alien_spacecraft1 = pygame.sprite.Sprite()
            self.alien_spacecraft1.image = pygame.image.load('spacecraft_alien.png').convert_alpha()
            self.alien_spacecraft1.rect = self.alien_spacecraft1.image.get_rect(center = (160, 160))
            self.alien_spacecraft1.mask = pygame.mask.from_surface(self.alien_spacecraft1.image)
    
        def _is_collision(self):
            if pygame.sprite.collide_mask(self.spaceship_main, self.alien_spacecraft1):
                return True
            return False