Truth or Dare

Learn about random numbers, inputs, and outputs in this fun activity!

• גיל 8+
• 30 דק'
• MakeCode Editor

הקדמה

This project teaches you how to use button inputs and LED outputs to create a fun 'truth or dare' game using the micro:bit!

Activity

5 steps

Step One

This program will show an arrow to select the player following a spin of the micro:bit. The user then presses the A button to see a randomly generated statement choosing from either Truth or Dare.

The first stage is to create the arrow. From the basic option in the block menu, select the “show leds” block. Drag the “Show leds” block onto the workspace and draw the pattern by clicking on the boxes that you want to light up. A white square indicates LED on. To ensure the LEDS are always shown, the LEDs are to be placed within a forever loop. The loop can be found from the basic option from the block menu. Drag the block and place around the LEDs. ``````
basic.forever(function () {
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`)
})
``````

Step Two

The next part of the program displays a random statement once the user has pressed a button. Select the Input option from the block menu and find the “On button press” block and drag onto the workspace. The result of the random function needs to be stored within a variable. From the block menu select the “Variable” and click “Make a Variable”. A popup dialogue box will then ask you to name your variable. In this example name it “Random”. Using the “set item” block, drag it onto the workspace and connect to the button “A” input block. ``````
let item = 0
input.onButtonPressed(Button.A, function () {
item = 0
})
``````

Step Three

Now the variable “Random” has been created this can be found on the “Set item” drop down menu by pressing the down arrow and selecting “Random”.

``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = 0
})
``````

Now that the variable is set to “0” when the A button is pressed, we now need to assign a random number to it. From the block menu select the “Math” option and select “pick random 0 to 10” Drag the “pick random” block onto the workspace and join to the “set random” block. A random number between 0 and 1 will give us the 2 options needed.

``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = Math.randomRange(0, 1)
})
``````

Step Four

Now we include a selection process using the IF statements found within the “logic” option within the block menu. Drag the “IF” block into the workspace and connect it to follow the “random” block; click '+' to add 'else' to the block.

``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = Math.randomRange(0, 1)
if (true) {

} else {

}
})
``````

Next there 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, add an “equals” block. ``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = Math.randomRange(0, 1)
if (0 == 0) {

} else {

}
})
``````

Step Five

To ensure the micro:bit bases the IF statement on the random number, the value stored in the variable “Random” needs to be checked. The “Random” variable can be found from the 'variables' option in the block menu. The “Random” variable block is placed in the “equals” block to test the IF statement logic. If the “Random” variable contains a “0” the first option is selected otherwise the second option is chosen.

``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = Math.randomRange(0, 1)
if (Random == 0) {

} else {

}
})
``````

To get the "Truth or Dare" statements displayed on the screen the “show string” block is required from the basic option on the block menu. The program is completed and both elements will work together at the same time, displaying the led arrow pattern, when the button hasn’t been pressed.

``````
let Random = 0
input.onButtonPressed(Button.A, function () {
Random = Math.randomRange(0, 1)
if (Random == 0) {
basic.showString("Truth")
} else {
basic.showString("Dare")
}
})
``````