1

I am trying to make a Towerdefense Game to check if the Enemy is in the hitcircle of the tower I want to use the function pygame.sprite.collide_mask to find out if they are touching.

This is the output: AttributeError: 'pygame.mask.Mask' object has no attribute 'add_internal'

How can I make it work? Am I even remotely in the right direction of detecting the collision for the game?

I hope this code can work as much as it needs

import pygame

pygame.init()


class Tower(pygame.sprite.Sprite):
    def __init__(self, window):
         self.collision_circle = 


pygame.image.load('assets/Towers&Projectiles/Collision_Circle/collision_circle.png')
        self.tower_mask = pygame.mask.from_surface(self.collision_circle)


class Enemy(pygame.sprite.Sprite):

    def __init__(self, window):
        self.enemy_mask_image = pygame.image.load('Assets/Enemy/WALK_000.png')
        self.enemy_mask = pygame.mask.from_surface(self.enemy_mask_image)


pygame.display.set_caption("Tower Defense")
width = 1200
height = 840
display_surface = pygame.display.set_mode((width, height))

my_enemy = Enemy(display_surface)
my_tower = Tower(display_surface)

while True:
    enemy_sprite = pygame.sprite.GroupSingle(my_enemy.enemy_mask)
    tower_sprite = pygame.sprite.GroupSingle(my_tower.tower_mask)
    if pygame.sprite.spritecollide(enemy_sprite.sprite,tower_sprite,False,pygame.sprite.collide_mask()):
        print("collision")
001
  • 13,291
  • 5
  • 35
  • 66
notDaniel
  • 17
  • 5

1 Answers1

0

collide_mask use the .rect and .mask object of the Spirte. Therefore the name of the Mask attribute must be mask, but not tower_mask or enemy_mask.
In addition, the objects must have an attribute named rect, which is a pygame.Rect object with the position of the sprite.
Also the super calls a missing from your class constructors. e.g.:

class TowerCircle(pygame.sprite.Sprite):
    def __init__(self, centerx, centery):
        super().__init__()
        self.image = pygame.image.load('assets/Towers&Projectiles/Collision_Circle/collision_circle.png')
        self.mask = pygame.mask.from_surface(self.collision_circle)
        self.rect = self.image.get_rect(center = (centerx, centery))

class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.image.load('Assets/Enemy/WALK_000.png')
        self.mask = pygame.mask.from_surface(self.enemy_mask_image)
        self.rect = self.image.get_rect(topleft = (x, y))

See Pygame mask collision and Make a line as a sprite with its own collision in Pygame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @notDaniel Please learn the basics and read about [Inheritance](https://docs.python.org/3/tutorial/classes.html#inheritance). None of your _Sprite_ classes will actually work as you expected. – Rabbid76 Mar 12 '22 at 15:00