Member-only story

MicroPython Tutorial V

Mark Lucking
2 min readAug 1, 2019

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)

--

--

Mark Lucking
Mark Lucking

Written by Mark Lucking

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

No responses yet