8. Built-in images

The microbit library contains over 60 built-in images that have specific names.
e.g Image.HEART
The Image object must have a capital I.
The built-in image name must be in capitals. e.g HEART not heart.
No quotation marks are used. e.g Image.HEART not ‘Image.HEART’.
The same syntax that was used to show text can be used for images.
In Mu editor, when typing Image., as soon as the stop is typed, a drop list of images will be displayed to allow selection of an image.

Tip

Variables and functions should only use lower case letetrs.
Classes should use CapWords like TeamPlayers.
Constants shoud be in ALLCAPS, like PIN.
The use of Image with a capital is typical of a class, and HEART is a constant within the Image class.

8.1. Display.show a built-in Image

display.show(image)
Display an image, where image is an image such as the built-in image Image.Heart.
The code below displays a built-in heart image.
from microbit import *

display.show(Image.HEART)

Tasks

  1. Write code to show an ARROW_N.

  2. Write code to show a GIRAFFE.

  3. Write code to show a SMILE.

Write code to show an ARROW_N.

from microbit import *

while True:
    display.show(Image.ARROW_N)

Write code to show a GIRAFFE.

from microbit import *

while True:
    display.show(Image.GIRAFFE)

Write code to show a SMILE.

from microbit import *

while True:
    display.show(Image.SMILE)

8.2. Flashing an Image

When a sleep is used along with clearing the display, an image can be made to flash.
The code below flashes a the built-in heart image.
from microbit import *

while True:
    display.show(Image.HEART)
    sleep(300)
    display.clear()
    sleep(300)

Tasks

  1. Modify the code to have the heart appear for 1/3 of the time.

  2. Modify the code to have the heart appear for 2/3 of the time.

Modify the code to have the heart appear for 1/3 of the time.

from microbit import *

while True:
    display.show(Image.HEART)
    sleep(300)
    display.clear()
    sleep(600)

Modify the code to have the heart appear for 2/3 of the time.

from microbit import *

while True:
    display.show(Image.HEART)
    sleep(600)
    display.clear()
    sleep(300)

8.3. Display.show a list of images

A list of images can be displayed in sequence.
display.show(imagelist, delay=400)
Display images from a list in sequence.
Each image in a list of images is shown with delay milliseconds between them.
The delay defaults to 400ms if it is omitted.
The code below shows a sequence of 5 face images with half a second between them.
from microbit import *

while True:
    display.show([Image.HAPPY, Image.SMILE, Image.SAD, Image.CONFUSED, Image.ANGRY], delay=500)

Tasks

  1. Write code to show a list of 3 different animals with an 0.5 sec delay between them.

  2. Write code to show a list of 4 different arrows with an 400ms delay between them.

  3. Write code to show a list of 3 different shapes with an 0.3 sec delay between them.

Write code to show a list of 3 different animals with an 0.5 sec delay between them.

from microbit import *

while True:
    display.show([Image.RABBIT, Image.COW, Image.GIRAFFE], delay=500)

Write code to show a list of 4 different arrows with an 400ms delay between them.

from microbit import *

while True:
    display.show([Image.ARROW_N, Image.ARROW_E, Image.ARROW_S, Image.ARROW_W], delay=400)

Write code to show a list of 3 different shapes with an 0.3 sec delay between them.

from microbit import *

while True:
    display.show([Image.TRIANGLE, Image.DIAMOND, Image.SQUARE], delay=300)

8.4. Image lists

When the list of images is more than a few, it is usual to put the list in a variable.
The list of 5 images is placed in the variable face_list.
The variable is then used to display the images: display.show(face_list, delay=500).
from microbit import *

face_list = [Image.HAPPY, Image.SMILE, Image.SAD, Image.CONFUSED, Image.ANGRY]
while True:
    display.show(face_list, delay=500)
The code below displays a list of shapes, so the variable name chosen is shape_list.
Whitespace (tabs, spaces, line endings) are ignored within a list, so a long list can be set out like that below, with all the Images lined up for easy reading.
Trailing commas are preferred in such lists, making it easy to extend the list with new lines.
In Mu Editor, the Tidy button will format a long list like that below.
from microbit import *

shape_list = [
    Image.TRIANGLE,
    Image.TRIANGLE_LEFT,
    Image.DIAMOND,
    Image.DIAMOND_SMALL,
    Image.SQUARE,
    Image.SQUARE_SMALL,
]
while True:
    display.show(shape_list, delay=100)

Tasks

  1. Write code to use a variable, animal_list, to show a list of 3 different animals with an 0.5 sec delay between them.

  2. Write code to use a variable, arrow_list, to show a list of 4 different arrows with an 0.4 sec delay between them.

  3. Write code to use a variable to show a list of 3 different music images with an 0.3 sec delay between them.

Write code to use a variable, animal_list, to show a list of 3 different animals with an 0.5 sec delay between them.

from microbit import *

animal_list = [Image.RABBIT, Image.COW, Image.GIRAFFE]
while True:
    display.show(animal_list, delay=500)

Write code to use a variable, arrow_list, to show a list of 4 different arrows with an 0.4 sec delay between them.

from microbit import *

arrow_list = [Image.ARROW_N, Image.ARROW_E, Image.ARROW_S, Image.ARROW_W]
while True:
    display.show(arrow_list, delay=400)

Write code to use a variable to show a list of 3 different music images with an 0.3 sec delay between them.

from microbit import *

music_list = [Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER, Image.MUSIC_QUAVERS]
while True:
    display.show(music_list, delay=300)

8.5. Built-in Image lists

There are 2 built-in collections of images.
They are: Image.ALL_CLOCKS and Image.ALL_ARROWS.
Both collections have images that follow a clockwise sequence.
The code below displays the built-in collection of clock images.
from microbit import *

while True:
    display.show(Image.ALL_CLOCKS, delay=100)

Tasks

  1. Write code to display the images in the built-in image collection: Image.ALL_ARROWS, with a delay of 200ms.

Write code to display the images in the built-in image collection: Image.ALL_ARROWS, with a delay of 200ms.

from microbit import *

while True:
    display.show(Image.ALL_ARROWS, delay=200)

8.6. Image sentences

The code below makes a sentence using words and images.
Delays of sleep(300) are used to prevent the sequence from being too fast to see.
from microbit import *

while True:
    display.scroll('I')
    sleep(300)
    display.show(Image.HEART)
    sleep(300)
    display.show(Image.GIRAFFE)
    sleep(300)

Tasks

  1. Write an image sentence combining words and images.

Write an image sentence combining words and images.

# tortoises live long
from microbit import *

while True:
    display.show(Image.TORTOISE)
    sleep(300)
    display.show(Image.HOUSE)
    sleep(300)
    display.scroll("long")
    sleep(300)