2014-01-23 38 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