Maze Runner Micro:bit

Navigate a series of mazes without running into the walls!


YOU WILL NEED

  • 1 x BBC micro:bit
  • 1 x Tinkercademy Breakout Board
  • 1 x Micro USB cable
  • 1 x Buzzer
  • 1 x ADKeypad
  • 2 x F-F Jumper Wires

STEP 1

Connecting Components

(a) Slot the micro:bit into the top of the Tinkercademy breakout board.

(b) Connect the external components to the Tinkercademy breakout board.

• Use 2 F-F jumper wires to connect the ‘+’ end of the buzzer to the yellow signal (S) pin, and ‘-’ end to the black ground (G) pin.

• Connect the ADKeypad to pin1, matching the colours of the wires to that on the breakout board pin!





Materials

  • 1 x BBC micro:bit
  • 1 x Tinkercademy Breakout Board
  • 1 x ADKeypad
  • 1 x Buzzer
  • 2 x F-F Jumper wires


STEP 2

Code tl;dr

(a) To create a maze game on the micro:bit, we will use the LED screen on the micro:bit.
• A blinking red LED represents the player’s position
• Solid red LEDs represents the walls
• Turned off LEDs represents the maze paths

(b) To track the player's location, we will use the LED coordinates on the micro:bit's LED screen.
• x coordinates start from 0 (left) to 4 (right)
• y coordinates start from 0 (top) to 4 (bottom)



STEP 3

On Start

(a) Create 'gameOn' to track whether the game is active / over.
• Set to 'True' at the start to start the game once the micro:bit is powered.

(b) Create 'playerx' and 'playery' to track the player's x and y positions.
• You can choose any x and y value for the starting point, but do remember it to set up our maze level later.

(c) Create 'level' to track the maze level. It should start at level 1.



STEP 4

Importing Tinkercademy Extension

To display messages on the OLED screen and respond to button press controls on the ADKeypad, we need to import an extension.

(a) Click on the ‘Advanced’ drawer, select 'Extensions'.

(b) Search ‘tinkercademy’ and click 'tinkercademy-tinker-kit' to import. You will see 2 new drawers on Makecode - ‘OLED’ and ‘Tinkercademy’.

(c) As importing this package is only valid for the current project, you’ll need to re-import it for new projects.



STEP 5

Starting Music & Message

When the micro:bit is powered, we can add some starting music and message. Adding to 'on start' (step 3),

(a) Play a melody. Set it to play 'once in background' so the rest of the code can run even when it hasn’t finished playing.

(b) Display “Maze Runner”, the name of the game, on the micro:bit screen.



STEP 6

Display Player LED

To display the player's LED on the micro:bit screen, alternate the ‘plot x y’ block with the ‘pause’ block (0.3s) in a forever loop.

(a) Use 'playerx' and 'playery' variables instead of numbers in the 'plot x y' block to easily update them later and automatically plot the latest updated player location.

(b) Alternating the blocks blinks the player LED on and off so it’s easily identifiable, and adds some delay so the player won’t be immediately re-plotted when we display the maze walls later (step 7).

(c) You can customize the 'pause' block to change the player LED flash speed.



STEP 7

Movement Controls
Now let's add some movement controls with the ADKeypad!
(a) Change the player's LED position by changing the values stored in 'playerx' or 'playery' to move the player left/right or up/down respectively.

(b) To the same forever block (step 6), add new ‘if’ and ‘elif’ statements to detect button presses (from 'Tinkercademy’ drawer).
• Set A to move up
• C to move down
• D to move left
• E to move right

(c) Pausing after each button press adds some delay so the player LED doesn't move many spaces on each button press.



STEP 8

Adding Maze Levels

Let's create our maze levels!
(a) Use the 'level' variable to store the level number.

For every level:
(b) Display the maze walls on the micro:bit screen. Create it such that the player's starting position is not inside a wall!

(c) Forever check if the player (playerx, playery) runs into a wall.
(i) For level 1: First wall is where playerx ≤ 2 AND playery ≤ 2. Second wall is where playerx = 4 OR playery = 4.
(ii) If yes, set gameOn to 'false'. Game Over!



STEP 9

Check for Success

(a) Adding to the same block (step 8), add an ‘elif’ statement to check if the player completes the maze (x=3 and y=0 for level 1).

(b) If yes,
(i) play success melody in the background
(ii) set player's starting position for the next level (it can be a different position from previously!)
(iii) display a smiley face to inform the player they succeeded
(iv) set gameOn to true to show the LED for the player’s starting position
(v) change 'level' variable by 1 to show the next level

(c) We'll need to do this later for every level too.



STEP 10

Game Over!

What happens when the player runs into a wall and it's game over?
(a) Detect this by using a conditional to check if 'gameOn' is false.
(b) If yes,
(i) Play a sad melody in the background
(ii) Reset ‘level’
(iii) Unplot the player LED
(iv) Show an angry face
(v) Display a message on the OLED to inform the player how to restart



STEP 11

Restarting the Game

Now, let's restart the game when button B on the ADKeypad is pressed.

(a) Add an ‘elif’ statement to the button press conditionals (step 7) in the forever loop.

(b) If button B is pressed,
• Set ‘level’ to 1, to restart the game from level 1
• Reset the player’s starting location by setting the ‘playerx’ to 0 and ‘playery’ to 3
• Reset ‘gameOn’ to ‘true’, to run the code from step 6 displaying the player's position



STEP 12

More Levels
Let's add more levels! Add them as ‘elif’ statements to the Level 1 code (step 9).
(a) Duplicate the blocks under our Level 1 code (step 9).

(b) But for each new level, change the maze walls and playerx, playery coordinates for our conditionals that check if the player crashed into a wall or completed the level.

(c) After the player completes the level and you reset their playerx and playery variables, make sure this position matches the next level so they don't start inside a wall!

(d) If it is complicated to create conditionals to test for every wall, break down your walls into separate rectangles and do so for each rectangle.



STEP 13

Victory!

Once you’re done adding in levels, you can also optionally create a victory section to congratulate the player for completing the game.

(a) Add this as an ‘elif’ statement to the previous code (step 12).

(b) Once the player successfully completes the first 3 levels and 'level' = 4:
(i) Unplot the player LED
(ii) Play a victory melody in the background
(iii) Show a heart icon on the micro:bit screen
(iv) Show a victory message on the OLED screen

Feel free to customize this section!



STEP 14

Complete!

(a) Congratulations, you've finished coding your maze runner game!

(b) Don't forget to connect the top of your micro:bit to your laptop using a microUSB cable, and click 'Download' on MakeCode.

(c) We hope you've learnt about the external components and conditionals, which you can also incorporate into other projects.

(d) You can also connect an OLED display so the messages don’t scroll word-by-word on the micro:bit screen - you can refer to our ‘OLED ver’ code below!

(e) Have fun playing, enjoy!


Code and References

Made this activity? Good Job! Now spread the word. Share a photo of your creation on social media with #letsgethacking #MazeRunnerMicro:bit