Pygame平台碰撞检测不工作

2024-05-19 02:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图实现碰撞检测到我的平板游戏。当我尝试运行游戏时,我只是从平台上摔下来,而不是在玩家点击时停止。任何帮助或建议将不胜感激。 My full code can be found here

    def collision_detect(self,x1,y1,platform):
    #Stops the player from falling once they hit the platform by setting falling to false
    if self.x > platform.x and self.x < platform.x2:
        if self.y == platform.y:
             self.yVel += 0 

Tags: theself游戏ifmy玩家code平台
1条回答
网友
1楼 · 发布于 2024-05-19 02:27:00

在逻辑和执行上都有一些错误。你知道吗

  • 在你的collision_detect中,你说你把堕落的状态改成了虚假,但你从来没有这样做过。另外,在检查之前,您将falling设置为true。但先看看我的其他观点。

  • 球员不应该有一个状态“下降”或“不下降”。地心引力总是存在的,所以玩家总是在下落。如果有平台阻挡它,速度降到0,就这样。就像你真的要摔倒了,但是地板挡住了你。

  • 您不应该检查self.y == platform.y,因为如果您将y坐标增加2或3,您可能会“跳过”确切的坐标,因此您实际需要的是self.y >= platform.y

  • 您可以完全删除gravity方法,而只使用collision_detect方法。

像这样:

def collision_detect(self, platform):
    if self.x > platform.x and self.x < platform.x2:
        if self.y >= platform.y:
            self.yVel = 0
        else:
            self.yVel = 5

do函数中尝试使用self.collision_detect(platform(0, 500, 800, 20))之类的函数。你知道吗

相关问题 更多 >

    热门问题