Deux leds sont allumées et se déplacent aléatoirement dans la matrice de leds 5×5. Lorsqu'elles sont sur la même case, le contour de la matrice s'allume.
from microbit import display, sleep, Image
from random import randint
display.clear()
x1, y1 = 0, 0
x2, y2 = 4, 4
def bouge(x, y):
dx = randint(-1,1)
x = x + dx
if x > 4:
x = 4
elif x < 0:
x = 0
dy = randint(-1,1)
y = y + dy
if y > 4:
y = 4
elif y < 0:
y = 0
return x, y
while True:
display.set_pixel(x1, y1, 0)
x1, y1 = bouge(x1, y1)
display.set_pixel(x1, y1, 9)
display.set_pixel(x2, y2, 0)
x2, y2 = bouge(x2, y2)
display.set_pixel(x2, y2, 5)
if x1 == x2 and y1 == y2:
display.show(Image.SQUARE)
sleep(100)
display.clear()
else:
sleep(100)