Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我只能检查2D数组的第一列_Python_Python 3.x_Pygame - Fatal编程技术网

Python 我只能检查2D数组的第一列

Python 我只能检查2D数组的第一列,python,python-3.x,pygame,Python,Python 3.x,Pygame,我用精灵写了记忆拼图,我有一些错误: 1.-当我将鼠标移到框上时,只有第一列显示高光效果。 2.-同样,只有第一列显示覆盖框的效果 我认为问题在于功能: def笛卡尔位置(x,y): ''' 该功能用于检查鼠标是否位于方框上方。在这种情况下,函数返回 按二维列表位置顺序在其上的框 ''' 对于范围内的boxx(列): 对于范围内的方形(行): 左侧,顶部=自流式(长方体x,长方体) boxRect=pygame.Rect(左、上、BOXSIZE、BOXSIZE) 如果boxRect.colli

我用精灵写了记忆拼图,我有一些错误: 1.-当我将鼠标移到框上时,只有第一列显示高光效果。 2.-同样,只有第一列显示覆盖框的效果

我认为问题在于功能:

def笛卡尔位置(x,y):
'''
该功能用于检查鼠标是否位于方框上方。在这种情况下,函数返回
按二维列表位置顺序在其上的框
'''
对于范围内的boxx(列):
对于范围内的方形(行):
左侧,顶部=自流式(长方体x,长方体)
boxRect=pygame.Rect(左、上、BOXSIZE、BOXSIZE)
如果boxRect.collidepoint(x,y):#该方法用于检查x,y位置是否与boxRect碰撞
返回(长方体,长方体)
返回(无,无)
这是一个问题。在
cartesiantoppositional
中,return语句(
return(None,None)
)必须位于函数的末尾,而不是外部循环中:

def笛卡尔位置(x,y):
对于范围内的boxx(列):
对于范围内的方形(行):
左侧,顶部=自流式(长方体x,长方体)
boxRect=pygame.Rect(左、上、BOXSIZE、BOXSIZE)
如果boxRect.collidepoint(x,y):
返回(长方体,长方体)

#在源代码中,有一个函数提前返回

def cartesianToPositional(x, y):
    '''
    That function is used to check if the mouse is over a box. In that case, the function return the position of the 
    box on which is over in the 2D list positional order
    '''
    for boxx in range(COLUMNS):
        for boxy in range(ROWS):
            left, top = positionalToCartesian(boxx, boxy)
            boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
            if boxRect.collidepoint(x, y): # That method is used to check if the x, y position is colliding with the boxRect
                return (boxx, boxy)
        # This prevents your outer for loop from completing
        return (None, None)

删除
返回(无,无)
上的一级缩进,应该可以正常工作。

请修复缩进。
def cartesianToPositional(x, y):
    '''
    That function is used to check if the mouse is over a box. In that case, the function return the position of the 
    box on which is over in the 2D list positional order
    '''
    for boxx in range(COLUMNS):
        for boxy in range(ROWS):
            left, top = positionalToCartesian(boxx, boxy)
            boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
            if boxRect.collidepoint(x, y): # That method is used to check if the x, y position is colliding with the boxRect
                return (boxx, boxy)
        # This prevents your outer for loop from completing
        return (None, None)