碰撞检测中的if语句与不同的变量不起作用

时间:2016-01-01 13:29:34

标签: python pygame collision-detection

我正在尝试检测玩家是否已击中该区块,如果是,则当然停止它们(例如,如果它击中左侧的墙壁则不能向左移动)。所以它工作正常,我已完成上下碰撞,但出于某种原因,当我继续为左侧(最后一个if语句)进行碰撞时,玩家能够直接穿过该区块而我无法弄清楚为什么,因为if语句基本相同。

仅供参考:pX是玩家的x坐标,      pY是玩家的y坐标,      XnY [1]是块的y坐标,      XnY [0]是块的x坐标,      playerSize是一个正方形,所以50x50

    level =  ["W","W","W","W","W","W","W","W","W","W","W","W",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",                                       
              "N","N","N","N","N","N","N","N","N","N","N","N","L",                                     
              "N","N","N","N","N","N","P","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L",
              "N","N","N","N","N","N","N","N","N","N","N","N","L","S"]    
blockXY = []

    for element in level:
        if element == "N":
             x += block_width
        if element == "L":                                              
             y += block_height
             x = 0
        if element == "P": 
            drawBlock(block_width,block_height,x,y)                
            blockXY.append(x)
            blockXY.append(y)
            if appendBlockXY:
                if len(collisionArray) > P_count:
                    del(collisionArray[P_count])
                    print(collisionArray)
                    appendBlockXY = False
                collisionArray.append(blockXY)
            blockXY = []
            x += block_width
        if element == "S":
            y = 0


for XnY in collisionArray

        if pX >= XnY[0] and pX <= XnY[0] + block_width or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:

            if pY - block_height == XnY[1]:
                canMoveUp = False
                y_change = 0

        if pX >= XnY[0] and pX <= XnY[0] + block_width or pX + playerSize >= XnY[0] and pX <= XnY[0] + block_width:

            if XnY[1] - playerSize == pY:
                canMoveDown = False
                y_change = 0

        if pY >= XnY[1] and pY <= XnY[1] + block_height or pY +  playerSize >= XnY[1] and pY + playerSize <= XnY[1] + block_height:

            if pX - block_width == XnY[0]:
                canMoveLeft = False
                y_change = 0

1 个答案:

答案 0 :(得分:0)

我搞定了!这是我的一个愚蠢的错误,而不是写y_change = 0它应该是x_change = 0.哎呀!