pxt-calliope/docs/reference/js/lessons/spinner/activity.md

86 lines
1.5 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# spinner activity
create an arrow that randomly points to a player. #docs
Let's begin by adding an `input->on shake` condition to know when the BBC micro:bit is shaken.
```
input.onGesture(Gesture.Shake, () => {
})
```
Now let's randomly generate a number from 0 to 3 so that we can randomly display an arrow in a given direction.
```
input.onGesture(Gesture.Shake, () => {
let randomArrow = Math.random(4)
})
```
Now let's handle each of the cases by displaying the appropriate arrow. (Let's display an up arrow if `random arrow` is 0.
```
input.onGesture(Gesture.Shake, () => {
let randomArrow1 = Math.random(4)
if (randomArrow1 == 0) {
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`, 400)
}
})
```
Now let's handle the rest of the cases for `random arrow`.
```
input.onGesture(Gesture.Shake, () => {
let randomArrow2 = Math.random(4)
if (randomArrow2 == 0) {
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`, 400)
} else if (randomArrow2 == 1) {
basic.showLeds(`
. . # . .
. . # . .
# # # # #
. # # # .
. . # . .
`, 400)
}
else if (randomArrow2 == 2) {
basic.showLeds(`
. . # . .
. . # # .
# # # # #
. . # # .
. . # . .
`, 400)
}
else if (randomArrow2 == 3) {
basic.showLeds(`
. . # . .
. # # . .
# # # # #
. # # . .
. . # . .
`, 400)
}
})
```
### ~avatar avatar
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/spinner/challenges)!
2016-03-26 00:47:20 +01:00
### ~