4.40: The “Game Won” Animation

Making Games with Python and Pygame · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    4.40: The “Game Won” Animation
    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
            DISPLAYSURF.fill(color1)
            drawBoard(board, coveredBoxes)
            pygame.display.update()
            pygame.time.wait(300)
    

    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.


    This page titled 4.40: The “Game Won” Animation was last modified on Wed, 13 Nov 2019 03:13:58 GMT and is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Sweigart via source content that was edited to the style and standards of the LibreTexts platform.