Introduction: Pixel Chaser Game With Micro:bit

About: ICT teacher passionate about coding ☺️

A fun play game that is easy to code and all you need is a micro:bit board !!!

I am an ICT teacher and I have created several projects with the micro:bit board at the beginning of this year I wanted to motivate my students more for coding so I decided to create an easy and fun game with micro:bit. After a research I chose this pixel chaser game. Indeed I found several tutorials on the game and all of them use the arduino board and the adressable LEDs so I thought why not use the microbit board with its 25 LEDs screen to develop the game and the result was great !!!

Supplies

Note that if you don't have the board you can use his simulator on https://makecode.microbit.org/

Step 1: Game Rules

The goal of the game is to stop the cycling light when it reaches the indicated LED and in the meanwhile you have to avoid enemy LED . If successful, your score will increase and the difficulty level too. If unsuccessful, the light cycle will restart.

Usually this game is made using a strip of addressable LEDs to create a light cycle so in this version since I'm using LEDs from the micro:bit screen I decided to only use the LEDs at the ends.

I used micro:bit buttons to direct the sprite and to catch the pixels. Let's move to the code !!!

Step 2: Coding

To code this game, I used MakeCode for micro:bit, a block-based visual programming tool. It's really easy to use, just drag and drop your blocks to write your code and you can see the result instantly on the simulator. 

  • On start block I called first of all to the function "animation_led" to animate the start of the game. I created then the three players of the game which are "sprite", "coin" and "ennemy" which are respectively the led chaser, the led to catch and the enemy led to avoid. Others initializations are made in this block such as set speaker ON and set the brightness of the "coin" LED to 255 and its blink to 200 and set the brightness of the "enemy" LED to 155.


  • The user can use the buttons A and B to move the led "sprite" forward and backward for this I created a function named "avancer" which allows the player to move forward by 1 and another function named "reculer" which allows the player to move backward by 1 special positions were treated in these two functions and which required that the player change direction before making his move and that its position must always be between that of "coin" and that of "ennemy" . 


  • To earn a point the user has to press the buttons A and B at the same time when the position of the led "sprite" is the same as the led "coin" in this case the score is incremented by 1 and the functions "creer_coin" and "creer_ennemy" are then called. Conditions were checked during the creation of the two players "coin" and "ennemy" is that their positions are not confused and that they must be positioned on the extremities of the microbit screen that is to say the leds having the following positions (0,1) (0,2), (0,3), (0,4), (0,4),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(4,0),(3,0),(2,0),(1,0)

The position (0,0) is not considered because it is reserved as the starting position of the "sprite" LED when the game start.

  • The game is over for two conditions: when user press A an B at the wrong time or when "sprite " hits the "ennemy"

You can Click here to open the editor in a separate browser tab or you can download the file (.hex) below