The aim is to align the three LEDs that “go down” on the same line. Button A is used to make
start the game, button B is used to reset the game after a game and therefore be able to play a new one
challenge.
| Code construction |
|
| The first step is to understand what we want to achieve visually on the microbit LED panel, i.e. a light running down from top to bottom.
In this case, the column used is the leftmost one, which is the space variable x always set to 0. By setting a plot-unplot pair, a for loop that covers the 5 positions involved and a pause to regulate the movement speed, the desired result is achieved. Within a forever loop, that loop is repeated endlessly.
As for the remaining two columns (x=2 and X=4), the same procedure is repeated, since all the forever loops of the same program are processed together.
|
 |
 |
By running such a program, it is immediately observed that the three luminous dots will run parallelly on our panel. Simply set pauses of different values in order to (even slightly) offset them. Each pause can be given a proper name, to be stated at the beginning, by indenting time1, time2 and time3 into OnStart. As for the value (ms), the matter can be randomized by using the appropriate mathematical operators:
|
 |
 |
| The multiplier operator is used to make our choice more consistent and practical. Random values can be set with wider steps, in order to detache them from one other.We can try to execute this code. It will make a difference. |
|
 |
Now we need to move the dots when we want them to do it. At each for loop an if is indented. The triggering event is a variable set upon a pressing action, so a microbit button is used, When the action is set, the for loops start working. In order to stop them, an antithetical button needs to be set, so as to gain complete control over the flow. The variable will be boolean in type.
|
 |
 |
At this point, the first "challenge" can be faced, namely slowing down the dots flow until it stops, otherwise it would last forever.Step by step procedure: slow down the dot, take column x=0 as an example, by resetting the pause time at each cycle by adding a little time; time1 equalling itself plus a small random value, in order to further deviate from deterministic methods and give variability to our functions. |
The loop must be stopped at a precise point.
Who decides that? The answer varies based on the solution and framework created up to this point.
It is good rule to work on the pause time (inscribed in an if with a comparison operator) which terminates the loop with a break. An expedient would be to ABSOLUTELY have the last dot corresponding to y as set by index before break. The whole function is indented within a further if, with the reversed sign comparison operation, to avoid rewriting the dot in position x=0; y=0.
The same must also be done for the code blocks relating to columns x=2 and x=4. It should be done by using the MakeCode simulator.
|
 |
| Now we are ready for the final rush. What is missing is to establish that if the dots stop on any line, Microbit will have to indicate that the player has won. Clearly, the three dots must stop on the same line. They equal the index value, which is a progressive number repeated several times in the for loops. This mode of operation is the same in the three loops. However, they are variables only inside the for loop and when break exits the loop, their value is always reset to zero and the equalities would always be verified.
|
 |
E.g.: take column x=0: a new variable is defined inside the for loop, y1, with value set by index. When break breaks the loop, the variable y1 is returned without being reset to zero. A comparison can now be made by repeating the procedure on the other two columns.
Enjoy the game!
|