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:

from rstem.led_matrix import FrameBuffer from rstem.mcpi import minecraft, control import time control.show() mc = minecraft.Minecraft.create() SCALE = 25 fb = FrameBuffer() *count = 0 *FLASH_COUNT = 3 *flash_lit = True while True: pos = mc.player.getTilePos() x = round(pos.x/SCALE + (fb.width-1)/2) * x_out_of_bounds = not 0 <= x < fb.width x = min(fb.width-1, max(0, x)) z = round(pos.z/SCALE + (fb.height-1)/2) * z_out_of_bounds = not 0 <= z < fb.height z = min(fb.height-1, max(0, z)) fb.erase() * count += 1 * if count > FLASH_COUNT: * flash_lit = not flash_lit * count = 0 * if not x_out_of_bounds and not z_out_of_bounds or flash_lit: * fb.point(z, x) fb.show() time.sleep(0.01)

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.

1. Can you make the out-of-bounds flashing occur at a slower rate?

2. Can you make the flashing occur only when Steve goes out-of-bounds in the north/south direction (the x axis)?

3. Can you make the flashing occur only when Steve goes out-of-bounds in the south direction (the x axis)?