TeachCraft Lesson 6

Bow/Arrow as a Magic Code-Wand

Similar to lesson 5, we're going to poll for events! However this time, we're going to use a bow/arrow instead of a sword! Each event we get will be the block that an arrow we shot landed in - this will let us target code-spells at whatever location we shoot!


In this specific example, it's setup so when you shoot a block with an arrow, your code is executed.


Whereas in lesson 5's example with a sword, we polled the function mc.player.pollBlockHits(), while in this example we will poll the function mc.player.pollProjectileHits(). In our for loop, each loop's blockhit variable contains information on the block you hit, and we can access its position via blockhit.pos


In the script on the right, everytime we shoot a block with a arrow, the code executes that turns that block into gold. Try it!

import time
from mcpi import minecraft
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

while True:

    for blockhit in mc.player.pollProjectileHits():
        pos = blockhit.pos
        gold_block_id = 41
        mc.setBlock(pos.x, pos.y, pos.z, gold_block_id)

    time.sleep(1)


Your Challenge: Turn your bow/arrow into a magic wand!

There's a lot of different actions you can trigger with your bow/arrow...

Your Challenge: Write a script that teleports your player to the location of any arrow you shoot. In this way, you can quickly scale mountains or travel long distances!