# 魔术8 游戏

Make a fun question-based game in MakeCode!

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

## 课堂活动

7个步骤

### 第一步

``````
``````

``````
basic.showNumber(8)
``````

### 第二步

``````
input.onGesture(Gesture.Shake, function () {

})
``````

``````
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
})
``````

### 第三步

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = 0
})
``````

### 第四步

Now that the variable is set to “0” when the micro:bit is shaken, we now need to assign a random number to it. From the block menu select the “Math” option and select “pick random 0 to 10”

Drag the “pick random” block onto the workspace and join to the “set random” block. A random number between 1 and 3 will give us the 3 options needed, so change the number “0” to “1” and “10” to “3”.

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

### 第五步

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = Math.randomRange(1, 3)
if (true) {

} else {

}
})
``````

As there are more than 2 conditions within this selection another selection statement is required, an “else if”. Click '+' on the if block to add extra else and else if statements.

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = Math.randomRange(1, 3)
if (true) {

} else if (false) {

} else {

}
})
``````

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

} else if (false) {

} else {

}
})
``````

### 第六步

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

} else if (false) {

} else {

}
})
``````

So the micro:bit can switch between three options, click '+' in the 'if' block so that the block has 1 'if', 1 'else if', and 1 'else' statement. Your code should look like this:

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

} else if (Random == 2) {

} else {

}
})
``````

### 第七步

“if”积木块中的每个“then”部分都要有一个“show string”积木块。 从菜单中拖拽出3或者在工作区域右击积木块一次来复制它。 字符串应该修改成“yes”（是的）、“no”（不是）和“I don't know”（我不知道）。

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = Math.randomRange(1, 3)
if (Random == 3) {
basic.showString("YES")
} else if (Random == 2) {
basic.showString("NO")
} else {
basic.showString("I DON'T KNOW")
}
})
``````

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = Math.randomRange(1, 3)
if (Random == 3) {
basic.showString("YES")
} else if (Random == 2) {
basic.showString("NO")
} else {
basic.showString("I DON'T KNOW")
}
basic.showNumber(8)
})
``````

``````
let Random = 0
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
Random = Math.randomRange(1, 3)
if (Random == 3) {
basic.showString("YES")
} else if (Random == 2) {
basic.showString("NO")
} else {
basic.showString("I DON'T KNOW")
}
basic.showNumber(8)
})