Resources for the 'Cyber-Pet Project' Creative Computing Club.
This page recaps what we discussed as a group.
To skip to the advanced extensions, click here.
Run our cyber-pet code on your micro:bit - what does it do? Can you work out how to make your pet happy?
This code has lots of sections:
Challenge: can you give your happy pet a heartbeat?
To add extra things that your pet needs, you will need to:
In this example, we will add the need play; the pet will show a P when it wants it; we will shake the micro:bit to respond.
needs = {
'food' = False,
'play' = False,
}
if needs['play']:
display.show('P')
if accelerometer.was_gesture('shake'):
needs['play'] = False
from microbit import *
import pet
needs = {
'food': False,
'play': False,
}
while True:
pet.asks(5000, needs)
if pet.happy(needs):
display.show(Image.HEART)
if needs['food']:
display.show('F')
if button_a.was_pressed():
needs['food'] = False
if needs['play']:
display.show('P')
if accelerometer.was_gesture('shake'):
needs['play'] = False
Challenge: create your own images instead of using the built-in ones.
What else could your pet ask for? Perhaps they want a nap, or to be stroked. Use the structure in the previous step to add as many needs as you want!
button_b.was_pressed()accelerometer.was_gesture('up')accelerometer.was_gesture('down')accelerometer.was_gesture('left')accelerometer.was_gesture('right')accelerometer.was_gesture('face up')accelerometer.was_gesture('face down')pin_logo.is_touched()microphone.was_event(SoundEvent.LOUD)Some outputs take time to run, so your pet might not respond until it has finished!
audio.play(Sound.GIGGLE)display.scroll('hello')speech.say('hello') - (add import speech to the top of your code)Challenge: use variables to add timers to your code - e.g. if your pet needs food for more than 10 seconds, start making a sound to get your attention! See an example here.