To program a frisky electronic pet, we first let the micro:bit – in addition to let it draw a friendly face – play the sound ||music:hello||
when the micro:bit starts processing our program.
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # # # .
. . . . .
`)
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # # # .
. . . . .
`)
soundExpression.hello.playUntilDone()
To keep track of how long our electronic pet is being ignored, we let the micro:bit count in ||Counter:one-second intervals||
, starting with 1
at the very beginning of our program's execution.
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # # # .
. . . . .
`)
soundExpression.hello.playUntilDone()
Counter.startFromMin(1, Intervals.Seconds)
To breathe life into our electronic pet, we let the micro:bit draw a sad face and let the micro:bit play the sound ||music:sad||
after we have not touched our electronic pet for 5
seconds.
Counter.onCountEvent(5, CountActions.Continue, function () {
basic.showIcon(IconNames.Sad)
soundExpression.sad.playUntilDone()
})
To make our electronic pet happy when we touch the micro:bit logo, we let the micro:bit draw a ||basic:happy||
face and let it play the sound ||music:happy||
on logo ||input:touch||
.
Having now satisfied our electronic pet for a while, we let the micro:bit continue counting in ||Counter:one-second intervals||
, but start over again, that is, beginning with 1
.
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
basic.showIcon(IconNames.Happy)
soundExpression.happy.playUntilDone()
Counter.startFromMinAgain()
})
Since our electronic pet needs a lot of attention, we let the micro:bit draw a ||basic:skull||
face and let it play the sound ||music:mysterious||
after we have not touched our electronic pet for 15
seconds.
Furthermore, to make our electronic pet stop working when we leave it alone for 15
seconds, we terminate the execution of our program by selecting the option ||Counter:stop counting||
.
Counter.onCountEvent(15, CountActions.Stopp, function () {
basic.showIcon(IconNames.Skull)
soundExpression.mysterious.playUntilDone()
})
Now it is your turn to make our electronic pet happy when we ||input:shake||
for instance our micro:bit …, or for example let it ||music:yawn||
and fall asleep when we do not touch it for 10
seconds.
Micro:bit Educational Foundation. (2021). micro:bit pet.
Counter.onCountEvent(30, CountActions.Continue, function () {
basic.showIcon(IconNames.Asleep)
soundExpression.yawn.playUntilDone()
})
input.onGesture(Gesture.Shake, function () {
Counter.startFromMinAgain()
basic.showIcon(IconNames.Surprised)
soundExpression.giggle.playUntilDone()
})
input.onButtonPressed(Button.AB, function () {
Counter.startFromMinAgain()
basic.showLeds(`
. # . # .
. # . # .
. . . . .
# . . . #
. # # # .
`)
soundExpression.hello.playUntilDone()
})