Bon, you got a simple game going, but there is room for improvement. I don’t know about you, but it is a little too difficult for me, let’s add some sound.

Let’s start by adding the a parameter to the two already defined.

from pybricks.parameters import Button, Color, SoundFile

We can then create another List that has all our colors in the same order as we defined them in the colorList.

soundList = [SoundFile.RED, SoundFile.YELLOW, SoundFile.BLUE, SoundFile.GREEN, SoundFile.BLACK]

Cavet: We don’t have a pre-recorded ORANGE so we’re going to use BLUE. No matter, everybody knows the new ORANGE is BLUE.

Now lets update the part of the program that shows the random colors selected so that it reads them out two.

randomSet() 
print(“randomList”,randomList)
blink()
for color in randomList:
brick.light(colorList[color])
brick.sound.file(soundList[color])
wait(1000)
blink()

Can you spot the difference. And then update all the if statements so that they tell as well as show you the color when you press buttons. Here is snippet showing the first, you need to update all four.

if Button.LEFT in brick.buttons(): 
colorNo = colorList[0]
brick.light(colorList[colorNo])
brick.sound.file(soundList[0])
answerList.append(0)
wait(500)

Finally let’s get it to tell us if we got it right or wrong as well as showing us some blinking lights.

if answerList == randomList: 
brick.sound.file(SoundFile.BRAVO)
blinkGreen()
else:
brick.sound.file(SoundFile.SORRY)
blinkRed()

The final program looks like this… if you’re one isn’t working, check thru, maybe you missed something, maybe I missed something….

#!/usr/bin/env pybricks-micropython 
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color, SoundFile
from pybricks.tools
import wait
import time
import random
randomList = []
answerList = []
colorList = [Color.RED, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLACK]
soundList = [SoundFile.LEFT, SoundFile.RIGHT, SoundFile.UP, SoundFile.DOWN, SoundFile.BLACK]
def blink():
brick.light(Color.BLACK)
wait(500)
def blinkGreen():
for loop in range(4):
brick.light(Color.GREEN)
wait(500)
blink()
def blinkRed():
for loop in range(4):
brick.light(Color.RED)
wait(500)
blink()
def randomSet():
global randomList
# RED = LEFT button
# YELLOW = RIGHT button
# ORANGE = DOWN button
# GREEN = UP button
# BLACK = starting mode
for loop in range(4):
randomList.append(randomColor())
wait(1000)
def randomColor():
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = int(random.randint(0,100) / 25)
return(colorNo)
randomSet()
print(“randomList”,randomList)
blink()
for color in randomList:
brick.light(colorList[color])
brick.sound.file(soundList[color])
wait(1000)
blink()
while len(answerList) != len(randomList):
if Button.LEFT in brick.buttons():
brick.light(colorList[0])
brick.sound.file(soundList[0])
answerList.append(0)
wait(500)
if Button.RIGHT in brick.buttons():
brick.light(colorList[1])
brick.sound.file(soundList[1])
answerList.append(1)
wait(500)
if Button.UP in brick.buttons():
brick.light(colorList[2])
brick.sound.file(soundList[2])
answerList.append(2)
wait(500)
if Button.DOWN in brick.buttons():
brick.light(colorList[3])
brick.sound.file(soundList[3])
answerList.append(3)
wait(500)
if answerList == randomList:
brick.sound.file(SoundFile.BRAVO)
blinkGreen()
else:
brick.sound.file(SoundFile.SORRY)
blinkRed()

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store