Scratch Project 5: Simon Says

Description: Create an interactive game using the Finch as a controller.

Programming Concepts: Using lists, generating random numbers, using complicated systems of if statements

Difficulty: Advanced

Approx. Time: 30 Minutes

Programming steps

In this game, the Scratch cat will tell the player how to orient the Finch. If the cat starts his statement with "Simon says", then the player must follow his instructions. For example, if the cat says "Simon says left wing down", then the player must hold the Finch on its side with its left wing pointed down. If the cat does not start his statement with "Simon says", then the player should not follow the instruction.

1. Let's start with a "When space key pressed" block to trigger the program.

2. Go to the "More Blocks" category and find the "Finch Orientation" block. Tilt the Finch in some direction and then click the "Finch Orientation" block. You should get outputs like "Beak_Up", "Left_Wing_Down", and "Level".

3. In order for the cat to tell the user how to orient the Finch, we need to give the program a list of all the possible Finch orientations. The program will then be able to pick a random entry from that list and display it (following the words "Simon says"). Go to the "Data" category and click "Make a List".

4. Name the list "Commands".

5. A list is like a variable that can hold multiple pieces of information instead of just one. The first thing our program needs to do is load the possible orientations into the list. Add 6 "add thing to Commands" blocks to the program and make them add "Beak_Up", "Beak_Down", "Left_Wing_Down", "Right_Wing_Down", "Upside_Down", and "Level" to the list.

6. If we run the program multiple times, we don't want the commands to be added each time. The commands should only be added if they are not on the list yet. We can do this by placing the "add to Commands" blocks inside an if statement that checks to see if the list has fewer than 6 elements. Use a "length of" block from the "Data" category.

7. Add a forever loop below the if so we can give each game multiple "Simon says" statements.

8. We now want to pick an item from the list randomly. We need the program to remember which item was picked so that we can later see if the user responded correctly. We can create a new variable to do this. Name the variable "Target Orientation".

9. Now set the target orientation to be an item from the list. Place an "item 1 of Commands" block within a "set Target Orientation to" block.

10. To select the item randomly, go to the "Operators" category and insert a "pick random" block into the slot where the 1 is.

11. Make the random number be a value from 1 to 6.

12. To make the cat say the command, grab a "say" block from the "Looks" category.

13. Place a "join" block from the "Operators" category inside the "say" block.

14. Type "Simon Says " in the first part of the join block and put a Target Orientation variable block in the second part.

15. Connect a "wait 1 secs" block below the "say" block to give the user time to orient the Finch. Change the time to 2 seconds.

16. If the Finch Orientation matches the Target Orientation, the user responded correctly and the LED should flash green. If not, the game ends and the LED should flash red. Add an if/else statement that compares the Target Orientation to the value of the Finch Orientation block.

17. In the if section, make the LED flash and the Finch buzz using the following block configuration:

18. In the else section, add these blocks to make the LED flash red:

19. To make the game end, go to the "Control" category and add a "stop all" block to the end of the else section.

20. Select "this script" instead of "all" from the stop block.

21. Run the program and try it out! The game should be playable, but it still could use some adjustments. Let's make the program keep track of how many commands the user responds to correctly. The cat will then announce this score at the end of the game.

22. Create a new variable named "Score".

23. When the program starts, set Score to 0.

24. Each time the Finch is oriented correctly, increase the Score by 1.

25. At the end of the game, the Scratch cat should say "You got # in a row!", where # is the player's score. Add a "say" block from the "Looks" category before the "stop" block.

26. Connect a "join" block (from "Operators") to the "say" block.

27. Add another "join" block to the first section of the original "join" block. This will let us join 3 pieces of text together.

28. In the first section, type "You got ". It is important to have a space after the word "got".

29. In the second section, connect a "Score" block from the "Data" category.

30. Type " in a row!" into the third section.

31. Run the program and the cat should announce the score at the end of the game.

Skilled players might find the game too easy after a while and get bored. To make the game more interesting, we can make the difficulty gradually increase. Instead of giving the user 2 seconds to orient the Finch, we can make this time smaller as the user's score increases.

32. Find the "wait 2 secs" block below the first "say" block and add a "-" block from "Operators" and a "Score" block from "Data" in the following arrangement:

33. This will make the time get much smaller, but too quickly. Divide the score by 10 to make the increase in difficulty more gradual.

34. The game should be substantially more difficult now. If you think it is too difficult, simply increase the number that Score is being divided by.

Trick statements

In a real game of Simon says, part of the challenge is avoiding trick statements that do not begin with the phrase "Simon says". This section shows how to add this feature to your program.

1. Create a new variable called "Trick Statement" to keep track of if we are trying to trick the player by using a phrase that does not begin with "Simon Says".

After we set the "Target Orientation" to a random orientation, we should set "Trick Statement" to 1 if we will try to trick the player and 0 if the statement is not a trick. We want most of our statements to not be trick statements, so "Trick Statement" should be set to 0 most of the time.

2. Add the following if statement before the first "say" block to set "Trick Statement" to 1 one fifth of the time:

3. If "Trick Statement" equals 1, we should exclude the text "Simon Says ". Replace the first "say" block with the following if statement to achieve this:

4. All we have to do now is determine if the user responded correctly. We could try to turn this into one long if statement involving many "and", "or", and "not" blocks, but our code will be a lot more understandable if we break it down into multiple if statements. To keep track of the result of these statements, we will need a new variable. Name this variable "Correct Response".

5. Before the final if statement, set "Correct Response" to 0.

6. If the player has oriented the Finch to the Target Orientation and it is not a Trick Statement, "Correct Response" should be set to one. Add the following if statement below the set block to do this.

7. In addition, if the Finch's orientation does not match the Target Orientation, but the statement was a Trick Statement, "Correct Response" should also be set to 1.

If neither of these conditions are true, "Correct Response" will remain at 0.

8. Finally, if "Correct Response" equals 1, the Finch should buzz and increase the score, else, the game ends. Replace the "Target Orientation = Finch Orientation" block on the last if statement with "Correct Response = 1" to complete the project.

Example file

example-file-5-simon-says.sb2