This chapter mainly focuses on how to show images on a micro:bit display. MakeCode provides a set of built-in images and image editing blocks to create your own images, limited up to two frames. You can play with images by scrolling and offsetting them in different ways.
5-1. Displaying Built-in Images
Problem
You want to display a built-in image on the micro:bit LED matrix.
Solution
In the Toolbox, click on Advanced to expand the category list, and then click on the Images category.
Click and drag the show image block over and place it inside of the on start block.
In the Toolbox, click on the Images category, and then click on the icon image block.
- Click on the myImage variable block and from the menu, choose Delete the “myImage” variable (Figure 5-2 ).
- Click on the icon image and from the drop-down menu, choose the happy icon. Keep the offset as 0 (Figure 5-3 ).
- Once finished, your code should look something like this (Figure 5-4 ).
How It Works
MakeCode comes with 40 built-in images to show on the micro:bit display. The full list of images can be found in Chapter 2 , Recipe 2-1. MakeCode uses the terms “icon” and “image” interchangeably. In the show image block, the offset parameter determines the start position (or end position) of the image to be displayed on the LED matrix.
5-2. Image Offsetting
Problem
You want to shift an image horizontally across the display with offset.
Solution
In the Toolbox, click on Advanced to expand the category list, and then click on the Images category.
Click and drag the show image block over, and place it inside of the on start block.
In the Toolbox, click on the Images category, and then click on the icon image block.
- Click and drag the icon image block over, and place it inside of the show image block (Figure 5-5 ).
Click on the myImage variable block and from the menu, choose Delete the “myImage” variable.
- Click on the icon image and from the drop-down menu, choose the happy icon. Change the offset to 2 (Figure 5-6 ).
- You will get an output as shown in Figure 5-7 .
How It Works
0 - no offset
Any positive number - offsets from left
Any negative number - offsets from right
The LED screen fits in a single frame. A frame is a part of the image. It is a square with five LEDs on a side. An image can span multiple frames. If you use the value 5 or -5 for the offset, you can completely hide the image inside the micro:bit display.
5-3. Scrolling Images
Problem
You want to scroll an image on the micro:bit display with different speeds.
Solution
In the Toolbox, click on Advanced to expand the category list, and then click on the Images category.
Click and drag the scroll image block over, and place it inside of the on start block.
In the Toolbox, click on the Images category, and then click on the icon image block.
- Click and drag the icon image block over, and place it inside of the scroll image block (Figure 5-8 ).
Click on the myImage variable block and from the menu, choose Delete the “myImage” variable .
Type 2000 for interval (ms).
- Once finished, your code should look something like this (Figure 5-9 ).
How It Works
The scroll image block allows you to scroll an image on the micro:bit display from right to left or left to right. The offset parameter specifies the number of LEDs from the left or right of the image that the micro:bit should start and continue with the animation. The offset value 0 and 1 does the same effect. The offset 0 and any positive number makes the image scroll from right to left. Any negative number makes the image scroll from left to right. The speed of the scrolling effect can be changed by the interval parameter. It accepts the time in milliseconds.
If you want to repeat the scrolling effect over and over again, place the scroll image block inside the forever block.
5-4. Creating Your Own Images
Problem
You want to create an image to fit with the micro:bit display.
Solution
In the Toolbox, click on Advanced to expand the category list, and then click on the Images category.
Click and drag the show image block over, and place it inside of the on start block.
In the Toolbox, click on the Images category, and then click on the create image block.
- Click and drag the create image block over, and place it inside of the show image block (Figure 5-10 ).
- Click on the myImage variable block and from the menu, choose Delete the “myImage” variable (Figure 5-11 ).
- In the create image block , click on the LEDs to create the image that you want (e.g., robot) as shown in Figure 5-12 .
How It Works
The create image block represents the micro:bit’s physical LED screen. The 5 X 5 image block is known as a single frame image.
5-5. Creating a Double-Sized Image
Problem
You want to create a large image with two frames.
Solution
In the Toolbox, click on Advanced to expand the category list, and then click on the Images category.
Click and drag the show image block over, and place it inside of the on start block.
In the Toolbox, click on the Images category, and then click on the create big image block.
- Click and drag the create big image block over, and place it inside of the show image block (Figure 5-13 ).
- Click on the myImage variable block and from the drop-down list, choose Delete the “myImage” variable (Figure 5-14 ).
- In the create big image block , draw two images (giraffes) by clicking on the squares (Figure 5-15 ).
- When you run the code on micro:bit, you can only see the Giraffe 1 (left) in Frame 1 on the micro:bit display (Figure 5-16 ).
How It Works
MakeCode allows you to create images with two frames. Each frame consists of 5 rows and five columns of LEDs. When you run the code on the micro:bit, the micro:bit display will show the first frame of the image. If you want to see the second frame, you should use offset or scroll methods.
5-6. Displaying Arrows
Problem
You want to display an arrow pointing to the south west direction.
Solution
In the Toolbox, click Advanced followed by Images. Then click and drag the show image block over and place it inside of the on start block.
- In the Toolbox, click Images again. Then click and drag the arrow image block over, and place it inside of the show image block (Figure 5-19 ).
Click on the myImage variable and from the drop-down list, choose Delete the “myImage” variable.
- In the arrow image block, click on the drop-down list and choose the South West option (Figure 5-20 ).
- Once finished, your code should look something like this (Figure 5-21 ).
- Figure 5-22 shows the output.
How It Works
North
North East
East
South East
South
South West
West
North West
5-7. Using Variable to Hold an Image
Problem
You want to use a variable to hold an image.
Solution
- In the Toolbox, click on the Variables category, and then click on Make a Variable… (Figure 5-23 ).
- In the New variable name modal box (window), type in the variable name (e.g., heart). Then click on the Ok button (Figure 5-24 ).
- Now your Variables Toolbox should look something like this (Figure 5-25 ). It contains the variable and two blocks to set and change the variable.
Now click and drag the set heart to block over and place it inside of the on start block.
- In the Toolbox, click Advanced followed by Images. Then click and drag the icon image block over, and place it inside of the set heart to block (Figure 5-26 ).
In the Toolbox, click Images. Then click and drag the show image block over and place it inside of the forever block.
- In the show image block, click on the myImage and from the drop-down list, choose the variable, heart (Figure 5-27 ).
- Now your code should look something like this (Figure 5-28 ).
How It Works
Variables can hold built-in images and custom images. Once assigned an image to a variable, you can use the variable name to display the image at any point in your code.