美文网首页
Microbit 开发一个电子宠物

Microbit 开发一个电子宠物

作者: _黑冰_ | 来源:发表于2019-05-29 11:13 被阅读0次

    功能

    • 开机时显示狗脸,年龄为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

    相关文章

      网友评论

          本文标题:Microbit 开发一个电子宠物

          本文链接:https://www.haomeiwen.com/subject/dduetctx.html