Tuesday, 2 February 2016

Microbit - a Bop-it game in Python

I wanted to create a simple game for the Microbit and after bring brought an R2D2 Bop-it game I thought I would make one whereby you have to press the A and B buttons in time.

Lets start by importing the microbit and random libraries:
from microbit import *
import random
Create some constants, one for the SPEED which is the amount of time in between each bop for each level, and one for the LEVELUP's which is the amount of points at each level:
SPEED = {0: 1000, 1: 750, 2: 650, 3: 600, 4: 550, 5: 500}
LEVELUP = (5, 10, 15, 20, 25, 30)
Create 4 functions which when caused will show A, B, a tick and a cross:
def show_a():

def show_b():

def show_tick():
    display.set_pixel(0, 3, 9)
    display.set_pixel(1, 4, 9)
    display.set_pixel(2, 3, 9)
    display.set_pixel(3, 2, 9)
    display.set_pixel(4, 1, 9)
def show_cross():
Create a function which will wait for the button to be pressed, returning True if it is and False if it isn't:
def wait_for_button(rightbutton, wrongbutton):
    rightpressed = False
    wrongpressed = False
    started = running_time()
    now = running_time()
    while now - started < SPEED[level]:
        if rightbutton.is_pressed():
            rightpressed = True
        if wrongbutton.is_pressed():
            wrongpressed = True
        now = running_time()
    if rightpressed == True and wrongpressed == False:
        return True
        return False
Set 3 variables for the level, the score and one which will be set to True when the game is over:
level = 0
score = 0
gameover = False
Scroll "BopBit" on the screen to show its the start of the game:
Loop until the game is over:
while gameover == False:
Randomly pick an action (either A or B), show it on the screen and wait for a button to be pressed:
    success = False
    #randomly pick an A or B button
    action = random.randint(0, 1)
    #wait for the button to be pressed
    if action == 0:
        success = wait_for_button(button_a, button_b)
    elif action == 1:
        success = wait_for_button(button_b, button_a)
If the player pressed the right button in time show a tick and increase the score or show a cross:
    if success:
        score = score + 1
        #if the score is a levelup score increase the level 
        if score in LEVELUP:
            level = level + 1
        gameover = True
Wait for a small amount of time (half the current speed):
    sleep(int(SPEED[level] / 2))
After the game is over, sleep for 1 second, and display the players score on a loop:
while True:
    display.scroll("{} points".format(score))
You can view the complete code in my Microbit MicroPython examples repository on github.

How about taking it further by adding new actions such as shaking or a connecting up a speaker and adding a sound track,


  1. Hi there,
    I'm not a game guru but I'm a bit interested in your blog. How does this work? I mean, what should I do first? Is this as easy as writing essays? Well, when you say about writing essays, I've got a good helper though, Urgent Essay Writing. Never lets me down.

  2. Thanks for tutorial. This is very helpful.