【问题标题】:Pygame platformer collision detection not workingPygame平台游戏碰撞检测不起作用
【发布时间】:2018-03-26 20:52:41
【问题描述】:
我正在尝试对我的平台游戏实施碰撞检测。当我尝试运行游戏时,我只是从平台上掉下来,而不是在玩家击中它时停止。任何帮助或建议将不胜感激。
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
【问题讨论】:
标签:
python
python-3.x
pygame
game-physics
【解决方案1】:
在逻辑和实现上都有一些错误。
在您的collision_detect 中,您说您将falling 状态更改为false,但您从未这样做。此外,您在检查之前将下降设置为真。但请先看看我的其他观点。
玩家不应有“下落”或“不下落”的状态。重力始终存在,因此玩家总是下落。如果有平台挡住它,速度就会降到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)) 的内容。