In this project, we will allow our Minecraft character to place a bomb anywhere in the Minecraft world with a the press of a button, and then detonate that bomb with another press of the button.

For this project, we only need a single button on the breadboard. In our code below, we assume the button is attached to GPIO24. You are welcome to use a setup from a previous project, as long as one of the buttons is correctly attached to GPIO you use in your code:

For this project, we'll create a game loop that has two modes:

- In Place mode, when the button is pressed, we place a bomb at the location immediately in front of Steve. Then, we change to Detonate mode.

- In Detonate mode, when the button is pressed, we detonate the bomb that was previously placed (using a separate `detonate()` function that we create). Then, we change back to Place mode.

To keep track of the modes, we use the variable `place_mode`, which will be set to `True` when we're in place mode. The game starts in place mode.

To find out where the bomb is placed when the button is hit in Place mode, we use the `pollBlockHits()` function, similar to the way we used it in the project.

Finally, our `detonate()` function is identical to the `dig()` function in the project.

Here is what the full code for this project should look like:

from rstem.button import Button from rstem.mcpi import minecraft, control, block from rstem.mcpi.vec3 import Vec3 import time control.show() mc = minecraft.Minecraft.create() button = Button(12) def detonate(mc, pos, radius=1): r_vector = Vec3(radius,radius,radius) mc.setBlocks(pos - r_vector, pos + r_vector, block.AIR) place_mode = True while True: if place_mode: if button.presses(): control.hit(duration=.2) hits = mc.events.pollBlockHits() if hits: tnt_pos = hits[0].pos mc.setBlock(tnt_pos, block.TNT) place_mode = False else: if button.presses(): detonate(mc, tnt_pos) place_mode = True time.sleep(0.01)

Give it a try... Hit the button once to place the bomb, then move away and hit the button again to detonate the bomb.

1. Assuming you still have your controller wired up, add this bomb code to the Minecraft controller code ( project) so that you can place a bomb while using the CREATOR Kit as a controller.

2. The default radius of our explosion is 1. Can you change the code to use a larger radius? Don't go too big, if you do, you can end up deleting the whole Minecraft world (and the Minecraft server will likely hang for a while as that occurs). Don't forget, though, you can always recreate a new world from the Minecraft start screen.

3. Can you change the code to allow Steve to place two blocks, one after the other, and detonate them both together?