使用 Pygame Rects 在 2D 平台游戏中进行碰撞检测(地面和斜坡)

发布于 2024-09-30 21:27:39 字数 391 浏览 2 评论 0原文

首先,我并不寻求任何关于碰撞检测逻辑的说明;我得到它。

我正在尝试解决的是使用 Sprites 和 Sprites 来使用 Pygame 执行此操作的最简单的方法。矩形。我希望能够检查玩家与地面、墙壁和物体的碰撞。连续下坡。从理论上讲,这是非常简单的,但我遇到了困难,因为似乎你不能用一个矩形来做到这一点。

一个矩形足够简单,可以让您在 X 平面上与墙壁发生碰撞。相同的矩形也可用于 Y 平面中的实体,但不能用于斜坡 - 因为使用 Pygame 中的碰撞例程,它会检查整个矩形(或掩模),而不仅仅是矩形的底部中间。此外,您似乎还需要有许多“精灵”来检查碰撞,这些“精灵”在播放器周围的各个位置都是 1x1 像素。

在没有一堆 3、4 或更多单独的“碰撞像素”来检查斜率的情况下,最简单的方法是什么?

杰夫

First off, I am not after any instructions on logic for collision detection; I get it.

What I am trying to work out is the least complicated way to do this with Pygame using Sprites & Rects. I want to be able to check collisions for the Player against ground, walls & slopes. In theory it is quite straight forward, but I'm having difficulty because it seems like you cannot do this with one Rect.

One Rect is simple enough to get you collisions in the X plane against walls. The same Rect could be used also be used in the Y plane against solids, but not with slopes - since with the collision routines in Pygame it checks the whole Rect (or mask), rather than perhaps just the bottom middle of the Rect. It seems in addition you need to have a number of "sprites" to check collisions with, that are 1x1 pixel in various places around the Player.

What's the easiest way to do this, without having a bunch of 3, 4, or more separate "collision pixels" to check against slopes?

Geoff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2024-10-07 21:27:39

在我看来,你想要像素完美的碰撞检测。 在这里你可以找到一个预制函数,在我看来这就是你想要的/需要。

It sound to me like you want pixel perfect collision detection. Here you can find a premade function that seems to me to be what you want/need.

玩物 2024-10-07 21:27:39

您可以使用 pygame.mask,它在 C 语言中提供像素完美的碰撞检测:

http://www .pygame.org/docs/ref/mask.html

You could use pygame.mask, which provides pixel perfect collision detection in C:

http://www.pygame.org/docs/ref/mask.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文