2014-01-23 41 views
0
def CFW(D): 
    for a in WL: 
     if D == 0: 

CFW是返回,如果播放器接触的壁(d为的方向) WL是包含壁的坐标数组的数组的函数,然而它会只读取WL中的第一个数组,而不是迭代到下一个数组,因此只有在玩家正在触摸WL中的第一组坐标时才会返回。谁能告诉我我做错了什么? 这里是整个函数:For循环只读取阵列的第一项

def CFW(D): 
    for a in WL: 
     if D == 0: 
      if a[0] > (plyposx-30) and a[0] < (plyposx+30) and plyposy == a[1]-32: 
       return 1 # 1 = is touching 
      else: 
       return 0 # 0 = is not touching 
     elif D == 2: 
      if a[0] > (plyposx-30) and a[0] < (plyposx+30) and plyposy == a[1]+32: 
       return 1 
      else: 
       return 0 
     elif D == 1: 
      if a[1] > (plyposy-30) and a[1] < (plyposy+30) and plyposx == a[0]+32: 
       return 1 
      else: 
       return 0 
     elif D == 3: 
      if a[1] > (plyposy-30) and a[1] < (plyposy+30) and plyposx == a[0]-32: 
       return 1 
      else: 
       return 0 

我改变了代码

def CFW(D): 
    for c in xrange(0,1): 
    a = WL[c] 
     if D == 0: 

但是它仍然无法读取第二阵列。 我已经定义WL的方式是这样的:

WL = [[32,32],[64,32]] 
+0

'WL'长什么样子? –

+0

WL = [[32,32],[64,32]] – user2883136

+0

假设D总是等于0,1,2或3,for循环中的每个代码路径都会返回。 – Matt

回答

2

乍一看,我还以为这是你想要做什么:

def CFW(D): 
    for a in WL: 
    if D == 0: 
     if a[0] > (plyposx-30) and a[0] < (plyposx+30) and plyposy == a[1]-32: 
     return 1 # 1 = is touching 
    elif D == 2: 
     if a[0] > (plyposx-30) and a[0] < (plyposx+30) and plyposy == a[1]+32: 
     return 1 
    elif D == 1: 
     if a[1] > (plyposy-30) and a[1] < (plyposy+30) and plyposx == a[0]+32: 
     return 1 
    elif D == 3: 
     if a[1] > (plyposy-30) and a[1] < (plyposy+30) and plyposx == a[0]-32: 
     return 1 
    return 0 # 0 = is not touching 

值得注意的是rangexrange类的支持in操作:

>>> 5 in xrange(1, 10) 
True 
>>> 100 in xrange(1, 10) 
False