updated lessons
This commit is contained in:
@ -12,19 +12,95 @@ Welcome! This guided tutorial will teach how to program a script that randomly p
|
||||
|
||||
Let's begin by adding an `on shake` condition to know when the micro:bit is shaken.
|
||||
|
||||

|
||||
```blocks
|
||||
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.
|
||||
|
||||

|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(4)
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
Now let's handle each of the cases by displaying the appropriate arrow. (Let's display an up arrow if `random arrow` is 0.
|
||||
|
||||

|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(4)
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 2) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . # . .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
Now let's handle the rest of the cases for `random arrow`.
|
||||
|
||||

|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(4)
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 2) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . # . .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 1) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # . .
|
||||
# # # # #
|
||||
. # # . .
|
||||
. . # . .
|
||||
`)
|
||||
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
|
@ -6,21 +6,164 @@ Create an arrow that randomly points to a player.
|
||||
|
||||
Complete the following [guided tutorial](/microbit/lessons/spinner/activity), your code should look like this:
|
||||
|
||||

|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(4)
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 2) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . # . .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 1) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # . .
|
||||
# # # # #
|
||||
. # # . .
|
||||
. . # . .
|
||||
`)
|
||||
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Challenge 1
|
||||
|
||||
Modify the random number generator so that it can include new arrows we will create in the next challenge.
|
||||
|
||||

|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(8)
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 2) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . # . .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 1) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # . .
|
||||
# # # # #
|
||||
. # # . .
|
||||
. . # . .
|
||||
`)
|
||||
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
* Do **not** run the code yet because it will not work until you have conditions for every random number.
|
||||
|
||||
### Challenge 2
|
||||
|
||||
Let's add four more arrows that point diagonally.
|
||||
Let's add more arrows that point diagonally.
|
||||
|
||||
|
||||
```blocks
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let randomArrow = Math.random(8)
|
||||
if (randomArrow = 7) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # # .
|
||||
# # # # #
|
||||
. . # . .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 6) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . # . .
|
||||
# # # # #
|
||||
. # # # .
|
||||
. . # . .
|
||||
`)
|
||||
}
|
||||
if (randomArrow = 5) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. # # . .
|
||||
# # # # #
|
||||
. # # . .
|
||||
. . # . .
|
||||
`)
|
||||
|
||||
}
|
||||
if (randomArrow = 4) {
|
||||
basic.showLeds(`
|
||||
. . # . .
|
||||
. . . # .
|
||||
# # # # #
|
||||
. . . # .
|
||||
. . # . .
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
if (randomArrow = 3) {
|
||||
basic.showLeds(`
|
||||
# # # # #
|
||||
# # # # .
|
||||
# # # # .
|
||||
# . . # .
|
||||
. . . . #
|
||||
`)
|
||||
|
||||
}
|
||||
if (randomArrow = 2) {
|
||||
basic.showLeds(`
|
||||
# # # # #
|
||||
# # # # #
|
||||
. . # # #
|
||||
. # . # #
|
||||
# . . . #
|
||||
`)
|
||||
|
||||
}
|
||||
if (randomArrow = 1) {
|
||||
basic.showLeds(`
|
||||
# . . . #
|
||||
# # . # .
|
||||
# # # . .
|
||||
# # # # .
|
||||
# # # # #
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
```
|
||||
|
||||

|
||||
|
||||
* Run your code to see if it works as expected
|
||||
|
||||
|
Reference in New Issue
Block a user