Let’s go through building a simple program to make “Minesweeper”(or battleship for the analog generation)
I will create a variable above and then explore how that variable behaves when I tilt the Microbit. I called my variable “xCord”, I used this short version of “X coordinate” because that’s what the variable will control, the x coordinate position.
I give the variable a first value of “0” in the “On Start “block.
Now let’s add some tilt blocks and print the variable to the screen to see if it does what we think it does.
I pulled the tilt blocks from”Input” an grabbed the “change xCord” from the variables palette.
after I build the blocks out, I can test by hovering over the sides of the virtual microbit and seeing how it affects the value of my variable, “xCord”.
Tilt left subtracts, or decrements, the variable and tilt right adds, or increments, the variable.
Now to move a pixel.
I change my “Show number” block to the “Plot x, y” block which is in the “LED” palette. Next, add the “xCord” variable to the “x” value window, This will light the LED that corresponds to the place (xCord, 0) on the pixel screen of the Microbit.
Next, I’ll add an “unplot x, y” command in both the tilt blocks to turn off the previously lit LED when there is a change in the value of “xCord”. I do this for both the “tilt left” and the “tilt right” blocks.
when this is complete, Your pixel should move on the x-axis in response to tilting the Microbit.
Now let’s create a yCord variable to move on the y-axis.
Once I create the ‘yCord variable, I use it in the “plot x, y” block to define the y coordinate.
Now we need to create a “fence” so that the LED cant move off the screen.
I’ll build two if statements to keep the numbers between 0 and 4.
I created a ”yCord” variable and set it to “0” in the setup function. Then I will plug the “yCord variable into the “y” value in the plot block. This will allow me to do the same kind of movement in the Y axis.
Now let’s build the tilt functions for the y axis, or “up and down”.
I basically use the same blocks and change my values to correspond to the “y” axis.
Don’t forget to add the “yCord” variable to the “tilt right” and “tilt left” blocks
To complete the movement of the LED pixel, we need to clone the if statements that are the “fences” and apply the “yCord” value to them.