y 轴上的碰撞检测不起作用(pygame) [英] Collision detection on the y-axis does not work (pygame)

查看:53
本文介绍了y 轴上的碰撞检测不起作用(pygame)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的代码中的碰撞检测工作.我正在使用向量,我希望玩家精灵在与名为 walls 的精灵组碰撞时发生碰撞并停止.问题是玩家可以穿过底墙.

I am trying to get the collision detection in my code to work. I am using vectors and I want the player sprite to collide and stop when it collides with a sprite group called walls. The problem is that the player can pass through the bottom wall.

在这个例子中我已经关闭了重力.

I've turned the gravity off in this example.

x 碰撞没问题,但画起来有点滑稽,只有 y 方向不能用相同的代码正常工作.

The x collision is okay but it draws a bit funny, only the y direction won't work properly with the same code.

我已经尝试用调试器调试代码无济于事.

I've already tried to debug the code with a debugger to no avail.

我主要想弄清楚为什么垂直碰撞检测不起作用,但我也很感激有关水平碰撞检测的建议.

I'm mainly interested in figuring out why the vertical collision detection doesn't work, but I'd also appreciate suggestions about the horizontal collision detection.

链接:Github:完整代码

当前碰撞检测代码:

def collide_with_walls(self, dir):
    if dir == 'x':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.pos.x > 0:
                self.pos.x = hits[0].rect.left - self.rect.width
            if self.pos.x < 0:
                self.pos.x = hits[0].rect.right
            self.rect.x = self.pos.x
    if dir == 'y':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.pos.y >= 0:
                self.pos.y = Wall.rect.top - self.rect.height / 2
            if self.pos.y < 0:
                self.pos.y = hits[0].rect.bottom
            self.rect.y = self.pos.y

推荐答案

据我所知,您的水平移动和碰撞检测工作正常.我再次启用了垂直移动,只需要修复一些东西.Wall 必须更改为 hits[0] 并且在接触墙壁后 y 速度必须设置为 0.

As far as I can see your horizontal movement and collision detection work correctly. I enabled the vertical movement again and had to fix only a few things. Wall had to be changed to hits[0] and the y-velocity had to be set to 0 after touching a wall.

def collide_with_walls(self, dir):
    if dir == 'x':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.pos.x > 0:
                self.pos.x = hits[0].rect.left - self.rect.width
            if self.pos.x < 0:
                self.pos.x = hits[0].rect.right
            self.rect.x = self.pos.x
    if dir == 'y':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.pos.y >= 0:
                # `Wall` had to be changed to `hits[0]`.
                self.pos.y = hits[0].rect.top - self.rect.height
            if self.pos.y < 0:
                self.pos.y = hits[0].rect.bottom
            self.rect.y = self.pos.y
            self.vel.y = 0  # Stop the player, otherwise he'll keep accelerating.

还有一个问题:如果玩家精灵下落的时间过长,它会加速并向下移动得如此之快,以至于它可以跳过与墙壁的碰撞检测,而只会穿过它.确保精灵不能跳过碰撞检测,通过限制它可以下降的距离,给它一个最大速度,或者简单地通过使墙壁更厚.您也可以使用光线投射,但实现起来会更复杂一些.

There's still a problem: If the player sprite falls too long, it will accelerate and move so fast downwards that it can skip the collision detection with the wall and will just fall through it. Make sure that the sprite can't skip the collision detection, either by limiting the distances that it can fall, giving it a maximum speed or simply by making the walls thicker. You could also use ray casting, but that would be a bit more complex to implement.

这篇关于y 轴上的碰撞检测不起作用(pygame)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆