Microbit 开发一个电子宠物

功能

  • 开机时显示狗脸,年龄为0,精力为0
  • 按A键喂食增长精力直到10,小于6的时候显示哭脸,大于6显示笑脸,到10显示心形
  • 喂饱之后(即精力大于6)每年龄 * 10秒长大一岁
  • 按B键查看年龄
  • 按A+B查看精力

代码

let days = 0
let full = 0
let age = 0
input.onButtonPressed(Button.A, function () {
    if (full < 10) {
        full += 1
    }
})
input.onButtonPressed(Button.B, function () {
    basic.showNumber(age)
})
input.onButtonPressed(Button.AB, function () {
    basic.showNumber(full)
})
basic.showLeds(`
    . . . . .
    # # . # #
    # # . # #
    . . . . .
    . # # # .
    `)
age = 0
full = 0
basic.forever(function () {
    if (full < 6) {
        basic.showIcon(IconNames.Sad)
    } else if (full < 10) {
        basic.showIcon(IconNames.Happy)
    } else {
        basic.showIcon(IconNames.Heart)
    }
    if (full >= 6) {
        days = age * 10000
        game.startCountdown(days)
        age += 1
    }
})


在线编辑器

microbit editor

转载于:https://www.jianshu.com/p/b6cc0ace8035

猜你喜欢

转载自blog.csdn.net/weixin_33824363/article/details/91210526