How to Create a Reaction Game for the HaloHD for micro:bit using EduBlocks hero image

Introduction

Hello, I am Luke from Aquinas College. I have always enjoyed the challenge of writing software and learning about how technology can solve a variety of real-world problems. Kitronik allowed me to experience this by allowing me to create an extension for their HaloHD product for the micro:bit. This developed my understanding of different programming languages whilst learning what a job in the industry truly is like. My week at Kitronik bettered my skills in programming in multiple languages.

How I Coded an Extension for the HaloHD

I programmed an extension that allowed users to code onto the HaloHD, using a micro:bit, with the EduBlocks platform. This code contained three core files; definitions, generators, and toolbox. The definitions contain the name of the block and what information will be displayed and passed. The generators generate code into Python code which is used by EduBlocks. The toolbox links the definitions and the generators. When put together, these files made the extension for EduBlocks which I used to code my game. I coded five blocks which did the following; imported the 'neopixel' feature, initialise 'neopixel', set individual led colour, set all LEDs to a certain colour, and show LEDs.

Concept of the Game

I then coded a game on the platform in which a user battled an enemy led. The user and enemy would randomly generate at different LEDs and would be represented by a green and a red colour. They would move towards each other and the user would have to press the A button so that it destroyed the enemy when they were in the pixels next to each other. If the user failed too, they would lose. If the user was successful then the next round would start, at a faster pace. To win, the user must complete the game at the fastest pace.

Main Techniques

The main stages to creating the game are:

  • Importing libraries we will need.
  • Setting up our variables.
  • The main game loop.
  • Validation of pixel numbers.
  • Generating random numbers.
  • Array bounds.
  • Setting and showing pixels.
  • Checking if the game is won/lost.
  • Validating variables.

Creating an EduBlocks Project

To code the game we need to create a new project in EduBlocks. For the project we need to select BBC micro:bit mode and click the Start Coding button at the bottom right corner of the screen to being.

Once in the project editor, we want to add the Kitronik HaloHD extension. We can do this by clicking the plus (+) button in the bottom left corner of the screen and select ZIP Halo HD from the approved extension list.

Coding the Game

For the first stage, we need to import the libraries that the program will need to use. Firstly, the micro:bit import allows us to use features on the micro:bit. Secondly, the random import, which will allow us to generate a random integer. Lastly, the import KitronikHaloHD library which is a block from the HaloHD extension which will allow us to use the pixels on the HaloHD. We do this by dragging the following blocks onto the startcode.

Python Blockly Imports

Here, we create the variables we need for the program. Firstly, the halo variable which is a HaloHD object (collection of variables and methods), in this instance for our LEDs. Secondly, we create the delay variable which determines how long the game sleeps for each round. Next the value for the enemy and player pixels. Lastly two boolean values, one which loops the main game, and the other which is used to validate values.

Python Blockly Initialise Variables

Here we use a loop to keep the game in play whilst the player is winning. We use the winning variable here, which changes when the player loses. All the code for the game is in this loop.

Python Blockly Game Loop

Next, above the while winning loop use a function checkValid to use a series of conditions to validate the enemy and player variables. We use this to validate the player and enemy pixels. The if statement which will be called from inside the while not valid loop, determines when the variables are not valid and so return False. Here add if statements to return that the positions are not valid when they are next to each other.

Next they are not valid unless they are both on either even or odd pixels. This ensure the pixels meet each other at some point around the HaloHD circle. If they are then none of the conditions are true and the loop reaches to bottom to exited exit, returning True.

Python Blockly Validate Variables

Now inside the while winning loop create a while statement so that the program will continue to generate a random combination of numbers until they are valid by using the checkValid function. We use the 'import random' feature. To generate the random number we use the random.randint function. This isn't built into EduBlocks, so you will need to use 'your own code' blocks. This will be used to pick a random spawning location for the enemy and player. We do this twice to get a more effective result.

Python Blockly Random Values

Next, outside of the while not valid loop, the pixels both rotate one value along the circle.

Python Blockly Increment Decrement Variables

Next, we will use IF statements, which works by determining if the condition is true, and if it is it will perform some code. We use if statements for many things like to check; the spawning point of the pixels is fair, and that they don't spawn next to each other. Here use if statements to guarantee that the pixels keep moving even when they meet the end of the LEDs on the HaloHD.

Python Blockly Bounds Checking

Next the value for the colour of the enemy pixel is set to red and the player pixel is set to green and then shown on the HaloHD. Before doing this clear the previously shown LEDs on the HaloHD so the enemy and player pixels last position are removed.

Python Blockly Update LEDs

This condition checks whether the player lost against the enemy pixel. It determines whether the game loop is exited or not, and therefore if the game is continued.

 

Python Blockly Check Lost

This statement decides whether the game has been won, and the challenge has been completed at the fastest speed. The next bit of code sleeps the loop for the amount of time that’s set related to the round.

Python Blockly Check Won

Full Game Code

# Start Code Here
from microbit import *
import random
from KitronikHaloHD import *
halo = HaloHD()
delay = 1000
enemy = 0
player = 0
winning = True
valid = False
def checkValid(enemy, player):
  if enemy == player:
    return False
  elif enemy + 1 == player:
    return False
  elif enemy == player + 1:
    return False
  elif enemy % 2 != player % 2:
    return False
  return True
while winning:
  while not valid:
    enemy = random.randint(0, 59) # your own code
    player = random.randint(0, 59) # your own code
    valid = checkValid(enemy, player) # your own code
  enemy += 1
  player -= 1
  if enemy > 59:
    enemy = 0
  if player < 0:
    player = 59
  halo.clearLEDs()
  halo.setLED(enemy, (64, 0, 0))
  halo.setLED(player, (0, 64, 0))
  halo.showLEDs()
  if enemy == player:
    if button_a.was_pressed():
      delay -= 50
      valid = False
    else:
      display.scroll("Game LOST!")
      winning = False
  if delay == 100:
    display.scroll("Game WON!")
    winning = False
  sleep(delay)

Leave a comment

All comments are moderated before being published