Don't have dice around when you want to play a board game, just write a little program for your Sphero. This program is very simple, but adds a few new features: events, random, and output. We'll see more event based options in Hot Potato and the Maze programs, but the important concept is that when certain events happen you can have Sphero do something.
We already had on start program events in each of the other programs, but this adds on collision, and a couple of actions after that. We get a main LED that turns blue to let us know a collision has occurred and then some "spoken" feedback or output. That output happens to be a random number 1-6, just like rolling a dice.
You'll notice this program's first block is to turn stabilization off, try turning it on and see the difference in how Sphero behaves.
Blocks Types: Movements, Lights, Sounds, Operators, Events
I like pigs, so let's modify our program to play a pig sound anytime the output of a collision is the number one.
We've added a variable (n) to hold the value of the output whenever there is a collision and then we ask a question about this value of this variable. We ask a question in Sphero world with an If...Then or an If...Then...Else block. Here we are asking if a condition (n === 1) is true. This is like asking if n is equal to 1. In this example, if n is 1 then we play a pig sound, but if it isn't then we move to the else part of the block. If n is not equal to 1 then we output a string that says, "You rolled a " and the value of variable n.
Blocks Types: Movements, Lights, Sounds, Controls, Operators, Comparators, Events, Variables
This is is very similar to Roll the Pig, but now we have nested If...Then...Else statements. This allows us to see if the variable (n) is equal to 1 or equal to 2 and play a different barnyard sound for each. Can you modify this program to play a different barnyard sound for each integer that n could be (1 - 6)?
Hint: You'll need a total of 5 If...Then...Else blocks to make it work and you won't have any speak blocks.
Blocks Types: Movements, Lights, Sounds, Controls, Operators, Comparators, Events, Variables
When you have lots of nested If...Then...Else blocks, it starts to get really busy and hard to read. Instead you can write a function and move all of those nested statements inside of the function. Look how clean this looks! Can you write this program and the function that it uses?
Hint: Here is the function!
Blocks Types: Movements, Lights, Sounds, Controls, Operators, Comparators, Events, Variables, Functions