首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >For循环仅读取数组的第一项

For循环仅读取数组的第一项
EN

Stack Overflow用户
提问于 2014-01-24 01:15:18
回答 1查看 62关注 0票数 0
代码语言:javascript
复制
def CFW(D):
    for a in WL:
        if D == 0:

CFW是一个函数,如果玩家正在触摸一面墙(D代表方向),则返回WL是一个包含墙的坐标数组的数组,但是它只会读取WL中的第一个数组,而不会迭代到下一个数组,所以它只会在玩家触摸WL中的第一组坐标时返回。有人能告诉我我哪里做错了吗?下面是整个函数:

代码语言:javascript
复制
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

我将代码更改为

代码语言:javascript
复制
def CFW(D):
    for c in xrange(0,1):
    a = WL[c]
        if D == 0: 

但是,它仍然不读取第二个数组。我定义WL的方式是这样的:

代码语言:javascript
复制
WL = [[32,32],[64,32]]
EN

回答 1

Stack Overflow用户

发布于 2014-01-24 01:39:06

乍一看,我以为这就是你想要做的:

代码语言:javascript
复制
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操作:

代码语言:javascript
复制
>>> 5 in xrange(1, 10)
True
>>> 100 in xrange(1, 10)
False
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21314997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档