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

2024-04-26 01:33:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我用精灵写了记忆拼图,我有一些错误: 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)

Tags: theinboxforifistopdef
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)上的一级缩进,它应该可以正常工作

这是一个Indentation问题。在cartesianToPositional中,返回语句(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)

相关问题 更多 >

    热门问题