Pygame 检测圆形和方形之间的碰撞
Pygame detect collision between circle and square
对于我的 pygame 项目,我想检测 2 个方形精灵之间的碰撞。
但是,其中一个精灵应该表现为 方形 ,另一个表现为 圆形
pygame.sprite.collide_rect()
应该检查 2 个正方形之间的碰撞,解释为正方形
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect
pygame.sprite.collide_circle()
应该检查 2 个正方形之间的碰撞,但被解释为圆形
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_circle
所以简而言之,我想要 pygame.sprite.collide_rect()
和 pygame.sprite.collide_circle()
的混合。可能吗?
我不确定如何将两者混合,但我确实做了一个碰撞检测,它最接近你想要的,这里是:
if circle_x < rect_x + circle_width and circle_x + rect_width > rect_x and circle_y < rect_y + circle_height and rect_height + circle_y > rect_y:
#put whatever code you need here
这会将圆圈放入一个假想的盒子中,该盒子的大小刚好可以绕着圆圈转,然后当假想的盒子与矩形碰撞时,您需要激活的代码就会激活。
对于我的 pygame 项目,我想检测 2 个方形精灵之间的碰撞。 但是,其中一个精灵应该表现为 方形 ,另一个表现为 圆形
pygame.sprite.collide_rect()
应该检查 2 个正方形之间的碰撞,解释为正方形
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect
pygame.sprite.collide_circle()
应该检查 2 个正方形之间的碰撞,但被解释为圆形
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_circle
所以简而言之,我想要 pygame.sprite.collide_rect()
和 pygame.sprite.collide_circle()
的混合。可能吗?
我不确定如何将两者混合,但我确实做了一个碰撞检测,它最接近你想要的,这里是:
if circle_x < rect_x + circle_width and circle_x + rect_width > rect_x and circle_y < rect_y + circle_height and rect_height + circle_y > rect_y:
#put whatever code you need here
这会将圆圈放入一个假想的盒子中,该盒子的大小刚好可以绕着圆圈转,然后当假想的盒子与矩形碰撞时,您需要激活的代码就会激活。