pxt-calliope/olddocs/lessons/spinner/activity.md

111 lines
2.1 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.
### ~avatar avatar
2016-05-27 00:24:10 +02:00
2016-03-26 00:47:20 +01:00
### ~
Welcome! This guided tutorial will teach how to program a script that randomly points to a player. Let's get started!
Let's begin by adding an `on shake` condition to know when the micro:bit is shaken.
2016-03-31 00:11:05 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
})
```
2016-03-26 00:47:20 +01:00
Now let's randomly generate a number from 0 to 3 so that we can randomly display an arrow in a given direction.
2016-03-31 00:11:05 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let randomArrow = Math.random(4)
2016-04-01 00:51:42 +02:00
if (randomArrow == 3) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`)
}
})
```
2016-03-26 00:47:20 +01:00
Now let's handle each of the cases by displaying the appropriate arrow. (Let's display an up arrow if `random arrow` is 0.
2016-03-31 00:11:05 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let randomArrow = Math.random(4)
2016-04-01 00:51:42 +02:00
if (randomArrow == 3) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`)
}
2016-04-01 00:51:42 +02:00
if (randomArrow == 2) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. . # . .
# # # # #
. # # # .
. . # . .
`)
}
})
```
2016-03-26 00:47:20 +01:00
Now let's handle the rest of the cases for `random arrow`.
2016-03-31 00:11:05 +02:00
```blocks
input.onGesture(Gesture.Shake, () => {
let randomArrow = Math.random(4)
2016-04-01 00:51:42 +02:00
if (randomArrow == 3) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. # # # .
# # # # #
. . # . .
. . # . .
`)
}
2016-04-01 00:51:42 +02:00
if (randomArrow == 2) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. . # . .
# # # # #
. # # # .
. . # . .
`)
}
2016-04-01 00:51:42 +02:00
if (randomArrow == 1) {
2016-03-31 00:11:05 +02:00
basic.showLeds(`
. . # . .
. # # . .
# # # # #
. # # . .
. . # . .
`)
}
})
```
2016-03-26 00:47:20 +01:00
### ~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
### ~