Skip to content

Atividade

micro:bit pet

Intermediário | MakeCode, Python | Acelerômetro, Logotipo Touch, Microfone, Tela de LED | Iteração, Seleção, Variáveis

Passo 1: Projeto

O que é?

Code your own electronic pet and customise it to make it your own. The new micro:bit's built-in speaker makes it even more fun with the new expressive sounds.

Introdução

Coding guide

O que você aprenderá

  • How to use variables and loops to make a simple timer.
  • How to use selection to make different events how to happen at different times.
  • A trick to make it appear as if a micro:bit program has stopped running!

Como funciona

  • Electronic pets were invented in Japan in the 1990s and soon became a popular toy all over the world.
  • They’re small gadgets that you can keep on your keyring. A creature is shown on a simple display. You have to look after it, interacting with it by pressing buttons and playing simple games. If you ignore it and don’t feed or clean it, it will get sad, misbehave or become unwell.
  • The code uses a variable called timer to keep track of how long your pet has been ignored. A forever loop adds 1 to the timer every 1 second (1000 milliseconds).
  • If timer reaches 20, your pet shows a sad face on the LED display and makes a sad sound on the built-in speaker outputs.
  • If it reaches 30, your pet falls asleep.
  • If it gets to 40 your pet plays a mysterious sound and dies. The code uses the 'set built-in speaker off' block to stop any other sounds coming out of the speaker. A 'while true' loop makes sure only the skull icon is shown on the LED display.
  • Your pet will stay alive and happy if you interact with it, however! Stroke the logo to make it happy, or shake it to make it giggle. This resets the timer back to 0.
  • If your pet dies you can bring it back to life by pressing the reset button on the back of your micro:bit.

O que você precisa

  • new micro:bit with sound (or MakeCode simulator)
  • Editor MakeCode ou Python
  • bateria (opcional)

Passo 2: É hora de programar

Passo 3: Vamos melhorar

  • Experiment with different times for each stage of its life.
  • Create your own facial expressions using the micro:bit's LED display.
  • Add more variables to track how hungry or dirty your pet is, and add new interactions to feed or clean your pet, for example by pressing buttons or making a loud sound picked up by the new micro:bit's built-in microphone.