Я могу проверить только первый столбец двумерного массива. - PullRequest
2 голосов
/ 10 февраля 2020

Я написал эту загадку памяти со спрайтами и у меня есть некоторые ошибки: 1.- Когда я перемещаю мышь над полями, только первый столбец отображает эффект выделения. 2.- Опять же, только первый столбец отображает эффект покрытых блоков

полной программы в 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)

Ответы [ 2 ]

4 голосов
/ 10 февраля 2020

Это Отступ . В cartesianToPositional оператор возврата (return (None, None)) должен находиться в конце функции, а не во внешнем l oop:

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)
3 голосов
/ 10 февраля 2020

В исходном коде у вас есть досрочный возврат в одну из ваших функций.

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), и он должен работать нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...