为什么这些条件多次成真?

时间:2014-04-05 07:38:59

标签: python list pygame conditional-statements

我有一段非常复杂的代码:

def translate(self, direction, step):

    def iterate(n1,n2,boolean):
        for j in self.gridKeys:
            if boolean == True:
                if ((j[0][0] == i[0][0]+n1) and (j[0][1] == i[0][1]+n2)):
                    if ((j[1] not in self.collisionGids) and (j[1] == '0')):
                        j[1] = i[1]
                        j[1].setX(j[0][0])
                        j[1].setY(j[0][1])
                        i[1] = '0'
            if boolean == False:
                if ((j[0][0] == i[0][0]-n1) and (j[0][1] == i[0][1]-n2)):
                    if ((j[1] not in self.collisionGids) and (j[1] == '0')):
                        j[1] = i[1]
                        j[1].setX(j[0][0])
                        j[1].setY(j[0][1])
                        i[1] = '0'

    for i in self.gridKeys:
        if isinstance(i[1],(Grect,Grict)):
            if direction == 0:
                iterate(0,1,False)
                print "True just once."
            elif direction == 1:
                iterate(0,1,True)
                print "True many times."
            elif direction == 2:
                iterate(1,0,False)
                print "True just once."
            elif direction == 3:
                iterate(1,0,True)
                print "True many times."

通过键盘输入选择方向:

@staticmethod
def useBasicMovement(keys, collisionModel, step):

    if keys[pygame.K_UP] or keys[pygame.K_w]:
        collisionModel.translate(0,step) # <<< FOCUS HERE
    elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
        collisionModel.translate(1,step) # <<< FOCUS HERE
    elif keys[pygame.K_LEFT] or keys[pygame.K_a]:
        collisionModel.translate(2,step) # <<< FOCUS HERE
    elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        collisionModel.translate(3,step) # <<< FOCUS HERE

当我按下向上或向左键一次时,它会到达他们的代码块并打印一次。但是当我按下向下或向右键时,它会打印多次(出于某种原因,它会打印,直到它到达列表的末尾)。这有点难以解释......这里是self.gridKeys的价值:

[[(0, 0), '1'], [(1, 0), '1'], [(2, 0), '1'], [(3, 0), '1'], [(4, 0), '1'], [(5, 0), '1'], [(6, 0), '1'], [(7, 0), '1'], [(8, 0), '1'], [(9, 0), '1'], [(0, 1), '1'], [(1, 1), '0'], [(2, 1), '0'], [(3, 1), '0'], [(4, 1), '1'], [(5, 1), '1'], [(6, 1), '0'], [(7, 1), '0'], [(8, 1), '0'], [(9, 1), '1'], [(0, 2), '1'], [(1, 2), '0'], [(2, 2), '0'], [(3, 2), '0'], [(4, 2), '0'], [(5, 2), '0'], [(6, 2), '0'], [(7, 2), '0'], [(8, 2), '0'], [(9, 2), '1'], [(0, 3), '1'], [(1, 3), '0'], [(2, 3), <pythun.Grict instance at 0x02447288>], [(3, 3), '0'], [(4, 3), '0'], [(5, 3), '0'], [(6, 3), '0'], [(7, 3), '0'], [(8, 3), '0'], [(9, 3), '1'], [(0, 4), '1'], [(1, 4), '0'], [(2, 4), '0'], [(3, 4), '0'], [(4, 4), '0'], [(5, 4), '0'], [(6, 4), '0'], [(7, 4), '0'], [(8, 4), '0'], [(9, 4), '1'], [(0, 5), '1'], [(1, 5), '3'], [(2, 5), '0'], [(3, 5), '0'], [(4, 5), '0'], [(5, 5), '0'], [(6, 5), '0'], [(7, 5), '0'], [(8, 5), '0'], [(9, 5), '1'], [(0, 6), '1'], [(1, 6), '1'], [(2, 6), '1'], [(3, 6), '1'], [(4, 6), '0'], [(5, 6), '0'], [(6, 6), '0'], [(7, 6), '0'], [(8, 6), '4'], [(9, 6), '1'], [(0, 7), '1'], [(1, 7), '0'], [(2, 7), '0'], [(3, 7), '0'], [(4, 7), '0'], [(5, 7), '0'], [(6, 7), '0'], [(7, 7), '1'], [(8, 7), '1'], [(9, 7), '1'], [(0, 8), '1'], [(1, 8), '0'], [(2, 8), '0'], [(3, 8), '0'], [(4, 8), '0'], [(5, 8), '0'], [(6, 8), '0'], [(7, 8), '1'], [(8, 8), '1'], [(9, 8), '1'], [(0, 9), '1'], [(1, 9), '1'], [(2, 9), '1'], [(3, 9), '1'], [(4, 9), '1'], [(5, 9), '1'], [(6, 9), '1'], [(7, 9), '1'], [(8, 9), '1'], [(9, 9), '1']]

正如你所看到的,在这个列表的某个点上有一个Grict类的实例,如果是真的,它会再次遍历所有相同的列表,然后根据前一个列表设置新的位置(我在列表中移动一个对象,并且tile-positions是元组)。出于某种原因,&#34; down&#34;和&#34;对&#34;多次执行。为什么这些条件多次成真?有什么可能性?如果我在翻译方法之外打印(如果按下按键则打印),我得到&#34;一次&#34;对于所有4个人。但是由于某种原因,这两个代码块会被执行多次。我知道尝试简化为一个简单的例子是一件很麻烦的事情,我无法做到,所以我很抱歉。

0 个答案:

没有答案