简体   繁体   English

我如何检测 pygame 碰撞?

[英]How do I detect pygame collisions?

I'm currently developing a small game with pygame and I'm having a hard time figuring out a way to check for collisions.我目前正在用 pygame 开发一个小游戏,我很难找到一种检查碰撞的方法。 I've found this question , but I can't use the pygame.sprite.spritecollide(player, wall_group, True) because my called player isn't a 'sprite', it's a class that I've created (that uses the functionality of normal shapes ( https://www.pygame.org/docs/ref/draw.html )).我发现了这个问题,但我不能使用pygame.sprite.spritecollide(player, wall_group, True)因为我被调用的玩家不是“精灵”,它是我创建的一个类(使用正常形状的功能( https://www.pygame.org/docs/ref/draw.html ))。

If anyone could figure a way that I can detect collisions and explain to me shortly about the difference between 'sprite' and just a normal shape like circle/rectangle I would appreciate it.如果有人能想出一种方法让我可以检测碰撞并简要地向我解释“精灵”与普通形状(如圆形/矩形)之间的区别,我将不胜感激。

To use pygame's collition detection funcions (like pygame.sprite.spritecollide ), your class does not need to be a subclass of pygame's Sprite .要使用 pygame 的碰撞检测函数(如pygame.sprite.spritecollide ),您的类不需要是 pygame 的Sprite的子类。

Thanks to python's duck typing, the actual class does not matter, as long as your class behaves like a Sprite .多亏了 python 的鸭子类型,实际的类并不重要,只要你的类表现得像一个Sprite In this context, it just means that your class should have a rect attribute, and it should be a Rect (again, technically, it does not need to be a Rect , only "look like" one).在这种情况下,它只是意味着你的类应该有一个rect属性,它应该是一个Rect (同样,从技术上讲,它不需要是一个Rect ,只需要“看起来像”一个)。

Given a simple class like this:给定一个像这样的简单类:

>>> class Foo():
...   def __init__(self):
...     self.rect=pygame.rect.Rect(100, 100, 100, 100)

we can use pygame.sprite.spritecollide like this:我们可以像这样使用pygame.sprite.spritecollide

>>> f=Foo()
>>> g=Foo()

>>> pygame.sprite.spritecollide(f, [g], False)
[<__main__.Foo instance at 0x0000000003C6DF48>]

Also see how the second argument isn't a Group , but a simple List , but python does not care, since the only thing that matters in this case is that you can iterate over the object.另请参阅第二个参数不是Group ,而是一个简单的List ,但 python 并不关心,因为在这种情况下唯一重要的是您可以迭代对象。 If you would pass True as the third argument, this would fail, since spritecollide would try to call sprites() and the second argument and kill() on the elements inside.如果您将True作为第三个参数传递,这将失败,因为spritecollide会尝试调用sprites()和第二个参数,并在内部元素上调用kill()

Likewise, if you want to eg use pixel perfect collision detection, you need a mask attribute etc. So you should use the Sprite class anyway since it offers some more stuff like managing groups.同样,如果你想使用像素完美碰撞检测,你需要一个mask属性等。所以你应该使用Sprite类,因为它提供了更多的东西,比如管理组。 Also, everytime you want to store a position or a size, consider using pygame's Rect class, since it's quite powerfull and it's used/expected in a lot of pygame functions.此外,每次您想要存储位置或大小时,请考虑使用 pygame 的Rect类,因为它非常强大,并且在许多 pygame 函数中使用/预期。

tl;dr: Use pygame's Sprite class. tl;dr:使用 pygame 的Sprite类。 There's probably no good reason for you to not do it, and a bunch of good reason to actually use it.您可能没有充分的理由不这样做,并且有很多充分的理由实际使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM