In this project, we'll expand on our previous project where we integrated the LED Matrix to indicate our position in the Minecraft world. In the previous project, we scaled our position in the Minecraft world to a position on the LED Matrix, but when our scaled position was outside the LED Matrix boundary of 0 to 7, we forced it to either 0 or 7 and displayed it at that location.
In this project, when we determine that our character is outside the bounds of the LED Matrix, instead of just displaying the location as the edge of the LED Matrix, we'll display the location at the edge and flash the LED. This will indicate that we're outside of the scaled world.
The main difference between the code in the last project and the code in this project is that, for this project, we'll add an extra test to determine if the location of our character is outside the boundaries of the LED Matrix, and if so, we'll flash the LED for as long as that condition continues to be true.
Here is what the full code for this project should look like. We'll dig further into the highlighted pieces of code below:
The above highlighted changes are:
On Lines 11 through 13, we initialize the variables we'll use in the game loop. We'll discuss their use below.
On Lines 19 and 23, we determine if Steve has moved out-of-bounds. He's out-of-bounds if his scaled position would be off of the LED Matrix (i.e., less than 0 or greater than the width of the LED Matrix).
On Lines 27 through 30, we keep a count that keeps track of how long
we flash on and off. Every time through the loop, we increment a
count
. If the count
exceeds
FLASH_COUNT
, then we toggle the flash_lit
variable, which keeps track of whether the flashing LED is on or off.
On Lines 31 and 32, we light the Steve's location on LED Matrix.
We light it in two cases: when its not out-of-bounds, or if it's
flash_lit
.
Give it a try...take Steve to the edge of the Minecraft world and you should see the LED Matrix flash his location when he moves off the edge.
Can you make the out-of-bounds flashing occur at a slower rate?
Can you make the flashing occur only when Steve goes out-of-bounds in the north/south direction (the x axis)?
Can you make the flashing occur only when Steve goes out-of-bounds in the south direction (the x axis)?