Member-only story
MicroPython Tutorial V
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)