使碰撞检测代码适应其他方面

时间:2014-01-22 23:02:07

标签: python-2.7 pygame

我已经开发了一个函数,可以在精灵移动的方向上搜索一个墙,但到目前为止,我只能让它向下运动并且不能让它在左右工作。功能如下:

def CFW(DirectionMoving):
    for a in WallList:
        if DirectionMoving == 0: #0=down
            if a[0] > (plyposx-30) and a[0] < (plyposx+30): #a[0] is x coord for the wall
                return 1 #positive for wall
            else:
                return 0 #negative for wall

这是左和右的非工作代码:

        elif DirectionMoving == 1: #1=left
            if a[1] > (plyposy-30) and a[1] < (plyposy+30):
                return 1
            else:
                return 0

如果有人能解决为什么这对左右不起作用我会很感激帮助。

1 个答案:

答案 0 :(得分:0)

elif DirectionMoving == 1: #1=left
    if a[1] > (plyposy-30) and a[1] < (plyposy+30):
        return 1
    else:
        return 0

是否输入了此代码?让我们改变一下,这样你就可以看到发生了什么。

elif DirectionMoving == 1: #1=left
    print 'dbg: moving left'
    if a[1] > (plyposy-30) and a[1] < (plyposy+30):
        print 'dbg: moving left - condition met'
        return 1
    else:
        print 'dbg: moving left - condition not met'
        return 0

当你发现它时,再次更改它。

elif DirectionMoving == 1: #1=left
    return a[1] > (plyposy-30) and a[1] < (plyposy+30)