In the last project, we learned how to read the list of previous hit events, cycle through them and then take some action (printing out the coordinates of the block that was hit). In this project, we're going to continue with the same idea, but instead of just printing out information about the block that was hit, we're going to create an effect on that block.
The hardware configuration for this project is exactly the same as the
previous project — a single button mapped to GPIO24:
The code for this project is going to look identical to the code in our last project, with one exception, highlighted below:
In the example above, instead of just printing out where a block was hit, we turn the block into Air, making the block disappear.
The mc.setBlock()
function takes two arguments:
The location of the block that was hit: This was gathered by the
mc.events.pollBlockHits()
function; and
The type of block that should be created in this location: In this case, we're creating a block type of "AIR" — this is essentially the same as removing the block entirely.
You've now created a shovel, which, you've probably noticed, does the exact same thing as just smashing blocks. However, our shovel code can be modified to do a whole lot more than just smashing a single block. In fact, in the next project, we'll expand on our shovel functionality in a way that can't be done without custom programming.
Can you modify the code so that instead of breaking the block in front of you, instead you break the block behind you? How about to the left or right?