def CFW(D):
for a in WL:
if D == 0:
CFW是一个功能,如果玩家正在触摸墙壁(D代表方向),则返回 WL是一个包含墙坐标数组的数组,但是它只会读取WL中的第一个数组,而不会迭代到下一个数组,因此只有当玩家触摸WL中的第一组坐标时它才会返回。谁能告诉我我做错了什么? 这是整个功能:
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 :(得分: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
值得注意的是range
和xrange
类支持in
操作:
>>> 5 in xrange(1, 10)
True
>>> 100 in xrange(1, 10)
False