Skip to main content
Engineering LibreTexts

6.10: The Two States of the Game Loop

  • Page ID
  •         if not waitingForInput:
                # play the pattern
                pattern.append(random.choice((YELLOW, BLUE, RED, GREEN)))
                for button in pattern:
                waitingForInput = True

    There are two different "modes" or "states" that the program can be in. When waitingForInput is False, the program will be displaying the animation for the pattern. When waitingForInput is True, the program will be waiting for the user to select buttons.

    Lines 1 [97] to 9 [105] will cover the case where the program displays the pattern animation. Since this is done at the start of the game or when the player finishes a pattern, line 5 [101] will add a random color to the pattern list to make the pattern one step longer. Then lines 6 [102] to 8 [104] loops through each of the values in the pattern list and calls flashButtonAnimation() which makes that button light up. After it is done lighting up all the buttons in the pattern list, the program sets the waitingForInput variable to True.