Die Roll Activity

Create virtual dice using the MakeCode editor

• Alter 8+
• 30 min.
• MakeCode Editor

Einführung

This project introduces the students to the selection condition IF...THEN...ELSE and how to control the individual LEDs.

Activity

5 steps

Step One

This is a program that allows the user to shake the micro:bit to show a die face. On the MakeCode editor select the input blocks from the block menu and select the “shake” block.

input.onGesture(Gesture.Shake, () => {

})

Once on the workspace the “on” block can be changed to a number of gestures, such as tilt left or right. For this project the shake gesture is selected. Step Two

Now your program reacts to the shake input gesture we need to add how it responds. In the variables blocks, add a "set item to" block.

let item = 0
input.onGesture(Gesture.Shake, () => {
item = 0
})

Rename the variable to something more suitable, such as “Roll”. Click the arrow next to 'item' and select 'Rename variable'; a popup box will allow you to change the variable name.  Now that a variable called “Roll” has been created we needed to pick a random number between 1 and 6. From the block menu select the “Math” option and select “pick random 0 to 10” Drag the “pick random” block onto the workspace and connect it to the “Set variable” block replacing the “0” with “1” and the “10” with “6”. Now the program will pick a random number between 1 and 6 every time the micro:bit is shaken. The number is stored in the variable “Roll”.

let roll = 0
input.onGesture(Gesture.Shake, function () {
roll = Math.randomRange(1, 6)
})

Step Three

Now the micro:bit needs to display a different result based upon the “roll” variable. This is known as selection. In this case we will be using the IF...THEN...ELSE section. To add selection to the micro:bit it can be found on the block menu under the Logic option. Add a logic statement of “IF THEN ELSE” and click the "+" button to edit the logic to have multiple conditions.

let roll = 0
input.onGesture(Gesture.Shake, function () {
roll = Math.randomRange(1, 6)
if (true) {

} else {

}
})

As there are 6 possible options, 4 “else if” statements need to be added. Since there is the first “If” statement followed by 4 “else if” statements and the final “else” statement, that covers 6 options in total.

let roll = 0
input.onGesture(Gesture.Shake, function () {
roll = Math.randomRange(1, 6)
if (true) {

} else if (false) {

} else if (false) {

} else if (false) {

} else if (false) {

} else {

}
})

Step Four

Now we need to create a logical statement for the IF statement to be based upon to allow for selection. Again from the “logic” option on the block menu select the “equals” block. Once selected drag the “equals” block onto the “IF” statement block.

To ensure the micro:bit bases the IF statement on the random number, the value stored in the variable “Roll” needs to be checked. The “Roll” variable can be found from the variable option in the block menu. The first of the 6 checks to carry out is if the variable “Roll” is equal to 1. Place the “Roll” variable within the “is equal to” (=) block.

Now this process needs to be repeated for the other 4 “else if” statements. By right clicking on the “equals” block it can be duplicated and modified for the different possible values. let roll = 0
input.onGesture(Gesture.Shake, function () {
roll = Math.randomRange(1, 6)
if (roll == 1) {

} else if (roll == 2) {

} else if (roll == 3) {

} else if (roll == 4) {

} else if (roll == 5) {

} else {

}
})

Step Five

The final stage of the program is now to display a different pattern for the different die faces. From the basic option in the block menu, select the “show leds” block. Drag the “Show leds” block onto the workspace and place under the first “IF” statement. By clicking on the dark blue squares, you can create the die pattern required ('on' LEDs are shown by the white colour). For this example, the pattern for 1 is completed below. This block again can be duplicated 5 times for the other die faces until all options have been completed. The final program is shown below. Save the project and download to the micro:bit.

let roll = 0
input.onGesture(Gesture.Shake, function () {
roll = Math.randomRange(1, 6)
if (roll == 1) {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
} else if (roll == 2) {
basic.showLeds(`
. . . . .
. . . # .
. . . . .
. # . . .
. . . . .
`)
} else if (roll == 3) {
basic.showLeds(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . #
`)
} else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .
`)
} else if (roll == 5) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .
`)
} else {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .
`)
}
})

Challenge

Try to add the following functionality:

• Add an animation of the die faces rolling.
• Allow the user to state the number of die faces i.e. 6, 8 or 12

Erfahre mehr

The 'Truth or Dare' activity uses the gesture command, LED screen and introduces IF statements again, but runs two programming loops at the same time.