2. Built-in images advanced
2.1. Advanced use of Built-in Image lists
from microbit import *
clock_list = Image.ALL_CLOCKS
print(type(clock_list))
list(Image.ALL_CLOCKS)
can convert Image.ALL_CLOCKS to the list:list(Image.ALL_ARROWS)
can convert Image.ALL_ARROWS to the list:2.1.1. Reverse direction of list using list slicing
arrow_list
, can be reversed using the slicing technique: arrow_list[::-1]
.arrow_list_anticlockwise = arrow_list[::-1]
reverses the list and places it in a the variable arrow_list_anticlockwise
.from microbit import *
arrow_list = list(Image.ALL_ARROWS)
arrow_list_anticlockwise = arrow_list[::-1]
while True:
display.show(arrow_list_anticlockwise, delay=200)
2.1.2. Reverse direction of a list using the reverse method
- a_list.reverse()
- The list, a_list, is reversed. No parameters are involved.
clock_list.reverse()
.from microbit import *
clock_list = list(Image.ALL_CLOCKS)
clock_list.reverse()
while True:
display.show(clock_list, delay=200)
2.1.3. Reverse direction of list using the reversed function
- reversed(sequence)
- sequence is the list to reverse.
reversed(clock_list)
.from microbit import *
clock_list = list(Image.ALL_CLOCKS)
clock_list_anticlockwise = reversed(clock_list)
while True:
display.show(clock_list_anticlockwise, delay=200)
list(reversed(clock_list))
and placing the result in the variable clock_list_anticlockwise.from microbit import *
clock_list = list(Image.ALL_CLOCKS)
clock_list_anticlockwise = list(reversed(clock_list))
while True:
display.show(clock_list_anticlockwise, delay=200)
Tasks
Write code that uses list slicing to display all the arrow images clockwise then anticlockwise.
Write code that uses the reverse method to display all the clock images clockwise then anticlockwise.
Write code that uses the reversed function to display all the clock images clockwise then anticlockwise.
Write code that uses list slicing to display all the arrow images clockwise then anticlockwise.
from microbit import *
arrow_list = list(Image.ALL_ARROWS)
arrow_list_anticlockwise = arrow_list[::-1]
while True:
display.show(arrow_list, delay=200)
display.show(arrow_list_anticlockwise, delay=200)
Write code that uses the reverse method to display all the clock images clockwise then anticlockwise.
from microbit import *
clock_list = list(Image.ALL_CLOCKS)
clock_list_anticlockwise = list(Image.ALL_CLOCKS)
clock_list_anticlockwise.reverse()
while True:
display.show(clock_list, delay=200)
display.show(clock_list_anticlockwise, delay=200)
Write code that uses the reversed function to display all the clock images clockwise then anticlockwise.
from microbit import *
clock_list = list(Image.ALL_CLOCKS)
clock_list_anticlockwise = list(reversed(clock_list))
while True:
display.show(clock_list, delay=200)
display.show(clock_list_anticlockwise, delay=200)
2.1.4. Randomize list
- sorted(iterable, key=None, reverse=False)
- iterable Required. The sequence to sort, list, dictionary, tuple etc.key Optional. A Function to execute to decide the order. Default is Nonereverse Optional. A Boolean. False will sort ascending, True will sort descending. Default is False
- random.random()
Returns a random floating number between 0 and 1.
def random_key(element):
return random.random()
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [1, 2, 3, 4]
while True:
new_list = sorted(orig_list, key=random_key)
for element in new_list:
display.scroll(element, delay=60)
sleep(1000)
Tasks
Modify the orig_list to be the list of letters “a”, “e”, “t”. Bonus: What do the 6 possible words mean?
A string can be turned to a list using the list function. Modify the orig_list to be list(“ate”).
Modify the orig_list to be the list of characters from list(“ab12”)
Modify the orig_list to be the list of letters “a”, “e”, “t”.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = ["a", "e", "t"]
while True:
new_list = sorted(orig_list, key=random_key)
for element in new_list:
display.scroll(element, delay=60)
sleep(1000)
A string can be turned to a list using the list function. Modify the orig_list to be list(“ate”).
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = list("ate")
while True:
new_list = sorted(orig_list, key=random_key)
for element in new_list:
display.scroll(element, delay=60)
sleep(1000)
Modify the orig_list to be the list of characters from list(“ab12”)
from microbit import *
import random
def random_key(element):
return random.random()
list("ab12")
while True:
new_list = sorted(orig_list, key=random_key)
for element in new_list:
display.scroll(element, delay=60)
sleep(1000)
2.1.5. Randomize image list
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = list(Image.ALL_ARROWS)
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
sleep(1000)
Tasks
Create a list of the 4 main compass direction arrow images, then randomly sort them and display them.
Create a list of the 4 secondary compass direction arrow images, then randomly sort them and display them.
Create a list of the clock images for 12, 3, 6 and 9 o’clock then randomly sort them and display them.
Create a list of the 4 main compass direction arrow images, then randomly sort them and display them, then display them in reverse order using the reverse method.
Create a list of the 4 secondary compass direction arrow images, then randomly sort them and display them, then display them in reverse order using the reversed function.
Create a list of the 4 main compass direction arrow images, then randomly sort them and display them.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [Image.ARROW_N, Image.ARROW_E, Image.ARROW_S, Image.ARROW_W]
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
sleep(1000)
Create a list of the 4 secondary compass direction arrow images, then randomly sort them and display them.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [Image.ARROW_NE, Image.ARROW_SE, Image.ARROW_SW, Image.ARROW_NW]
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
sleep(1000)
Create a list of the clock images for 12, 3, 6 and 9 o’clock then randomly sort them and display them.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [Image.CLOCK12, Image.CLOCK9, Image.CLOCK6, Image.CLOCK3]
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
sleep(1000)
Create a list of the 4 main compass direction arrow images, then randomly sort them and display them then display them in reverse order using the reverse method.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [Image.ARROW_N, Image.ARROW_E, Image.ARROW_S, Image.ARROW_W]
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
display.clear()
sleep(500)
new_list.reverse()
display.show(new_list, delay=500)
display.clear()
sleep(1000)
Create a list of the 4 secondary compass direction arrow images, then randomly sort them and display them then display them in reverse order using the reversed function.
from microbit import *
import random
def random_key(element):
return random.random()
orig_list = [Image.ARROW_NE, Image.ARROW_SE, Image.ARROW_SW, Image.ARROW_NW]
while True:
new_list = sorted(orig_list, key=random_key)
display.show(new_list, delay=500)
display.clear()
sleep(500)
rev_list = reversed(new_list)
display.show(rev_list, delay=500)
display.clear()
sleep(1000)
2.1.6. Interrupting an image list
wait=False
. This causes display.show to run in the background. The rest of the code in the while block runs without waiting for the display.show to complete.from microbit import *
while True:
display.show(Image.ALL_CLOCKS, delay=100, wait=False)
for i in range(12):
if button_a.is_pressed():
display.show("A")
elif button_b.is_pressed():
display.show("B")
sleep(100)
from microbit import *
img_list = list(Image.ALL_CLOCKS)
while True:
for img in img_list:
display.show(img)
sleep(200)
if button_a.is_pressed():
display.show("A")
elif button_b.is_pressed():
display.show("B")
sleep(400)
Tasks
Modify the code to make better use of A or B-button pressing.
2.2. Enumerate to show the clock time
- enumerate(iterable, start)
- iterable An iterable object such as a list or tuplestart A number defining the start number of the enumerate object. Default 0.
See: https://realpython.com/python-enumerate/
from microbit import *
all_img = list(enumerate(Image.ALL_CLOCKS, start=0))
while True:
for count, img in all_img:
display.show(img)
if button_a.is_pressed():
sleep(1000)
display.scroll(count, delay = 60)
sleep(1000)
elif button_b.is_pressed():
sleep(30)
else:
sleep(1000)
[
(0, Image("00900:00900:00900:00000:00000:")),
(1, Image("00090:00090:00900:00000:00000:")),
(2, Image("00000:00099:00900:00000:00000:")),
(3, Image("00000:00000:00999:00000:00000:")),
(4, Image("00000:00000:00900:00099:00000:")),
(5, Image("00000:00000:00900:00090:00090:")),
(6, Image("00000:00000:00900:00900:00900:")),
(7, Image("00000:00000:00900:09000:09000:")),
(8, Image("00000:00000:00900:99000:00000:")),
(9, Image("00000:00000:99900:00000:00000:")),
(10, Image("00000:99000:00900:00000:00000:")),
(11, Image("09000:09000:00900:00000:00000:")),
]
Tasks
Modify the code to display 12 instead of 0 when the clock is in the 12 O’clock position.