在pygame中有没有办法找到组内的碰撞

问题描述

pygame 模块在其各种碰撞检测方法中利用组。

我一直在尝试寻找一种方法来查找精灵与包含在同一组中的其他成员的冲突。

在示例中 -

pygame.sprite.spritecollide(creature,creaturegroup,False)

这继续为碰撞检测提供误报,我认为这是由于“生物”精灵包含在“生物组”组中。在组内查找冲突的任何建议解决方法

解决方法

一种方法是创建一个临时,其中包含所有 Sprite 但您要测试的那个:

test_group = pagame.sprite.Group([s for s in creaturegroup if s != creature])
pygame.sprite.spritecollide(creature,test_group,False) 

另一种选择是编写自己的测试函数,跳过相等的对象:
(见pygame.sprite.collide_rect()

def collide_if_not_self(left,right):
    if left != right:
        return pygame.sprite.collide_rect(left,right)
    return False
pygame.sprite.spritecollide(creature,creaturegroup,False,collide_if_not_self)