Science Oxford Logo

CCC - Cyber-Pet Project

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.


Testing our cyber pet

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:


Modifying the code

  1. Change the HEART image on line 12 to a different image - what should your pet do if it is happy?
  2. Change how your pet tells you it is hungry.

Challenge: can you give your happy pet a heartbeat?


What else does your pet need?

To add extra things that your pet needs, you will need to:

  1. Add to the needs dictionary
  2. Set how the pet gets your attention
  3. Set how you respond to this new need

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.

1: add to the needs dictionary
needs =  {
         'food' = False,
         'play' = False,
         }

2: set how your pet gets your attention
    if needs['play']:
        display.show('P')

3: set how your respond to the need
    if accelerometer.was_gesture('shake'):
        needs['play'] = False

Full code example:
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 does your pet need?

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!

Inputs you can respond with:

Outputs your pet could use:

Some outputs take time to run, so your pet might not respond until it has finished!


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.