Add_internal 精灵和面具的属性错误

Add_internal Attribut Error with sprites and Masks

我正在尝试制作塔防游戏 要检查 Enemy 是否在塔的 hitcircle 中,我想使用函数 pygame.sprite.collide_mask 来确定它们是否在接触。

这是输出: AttributeError: 'pygame.mask.Mask' 对象没有属性 'add_internal'

我怎样才能让它发挥作用?我什至在检测游戏碰撞的正确方向上遥遥领先吗?

我希望这段代码能发挥它所需要的作用

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")

collide_mask 使用 Spirte.rect.mask 对象。因此 Mask 属性的名称必须是 mask,而不是 tower_maskenemy_mask.
此外,对象必须有一个名为 rect 的属性,这是一个带有精灵位置的 pygame.Rect 对象。
super 还调用了 class 构造函数中的缺失值。例如:

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))

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