Blowing up things!

So far we learned about functions, conditions and loops. Lets use our knowledge so far to do something fun - blow up things!

In this adventure we will build structures with TNT. Once we are done building with TNT and we are ready we'll explode the TNT.

Lets get started ...


In [ ]:
# Run this once before starting your tasks
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
import thread
mc = minecraft.Minecraft.create()

Task 1: Toggling TNT building

We need to know when Steve is building a TNT structure and when he is done building and just wants to move around in the world. There are two phases when Steve is moving around

  • Steve is moving to build with TNT
  • Steve is moving around normally and is not building with TNT

We use a variable named buildingWithTNT and set it to the value True when Steve is in the building phase and set the variable to False when he is not building with TNT. Note that the variable takes only two values True and False. When the current value of the variable is True, the next value it should take is False and when the current value is False, the next value the variable should take is True. This technique of setting the next value of the variable to the opposite of its current value is called toggling. In Python an easy way to toggle a boolean variable is shown below

myvariable = False # initial value
myvariable = not myvariable # toggles value

Complete the program below to add the variable buildingWithTNT and toggle the variable when run and verify your program runs correctly by printing the variable value with a string like 'buildingWithTNT = False'.


In [ ]:
# Task 1 code
# add a variable with an initial value
# toggle the variable
# print the value of the variable

Task 2: Building with TNT

To build with TNT blocks, we first have to toggle the buildingWithTNT variable by executing the Task1 code block. This will toggle buildingWithTNT to True. Then Steve has to run (or jump or fly) wherever he wants to place the TNT blocks. Once we are done building, we have to toggle the buildingWithTNT variable again by executing the Task1 code block again. This will now toggle the buildingWithTNT variable to False and Steve will now move normally.

Lets write the function placeTNTBlock that will set the block that Steve is currently on to a TNT block. A TNT block has the id block.TNT.id. Remember that in order to define a function, one needs to use def Its a good idea to review functions in Adventure 3 before you write the function placeTNTBlock.

Make sure that in your function, you set a TNT block only if the buildingWithTNT variable is set to True. Use an if conditional to do so. Your program should look like the one below

def myfunction():
    while True:
        time.sleep(0.1)
        pos = mc.player.getTilePos()
        # if buildingWithTNT is True set the block at the current position to block.TNT.id

Great Job


In [ ]:
# Task 2 code

We need to run the function you defined in Task 2 using the following statement.

thread.start_new_thread(placeTNTBlock,())

We need to do so since we are going to toggle the buildingWithTNT variable as the program is running.

Note: Ask your instructor for help if you get stuck running your program or run into errors!


In [ ]:
# Run Task 2 function on using thread.start_new_thread

Task 3: Build and Blow Up!

Now that your function is running, try the following steps in order

  1. In Minecraft, move Steve to the location you want to start building a TNT structure
  2. In the jupyter editor, toggle the buildingWithTNT variable by running the code in Task 1
  3. In Minecraft, move Steve to build the structure by placing TNT blocks wherever he moves
  4. In the jupyter editor, toggle the buildingWithTNT variable by running the code in Task 1
  5. Now move Steve and place a redstone block on one of the TNT blocks and watch the explosion!

Try building more TNT structures and blowing them up.