In honour of Halloween, we will create a spooky block that follows us around. Once you have the code working, you can build on the basic spooky block and make it into a minecraft creeper, a weeping angel from Dr Who or a ghost.
Write a program to create a block a little bit away from your player’s current position. Each time you move, have the spooky block move a little bit closer to where you are.
Hint: Easiest way follow is to change remove the block at the current position by changing it to “air” and rebuild the block at the new location.
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
import random
# build the figure using the given material
def build_spooky_figure(world, x, y, z, material):
for i in range(3):
world.setBlock(x, y + i, z, material)
world = minecraft.Minecraft.create()
world.postToChat("What ever you do ...")
time.sleep(5)
world.postToChat("Don't blink!")
# create the spooky block 20 blocks away
playerTile = world.player.getTilePos()
x = playerTile.x
z = playerTile.z + 20
y = world.getHeight(x, z)
build_spooky_figure(world, x, y, z, block.OBSIDIAN)
while True:
time.sleep(random.randint(1, 5))
playerTile = world.player.getTilePos()
# remove the spooky block from old position by building
# over the existing figure with "air"
build_spooky_figure(world, x, y, z, block.AIR)
# move it to new position
if (x - playerTile.x) > 0: x -= 1
if (x - playerTile.x) < 0: x += 1
if (z - playerTile.z) > 0: z -= 1
if (z - playerTile.z) < 0: z += 1
y = world.getHeight(x, z)
build_spooky_figure(world, x, y, z, block.OBSIDIAN.id)