← micro:bit Foundations

Pocket pet

Build a pet that lives on your micro:bit, using everything in this course. It gets hungry as time passes, you feed it, and its face shows how it feels.

Tamagotchis, Neopets and the pets in phone games all run the same loop: time makes a number go up, you bring it down, and a picture shows how things are going.

Build a pocket pet. It gets hungry as time passes, A feeds it, its face shows how it feels, and a shake makes it do a trick. You already know every block it needs. You will build it in three milestones, with less help each time.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
lesson.ts
let hunger = 0
basic.forever(function () {
    basic.pause(3000)
    hunger += 1
    basic.showNumber(hunger)
})

This is the heart of the pet: time making hunger go up.

Nobody presses anything. About what number shows 10 seconds after the pet starts?

RunRun it and compare the result with your prediction.

Run

Watch for about 10 seconds without pressing anything.

The program as blocks
Show as code
lesson.ts
let hunger = 0
basic.forever(function () {
    basic.pause(3000)
    hunger += 1
    basic.showNumber(hunger)
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

This is the finished pet. Read it before you build your own.

The program as blocks
Show as code
lesson.ts
let tricks = ["SPIN", "JUMP", "WAVE"]
let hunger = 0
input.onButtonPressed(Button.A, function () {
    if (hunger > 0) {
        hunger += -1
    }
    basic.showIcon(IconNames.Heart)
})
input.onGesture(Gesture.Shake, function () {
    basic.showString(tricks[randint(0, tricks.length - 1)])
})
basic.forever(function () {
    basic.pause(3000)
    hunger += 1
    if (hunger < 3) {
        basic.showIcon(IconNames.Happy)
    } else if (hunger < 6) {
        basic.showIcon(IconNames.Asleep)
    } else {
        basic.showIcon(IconNames.Sad)
    }
})
A feeds the pet: if hunger > 0, change hunger by -1. Why is the if there?

The face is chosen by if hunger < 3 … else if hunger < 6 … else. What does each hunger show?

hungerface
1
4
8

A shake picks a trick with get value at (pick random 0 to length of tricks − 1). You add a fourth trick. What else must change?

ModifyChange the code on purpose and check the result.

Milestone 1: hungry over time

The variable hunger and the feeding button A are made for you.

  1. Inside forever, pause for 3 seconds.
  2. Then change hunger by 1 and show it.
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

Inside forever: pause, then change hunger by 1, then show number hunger.

Another hint

3000 ms is 3 seconds.

Every few seconds, hunger goes up by 1 by itself, and shows.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Milestone 2: a face for each feeling

Swap the number for a face. Choose your own pictures:

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

Replace show number with one if block. Click + to add else if and else.

Another hint

Ask hunger < 3 first, then hunger < 6. Anything else is very hungry.

The face shows how the pet feels: happy, tired or sad, from hunger, in one if … else if … else.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Milestone 3: tricks

The list tricks is made for you. Make a shake show one trick, picked at random. Add a trick of your own to the list.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

A shake makes the pet do a trick, picked at random from a list of tricks.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Make it yours

Give your pet a life of its own. Some ideas:

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers

Students build a pocket pet in three milestones, using forever, events, a variable, ranges with else if, and a random pick from a list. About 55 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

This challenge uses every lesson in the course and adds nothing new. The pet is a variable, hunger, that forever makes go up (pause, then change by 1); A brings it down (with an if so it never goes below 0); if … else if … else turns the number into a face; and a shake picks a trick from a list at random. The milestones fade the help: Milestone 1 gives nearly all the code, Milestone 2 an outline, Milestone 3 only a goal. Build it yourself first; it takes about ten minutes.

Running the lesson

  1. Predict and Run: the pause sets the pet's speed of life. Ask what would make it get hungry faster. Count it out together: 3, 6, 9 seconds.
  2. Investigate: three looks back: an if that guards a number, ranges, and a list with its length.
  3. Milestone 1: if hunger never changes, the change block is outside forever.
  4. Milestone 2: the order of the questions matters, as in The first true question wins.
  5. Milestone 3: the trick uses pick random 0 to length − 1, from Pick from a list.
  6. Make it yours: the sleeping pet needs an if around the change in forever.

The hard part (on purpose)

Putting it all in the right blocks. Ask which block each job belongs in: time, button, face or shake.

When students get stuck

Stretch and support

Stretch: Add a second need, bored, with its own number, its own button and its own face rule. Which need wins when both are high?

Support: Pair students for Milestone 3: one reads the goal aloud while the other builds.

Where it leads

Course complete. Next: micro:bit: talk and play, where functions name jobs and radio connects micro:bits. With real micro:bits: keep the pet in a pocket for a lesson and see if it survives.

What each step is for

Now you can
You reached the endBack to micro:bit Foundations