Shaking All Over
On the last page, we used code blocks to infer the pitch and roll of the micro:bit from the accelerometer readings. We can also use the accelerometer to detect when the micro:bit is being shaken.
Making A Die
There is a die rolling script tutorial for Code Kingdoms. The same effect can be achieved quite simply using the block editor and that is what is shown here. When the user shakes the micro:bit, a pseudorandom number will be generated and displayed on the LED matrix.
The pick random block generates numbers in a range starting at 0. If we generate the numbers 0-5 and add 1, we get a die roll from 1 to 6.
Notice that there are two blocks for displaying information on the matrix. One of them is for strings (text), another is for numeric values. We need to use that one here or the script will not work.
It doesn't look much like the dice you normally use though. The font for writing numbers on a 5x5 grid isn't the prettiest thing in the world. Here is a snippet from a slightly longer script that displays the results using dot patterns,
Obviously, the script continues beyond what is shown in the image. Each dot pattern needs to be defined in a similar way to the two that are shown here.
- Finish the dot pattern display that you can see in the example above.
- Use the Code Kingdoms editor to achieve the same effect. Which editor was most convenient for the process? Does one of the editors have more powerful or more flexible statements for doing this?