Die Roll Activity

Create virtual dice using the JavaScript Blocks Editor

  • Get started
  • JavaScript Blocks
  • Inputs
  • Random
  • Variables
Print lesson
  • Kor 8+
  • 30 perc
  • JavaScript Blocks

Bevezetés

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

Teacher Guide

Open

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 JavaScript Blocks editor select the input blocks from the block menu and select the “shake” block.


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.


Rename the variable to something more suitable, such as “Roll”. 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 0 and 5. This is 6 different values in total. From the block menu select the “Math” option and select “pick random 0 to 4”


Drag the “pick random” block onto the workspace and connect it to the “Set variable” block replacing the “0”. Now the program will pick a random number between 0 to 5 every time the micro:bit is shaken. The number is stored in the variable “Roll”.


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 use the cog to edit the logic to have multiple conditions.


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.

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” contains 0. Place the “Roll” variable within the “equal” 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.


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 light blue coloured squares, you can create the die pattern required. 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.

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

Learn More

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.