Micro:Bit Week 2, Day 4

Extension Link

https://github.com/jhlucky/maqueen

Code

radio.onReceivedNumber(function (receivedNumber) {

    if (radio.receivedPacket(RadioPacketProperty.SignalStrength) > -47) {

        health = health - 1

    }

})

input.onButtonPressed(Button.A, function () {

    basic.showNumber(health)

})

input.onButtonPressed(Button.AB, function () {

    health = 5

})

let health = 0

radio.setGroup(1)

health = 10

basic.forever(function () {

    if (health > 5) {

        basic.showIcon(IconNames.Heart)

    } else if (health > 0) {

        basic.showIcon(IconNames.Sad)

        radio.sendNumber(1)

        basic.pause(1000)

    } else {

        basic.showIcon(IconNames.Ghost)

        maqueen.motorStopAll()

    }

    if (maqueen.sensor(PingUnit.Centimeters) < 30 && health > 0) {

        if (Math.randomBoolean()) {

            maqueen.MotorRun(maqueen.aMotors.M1, maqueen.Dir.CCW, 125)

maqueen.MotorRun(maqueen.aMotors.M2, maqueen.Dir.CW, 125)

        } else {

            maqueen.MotorRun(maqueen.aMotors.M1, maqueen.Dir.CW, 125)

maqueen.MotorRun(maqueen.aMotors.M2, maqueen.Dir.CCW, 125)

        }

        basic.pause(250)

    } else if (health > 0) {

        maqueen.MotorRun(maqueen.aMotors.M1, maqueen.Dir.CW, health * 25)

maqueen.MotorRun(maqueen.aMotors.M2, maqueen.Dir.CW, health * 25)

    }

})