pygame碰撞y轴不工作

时间:2014-12-04 01:41:15

标签: python pygame collision-detection collision

我正在制作一个自上而下的生存游戏。碰撞问题:

我正在让玩家与长凳发生碰撞,问题是,x轴碰撞是否有效,但是y轴碰撞甚至都没有检测到。

if playerx + playerw > 268 and playerx < 368 and playery + playerh > 392 and playery < 442:
    if playerx + playerw > 268 and playerx + playerw < 275:
        playerx -= 5
    if playerx > 360 and playerx < 368:
        playerx += 5
    if playery > 430 and playery < 441:
        playery += 5
    if playery + playerh > 386 and playery + playerh < 392:
        playery -= 5

代码很明显,playerx =玩家x coords ,, playery =玩家y coords ,, playerw =玩家宽度,玩家h =玩家h ,, 268,368,392和442是替补上左下角正确的坐标。

同样,我可以左右碰撞,但不能向上或向下碰撞任何解决方案?

编辑:

修正了一半,底部,左侧和右侧碰撞都有效,而不是顶部。

1 个答案:

答案 0 :(得分:1)

尝试将第一行中的392更改为386。您正在设置错误的限制,以便忽略检查。

if playerx + playerw > 268 and playerx < 368 and playery + playerh > 386 and playery < 442: