Member-only story

MicroPython Tutorial III

Mark Lucking
2 min readAug 1, 2019

Ok, lets try and build a simple game with the code we learnt over the last two lessons in the next two. Here is our code template.

#!/usr/bin/env pybricks-micropython 
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color
from pybricks.tools import wait
import time
import random
colorList = [Color.RED, Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLACK]def randomColor():
seedling = int(round(time.time()))
random.seed(seedling)
colorNo = random.randint(0,4)
return(colorNo)
while True:
if Button.DOWN in brick.buttons():
colorNo = randomColor()
brick.light(colorList[colorNo])
wait(500)
if Button.UP in brick.buttons():
colorNo = randomColor()
brick.light(colorList[colorNo])
wait(500)

It is basically the same code you did in tutorial II, with a few minor changes that will come into play as we move forward.

We’re going to build a “simple Simon says” type game. We start by adding two subroutines. Code that you can place above the randomColor routine.

def blink(): 
brick.light(Color.BLACK)
wait(500)
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)

--

--

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