Skip to main content
Engineering LibreTexts

4.40: The “Game Won” Animation

  • Page ID
  • def gameWonAnimation(board):
        # flash the background color when the player has won
        coveredBoxes = generateRevealedBoxesData(True)
        color1 = LIGHTBGCOLOR
        color2 = BGCOLOR
        for i in range(13):
            color1, color2 = color2, color1 # swap colors
            drawBoard(board, coveredBoxes)

    When the player has uncovered all of the boxes by matching every pair on the board, we want to congratulate them by flashing the background color. The for loop will draw the color in the color1 variable for the background color and then draw the board over it. However, on each iteration of the for loop, the values in color1 and color2 will be swapped with each other on line 8 [276]. This way the program will alternate between drawing two different background colors.

    Remember that this function needs to call pygame.display.update() to actually make the DISPLAYSURF surface appear on the screen.