from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math
import random # Needed to generate random numbers
hub = MSHub()
images = ['ANGRY', 'ARROW_E', 'ARROW_N', 'ARROW_NE', 'ARROW_NW',
'ARROW_S', 'ARROW_SE', 'ARROW_SW', 'ARROW_W', 'ASLEEP',
'BUTTERFLY', 'CHESSBOARD', 'CLOCK1', 'CLOCK10', 'CLOCK11',
'CLOCK12', 'CLOCK2', 'CLOCK3', 'CLOCK4', 'CLOCK5',
'CLOCK6', 'CLOCK7', 'CLOCK8', 'CLOCK9', 'CONFUSED',
'COW', 'DIAMOND', 'DIAMOND_SMALL', 'DUCK', 'FABULOUS',
'GHOST', 'GIRAFFE', 'GO_RIGHT', 'GO_LEFT', 'GO_UP',
'GO_DOWN', 'HAPPY', 'HEART', 'HEART_SMALL', 'HOUSE',
'MEH', 'MUSIC_CROTCHET', 'MUSIC_QUAVER', 'MUSIC_QUAVERS', 'NO',
'PACMAN', 'PITCHFORK', 'RABBIT', 'ROLLERSKATE', 'SAD',
'SILLY', 'SKULL', 'SMILE', 'SNAKE', 'SQUARE',
'SQUARE_SMALL', 'STICKFIGURE', 'SURPRISED', 'SWORD', 'TARGET',
'TORTOISE', 'TRIANGLE', 'TRIANGLE_LEFT', 'TSHIRT', 'UMBRELLA',
'XMAS', 'YES']
n_images = len(images)
print("Total number of images: " + str(n_images))
To choose the images to display, we will generate a random index
using random.randint
for ii in range(1, 4):
# Generate a random number
idx = random.randint(0, n_images-1)
# Display the random image
image = images[idx]
print("Displaying image " + str(ii) + " with index " + str(idx) + " ('" + image + "')...")
hub.light_matrix.show_image(image)
print("DONE!")
# Insert a pause so we can see the image
wait_for_seconds(2.5)