【问题标题】:Is there a way in pygame to find collisions within a grouppygame中有没有办法找到组内的碰撞
【发布时间】:2021-04-16 12:53:11
【问题描述】:

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

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

在示例中-

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

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

【问题讨论】:

  • 是的,上一个响应者的建议成功了!

标签: python pygame collision-detection


【解决方案1】:

一种方法是创建一个临时 Group,其中包含所有 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)

【讨论】:

  • 谢谢,我已经尝试了这两个建议,它们都在我的项目中起到了解决方案的作用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多