Die Roll Activity

Create virtual dice using the MakeCode editor

• 대상 연령 8+
• 30 예상 시간(분)
• MakeCode Editor

소개

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

활동

5단계

1단계

이 프로그램은 micro:bit을 흔들어 주사위 화면을 보여주는 프로그램입니다. MakeCode editor에서 "입력" 블록 메뉴를 선택한 뒤 "흔들림 감지하면 실행" 블럭을 가져옵니다.

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

})

"흔들림 감지하면 실행" 블럭의 "흔들림" 을 "왼쪽으로 기울임" 또는 "오른쪽으로 기울임"으로 변경할 수도 있습니다. 이 프로젝트에서는 "흔들림" 동작을 사용합니다. 2단계

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

더 알아보기

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.