微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。