程序卡在 for 循环中,但 return 什么都没有 Pygame

Program being stuck in a for loop but doesn't return anything Pygame

我程序中的 for 循环(python 使用 pygame)不打印或 return 任何东西(意味着它停止循环)但外面的 return 和打印行也不是 运行.

这是我指的函数:

def square_under_attack(posx, posy):
    print("SQUARE UNDER ATTACK FUNCTION CALLED")
    global white_to_move
    white_to_move = not white_to_move
    enemy_moves = generate_possible_moves()
    white_to_move = not white_to_move
    x=0
    for opponent_move in enemy_moves:
        print(x)
        if opponent_move[3] == posx and opponent_move[4] == posy:  # if the opponent can move to the square being tested
            print("returned true")
            return True
        x+=1
    print("returned false")
    return False

此函数调用此函数时会卡住(包括在内,以防前一个函数与此函数的 运行ning 有关 - 我在这个问题上很困惑,所以任何额外信息为您提供所有我认为有用的帮助。)

def check_check():
    global white_to_move
    global white_king
    global black_king
    if white_to_move is True:
        return square_under_attack(white_king[0], white_king[1])
    else:
        return square_under_attack(black_king[0], black_king[1])

对于那些想知道的人,下面是 generate_possible_moves() 函数 return 在这种情况下的作用:

[(6, 0, 5, 0), (6, 0, 4, 0), (6, 1, 5, 1), (6, 1, 4, 1), (6, 2, 5, 2), (6, 2, 4, 2), (6, 3, 5, 3), (6, 3, 4, 3), (6, 4, 5, 4), (6, 4, 4, 4), (6, 5, 5, 5), (6, 5, 4, 5), (6, 6, 5, 6), (6, 6, 4, 6), (6, 7, 5, 7), (6, 7, 4, 7), (7, 1, 5, 2), (7, 1, 5, 0), (7, 6, 5, 7), (7, 6, 5, 5)]

问题是代码试图用 5 索引 4 位元组:

if opponent_move[3] == posx and opponent_move[4] == posy:

opponent_move 指的是与此类似的列表:

if opponent_move[3] == posx and opponent_move[4] == posy:

所以它是索引错误并在程序的其他部分触发错误回退。