剪刀石头布活动

在这个活动中,让我们一起来做剪刀石头布的游戏吧!

  • Get started
  • MakeCode
  • Random
Print lesson
  • 年龄 8+
  • 30 分钟
  • MakeCode Editor

简介

这个项目教你如何使用“random”(随机)积木块和LED灯来创建剪刀石头布的游戏。

教学指南

打开 打开 teacher resources

课堂活动

8个步骤

第一步

我们想让micro:bit在摇晃的时候选择剪刀石头布。放置一个on shake(摇晃)积木块,这样当你摇晃micro:bit的时候,它就会运行部分程序。


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

第二步

添加一个weapon(武器)变量来存储由 pick random(选择随机)计算出来的随机数。

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 {
      
    }
})
          

第六步

将一个 show leds (显示LED)放置在else if下方,并在屏幕上画出一个 rock(石头)的图像。


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 {

    }
})
          

第七步

将积木块show leds(显示LED)添加到else 部分,在这个积木块上画出一个剪刀的图像。

你不需要检查 weapon是否是 2 ,因为 2012之中唯一被遗留出来的数字。 That’s why you can use an else instead of an else if (click the '+' button to add extra else / else if statements to your condition).


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(`
            # # . . #
            # # . # .
            . . # . .
            # # . # .
            # # . . #
            `)
    }
})
          

Step Eight

游戏完成!叫上你的朋友们一起来玩剪刀石头布吧!

挑战

试着添加下列功能:

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

了解更多

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

Selecting this opens external content from our support system, which adheres to their privacy policy.