micro:bit Project 12: Escape the maze
Micro:bit is trapped inside the maze with a time bomb! Poor micro:bit cannot move by itself. However, it sends out signal through the LED panel to lead the way. We need to help him escape the maze before the bomb explodes!
Connect the RGB LED strip to P1.
STEP1: First, we will need to let micro:bit randomly generate some movements to simulate the maze. Also, micro:bit should be able to know which way it tilts. If the two directions match, 1 point will be added to the final score, which means that we are 1 step closer from escaping the maze.
The following flow chart may be helpful in explaining how the program works.
We will use number 0 ~ 3 to represent four target directions respectively. The function "pick random" under “Math” help us to get a random number from 0 ~ 3.
STEP2: Show the arrow image on the LED panel. The arrow should correspond to the randomly generated target direction we got from above. Again, the “if-else” function will be our choice to do the job.
STEP3: Use the “on xxx gesture” function under “input” to sense which direction the micro: bit tilts. The number inside the function loop represents four different directions.
STEP4: Put everything we mentioned above inside an “if-then” loop to compare if the two directions match. Also, don't forget the “forever” loop to keep the game always on going.
Note: it will be helpful to add a “While” function inside the loop, so as to avoid identical directions appear in a row.
STEP5: it's the time to add the bomb. The bomb will start ticking as soon as the game begins. Also, we will use the RGB LED strip to simulate the fuse. With the time running, the LEDs on the fuse will go off one after another in a steady pace. If micro:bit cannot make it to escape the maze before the fuse burning out, the game is over.
The flow chart below shows how to set the LED go one by one with the time going.
We will be explaining the functions that we need to achieve it.
Note: Micro: bit needs extra time to process the data, so the clock will run a bit slower than what we set.
The following program is used to set how many LED lights up.
STEP6: A scoring mechanism will be needed to tell us whether we made it to escape. The following flow chat may give you an idea of how it works..
Remember to reset the score at the beginning of the program.
STEP7: Now, integrate the scoring function to the main program.
STEP8：Here is how the final program looks like
Try to make the clock tick faster to increase the difficulty;
Program the LEDs to make them flash at a pace based on the remaining time;
Unintuitive challenge! Set different directions in pairs and see if you can still finish the game. For example, you will need to tile top to match a left arrow, tile bottom to match a right arrow.