代码之家  ›  专栏  ›  技术社区  ›  Vincenzo Hamond

Pygame platformer碰撞检测不工作

  •  0
  • Vincenzo Hamond  · 技术社区  · 8 年前

    我正在尝试在我的platformer游戏中实现碰撞检测。当我尝试运行游戏时,我只是从平台上摔下来,而不是在玩家点击时停止。如有任何帮助或建议,将不胜感激。 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 
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   ChatterOne    8 年前

    在逻辑和实现上都有一些错误。

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

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

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

    • 您可以完全删除 gravity 方法并仅使用 collision\u检测 方法

    类似这样:

    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
    

    试试这样的东西 self.collision_detect(platform(0, 500, 800, 20)) 在您的 do 作用