# 课程 F——条件语句 2——条件与剪刀石头布的游戏

• 年龄 10+
• 65+ 分钟
• MakeCode Editor

## 课堂活动

• 1块micro:bit主板
• 1根USB线
• 1个AAA电池组（可选）
• 2节AAA电池（可选）

### 编写文本

``````if (true) {

} else if (false) {

} else {

}``````

### 如果

``````
let item = 0
item = 0
``````

### 设置项目

``let item = Math.randomRange(0, 10)``

### 随机数

``基本.显示图标(图标名称.心)``

### 显示图标

``输入.手势(手势.摇晃, () => {})``

4个步骤

## 指导练习

``````
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
RandomNumber = Math.randomRange(1, 3)
if (RandomNumber == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (RandomNumber == 2) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
})

input.onButtonPressed(Button.A, () => {

})

input.onButtonPressed(Button.B, () => {

})
``````

Shaking tells the micro:bit to choose either 1, 2, or 3 randomly.

• if (RandomNumber = 1) >> show rock
• if (RandomNumber = 2) >> show paper
• if (RandomNumber = 3) >> show scissors

The finished code is available here.

## 挑战

### 挑战1

``````
let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
RandomNumber = Math.randomRange(1, 3)
if (RandomNumber == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (RandomNumber == 2) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
})

input.onButtonPressed(Button.A, () => {
Score += 1
})

input.onButtonPressed(Button.B, () => {

})
Score = 0
``````

The finished code is available here.

### 挑战2

``````
let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
RandomNumber = Math.randomRange(1, 3)
if (RandomNumber == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (RandomNumber == 2) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
})

input.onButtonPressed(Button.A, () => {
Score += 1
})

input.onButtonPressed(Button.B, () => {
Score += -1
})
Score = 0
``````

The finished code is available here.

### 挑战3

``````
let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
RandomNumber = Math.randomRange(1, 3)
if (RandomNumber == 1) {
basic.showIcon(IconNames.SmallSquare)
} else if (RandomNumber == 2) {
basic.showIcon(IconNames.Square)
} else {
basic.showIcon(IconNames.Scissors)
}
})
input.onButtonPressed(Button.A, () => {
Score += 1
})
input.onButtonPressed(Button.B, () => {
Score += -1
})
input.onButtonPressed(Button.AB, () => {
basic.showNumber(Score)
})
Score = 0
``````

The finished code is available here.