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

关于条件语句的第二课,我们将进一步学习条件语句。

  • Computer-science
  • Conditionals
Print lesson
  • 年龄 10+
  • 65+ 分钟
  • Javascript Blocks

介绍

本系列的第一课是关于条件语句, "天气预测因子",重点讲解”if...then...else'“语句。 如果你还没有讲解到这一课,或学生感到有困难,请尝试再次学习。

条件语句的第二课,我们将进一步学习条件语句。 它使我们能够判断两个以上的结果。 我们用micro:bit来玩剪刀石头布的游戏时将会用到它。 最后的将游戏的分数记录在一起

我们的micro:bit课程是根据 Code.org CS 的基础知识应用而量身定做的。 在学生开始这些课程之前, 我们鼓励学生首先完成所有 CS 基础知识。 学生应该熟悉 Code.org CS 基本原理的条件。

教学指南

开始

Activity

你将需要这些:

  • micro:bit主板
  • USB电缆
  • AAA电池组(可选项)
  • 2-AAA电池盒(可选项)

文档

if (true) {

} else if (false) {

} else {

}

if

条件代码是否运行取决于布尔条件是真还是假


let item = 0
item = 0
            

赋值

使用等号来为变量或者字符串赋值

let item = Math.random(5)

随机

返回 0 到“限值”之间的随机数。

basic.showIcon(IconNames.Heart)

显示图标

在LED 屏幕上显示所选的图标

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

当摇晃的时候

启动事件处理程序 (当发生某些事情时将启动该程序)。当你做手势的时候, 这个处理程序会起作用 (比如摇晃micro:bit)。

4步

指导练习

We will make instructions or code online for the micro:bit. Next, go to https://makecode.microbit.org


let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
    RandomNumber = Math.random(3)
    if (RandomNumber == 0) {
        basic.showIcon(IconNames.SmallSquare)
    } else if (RandomNumber == 1) {
        basic.showIcon(IconNames.Square)
    } else {
        basic.showIcon(IconNames.Scissors)
    }
})
input.onButtonPressed(Button.A, () => {

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

})
        

当摇晃的时候,micro:bit将会显示手头、剪刀或者布的图标 用力晃动模拟器上的micro:bit,或者按震动按钮出发该事件。 晃动后你将会看到一个图标显示出来。

晃动会使micro:bit随机选择0、1或者2。

我们将根据条件作出判断。

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

注意: 在代码的底部和旁边的未使用的积木块,会在在对战中使用。

完成后的代码请见此处: here

挑战

挑战1

Can you track the times you win against the micro:bit?

Hint: Place your blocks in the event handler for on button A pressed. You will need to initialize(create) a variable in the on start event handler to store your score.

View code

let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
    RandomNumber = Math.random(3)
    if (RandomNumber == 0) {
        basic.showIcon(IconNames.SmallSquare)
    } else if (RandomNumber == 1) {
        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.

Challenge 2

What if you lose? Can you subtract from your score when you lose?

Hint: Place your blocks in the event handler for on button B pressed.

View code

let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
    RandomNumber = Math.random(3)
    if (RandomNumber == 0) {
        basic.showIcon(IconNames.SmallSquare)
    } else if (RandomNumber == 1) {
        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.

Challenge 3

How can we show our score?

Hint: Place your blocks in the event handler for on button A+B pressed.

View code

let Score = 0
let RandomNumber = 0
input.onGesture(Gesture.Shake, () => {
    RandomNumber = Math.random(3)
    if (RandomNumber == 0) {
        basic.showIcon(IconNames.SmallSquare)
    } else if (RandomNumber == 1) {
        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.

Independent Practice

Create your own six-sided die with the micro:bit and create an adventure board game. This die could be custom. If you roll a one, what happens?

Example - If you roll one, you meet find some treasure.

Hint: You will need to increase the RandomNumber and create a longer if block.

Quiz

Use these questions to reflect on the activity and challenges.

Question 1

Write the part of the code in the activity that randomly chooses a number from 0 to 2 and stores the value inside a variable called 'RandomNumber'.

Show answer

NOTE: We highlight the block of code in yellow that stores the RandomNumber value inside a variable called RandomNumber. The second part of the code randomly returns a number from 0-3.

Question 2

If the variable 'RandomNumber' is equal to 0, what icon will appear?

Show answer

The code would show Rock.

Question 3

If the variable 'RandomNumber' is NOT equal to 0, what icons could appear?

Show answer

The code would show Scissors and Rock.

Question 4

If the variable 'RandomNumber' is NOT equal to 0 or 1, what icon will appear?

Show answer

The code would show Scissors.