【问题标题】:I only can check the first column of an 2D array我只能检查二维数组的第一列
【发布时间】:2020-02-10 17:02:58
【问题描述】:

我用精灵写了那个记忆谜题,我有一些错误: 1.- 当我将鼠标移到框上时,只有第一列显示突出显示效果。 2.- 同样,只有第一列显示了被覆盖的盒子的效果

the complete program in github

我认为问题在于函数:

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)
        return (None, None)
def drawHighlightBox(boxx, boxy):
    '''
    This function draw a perimether around the box passed with the highlightcolor
    '''
    left, top = positionalToCartesian(boxx, boxy)
    pygame.draw.rect(DISPLAY, HIGHLIGHTCOLOR, (left - 5, top - 5, BOXSIZE + 10, BOXSIZE + 10), 4) # 4 is for the width of the line

def drawBoxCovers(board, boxes, cover):
    '''
    This function cover the icons if is needed
    '''
    for box in boxes:
        left, top = positionalToCartesian(box[0], box[1])
        pygame.draw.rect(DISPLAY, BGCOLOR, (left, top, BOXSIZE, BOXSIZE))
        ball = getBall(board, box[0], box[1])
        drawIcon(ball, box[0], box[1])
        if cover > 0: 
            pygame.draw.rect(DISPLAY, BOXCOLOR, (left, top, BOXSIZE, BOXSIZE))
    pygame.display.update()
    FPSCLOCK.tick(FPS)

【问题讨论】:

  • 请修正你的缩进。

标签: python python-3.x pygame


【解决方案1】:

这是一个Indentation 问题。在cartesianToPositional 中,return 语句(return (None, None))必须在函数的末尾,而不是在外循环中:

def cartesianToPositional(x, y):

    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): 
                return (boxx, boxy)

    # <--
    return (None, None)

【讨论】:

    【解决方案2】:

    在您的源代码中,您的某个函数会提前返回。

    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)
    

    删除return (none, None) 上的一级缩进,它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 2010-11-17
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多