/* jquery */ /* jquery accordion style*/ /* jquery init */

15 October 2017

micro:bit MicroPython Animation Code

MicroPython for the micro:bit has a useful collection of images. And you can create your own custom LED images too. So let's write some code to animate a collection of these images.

We'll use Python's built-in list data type to hold our image collection. After adding the images with the append method we can create a for loop to iterate through the list.

Here's the code:

from microbit import *

# create an empty list
images = []

# add images to list
images.append(Image.TRIANGLE)
images.append(Image.DIAMOND)
images.append(Image.SQUARE)

# display each image in the list
for img in images:
   display.show(img)
   sleep(1000)

Note there is a one second pause between the display of each image so we can clearly see each one.

If you'd like to add code that builds a new image collection first use the clear method to remove the old images, like this:

images.clear()

Next time we'll see how to create an Animation Class that incorporates both one-time play and looping functionality.

Return to the micro:bit MicroPython Coding Tutorials page.

No comments: