pxt-calliope/docs/lessons/dice-roll/activity.md

170 lines
2.7 KiB
Markdown
Raw Normal View History

2016-05-27 00:24:10 +02:00
# dice roll activity
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
Create a dice on the @boardname@
2016-03-26 00:47:20 +01:00
## ~avatar avatar
2016-03-26 00:47:20 +01:00
2016-05-27 00:24:10 +02:00
Welcome! This tutorial will help you create a dice. Let's get started!
2016-03-26 00:47:20 +01:00
## ~
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
Let's create a condition for when the @boardname@ is shaken.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
2016-05-27 00:24:10 +02:00
We need to show a random value from 1 to 6 on our dice. So let's make a local variable called **roll**.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6)
2016-03-26 00:47:20 +01:00
})
```
2016-05-27 00:24:10 +02:00
We need a condition for if **roll** is 5. We will show a `6` if **roll** is 5 because **roll** has a range from 0 to 5. We can use `show LEDs` to display the side of a dice that shows 6.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
});
```
2016-05-27 00:24:10 +02:00
Let's use an `else if` condition for if **roll** is 4. If **roll** is 4 we can show 5 dots on the dice.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, ()=> {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
});
```
2016-05-27 00:24:10 +02:00
Now we need to repeat the same steps for if **roll** is 3. If **roll** is 3 we will show `4` on the dice.
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
else if (roll == 3) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .`);
}
});
```
2016-05-27 00:24:10 +02:00
Let's also repeat these steps to show the 3, 2, and 1 on the dice. We are almost done with our dice!
2016-03-26 00:47:20 +01:00
```blocks
input.onGesture(Gesture.Shake, () => {
2018-04-21 19:52:09 +02:00
let roll = Math.randomInt(6);
2016-03-26 00:47:20 +01:00
if (roll == 5) {
basic.showLeds(`
. # . # .
. . . . .
. # . # .
. . . . .
. # . # .`);
}
else if (roll == 4) {
basic.showLeds(`
. . . . .
. # . # .
. . # . .
. # . # .
. . . . .`);
}
else if (roll == 3) {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # . # .
. . . . .`);
}
else if (roll == 2) {
basic.showLeds(`
# . . . .
. . . . .
. . # . .
. . . . .
. . . . #`);
}
else if (roll == 1) {
basic.showLeds(`
. . . . .
. # . . .
. . . . .
. . . # .
. . . . .`);
}
else {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .`);
}
});
```
## ~avatar avatar
2016-03-26 00:47:20 +01:00
2016-05-27 00:24:10 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/dice-roll/challenges)!
2016-03-26 00:47:20 +01:00
## ~
2016-03-26 00:47:20 +01:00