# 剪刀石头布活动

• 年龄 8+
• 30 分钟
• MakeCode Editor

## 课堂活动

8个步骤

### 第一步

``````
input.onGesture(Gesture.Shake, () => {

})
``````

### 第二步

When you shake the micro:bit, it should pick a random number from 1 to 3 and store it in the variable weapon. （这个变量命名为 weapon是因为剪刀石头布是你用来击败你的朋友们的武器。）

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
})
``````

In a later step, each of the possible numbers (1, 2, or 3) is matched to its own picture. The picture is shown on the LEDs when its number is picked.

### 第三步

Place an if block under the pick random and check whether weapon is equal to 1.

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
if (weapon == 1) {

}
})
``````

### 第四步

if积木块内，放置一个show leds（显示LED）积木块，让它显示布的图片。

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
if (weapon == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
}
})
``````

### 第五步

Click '+' to add an 'else' and 'else if' section, then add a condition to check whether weapon is equal to 1.

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
if (weapon == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (weapon == 2) {

} else {

}
})
``````

### 第六步

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
if (weapon == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (weapon == 2) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {

}
})
``````

### 第七步

``````
let weapon = 0
input.onGesture(Gesture.Shake, function () {
weapon = Math.randomRange(1, 3)
if (weapon == 1) {
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
} else if (weapon == 2) {
basic.showLeds(`
. . . . .
. # # # .
. # # # .
. # # # .
. . . . .
`)
} else {
basic.showLeds(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #
`)
}
})
``````

• 显示不同选项的动画
• 将它改变为石头剪刀布蜥蜴

## 了解更多

“神奇的按钮”活动介绍了内置的传感器，教你如何测量磁场。