6.11: Figuring Out if the Player Pressed the Right Buttons
- Page ID
else: # wait for the player to enter buttons if clickedButton and clickedButton == pattern[currentStep]: # pushed the correct button flashButtonAnimation(clickedButton) currentStep += 1 lastClickTime = time.time() if currentStep == len(pattern): # pushed the last button in the pattern changeBackgroundAnimation() score += 1 waitingForInput = False currentStep = 0 # reset back to first step elif (clickedButton and clickedButton != pattern[currentStep]) or (currentStep != 0 and time.time() - TIMEOUT > lastClickTime):
True, then the code in line 1's 
else statement will execute. Line 3  checks if the player has clicked on a button during this iteration of the game loop and if that button was the correct one. The
currentStep variable keeps track of the index in the pattern list for the button that the player should click on next.
For example, if pattern was set to
[YELLOW, RED, RED] and the
currentStep variable was set to
0 (like it would be when the player first starts the game), then the correct button for the player to click would be
pattern (the yellow button).
If the player has clicked on the correct button, we want to flash the button the player clicked by calling
flashButtonAnimation() then, increase the
currentStep to the next step, and then update the
lastClickTime variable to the current time. (The
time.time() function returns a float value of the number of seconds since January 1st, 1970, so we can use it to keep track of time.)
Lines 9  to 14  are inside the
else statement that started on line 1 . If the execution is inside that else statement, we know the player clicked on a button and also it was the correct button. Line 9  checks if this was the last correct button in the pattern list by checking if the integer stored in
currentStep is equal to the number of values inside the pattern list.
If this is
True, then we want to change the background color by calling our
changeBackgroundAnimation(). This is a simple way to let the player know they have entered the entire pattern correctly. The score is incremented,
currentStep is set back to
0, and the
waitingForInput variable is set to
False so that on the next iteration of the game loop the code will add a new Color value to the pattern list and then flash the buttons.
If the player did not click on the correct button, the
elif statement on line 16  handles the case where either the player clicked on the wrong button or the player has waited too long to click on a button. Either way, we need to show the "game over" animation and start a new game.
(clickedButton and clickedButton != pattern[currentStep]) part of the
elif statement’s condition checks if a button was clicked and was the wrong button to click. You can compare this to line 3's 
if statement’s condition
clickedButton == pattern[currentStep] which evaluates to
True if the player clicked a button and it was the correct button to click.
The other part of line 16's 
elif condition is
(currentStep != 0 and time.time() - TIMEOUT > lastClickTime). This handles making sure the player did not "time out". Notice that this part of the condition has two expressions connected by an and keyword. That means both sides of the and keyword need to evaluate to
In order to "time out", it must not be the player’s first button click. But once they’ve started to click buttons, they must keep clicking the buttons quickly enough until they’ve entered the entire pattern (or have clicked on the wrong pattern and gotten a "game over"). If
currentStep != 0 is
True, then we know the player has begun clicking the buttons.