Python in Minecraft 6 – stone floor tunnel with torches

In the previous example the tunnel was glass all around. To make a more interesting tunnel this example adds a stone floor and adds a torch. We will develop this tunnel in two stages. The first stage is to create a profile showing what a cross-section of the tunnel will look like for any value of x.

nano tunnelprofile2.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
x = -190
y = 72
z = -222
mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)
mc.setBlocks(x,y,z-1,x,y,z+1,block.STONE)
mc.setBlocks(x,y+1,z-1,x,y+5,z+1,block.AIR)
mc.setBlock(x,y+1,z+1,block.TORCH.id,5)

python3 tunnelprofile2.py

The second stage is to create a loop to draw each cross section of the tunnel at each value of x between xmin and xmax. Note that at the ends we ant the wall of glass to stay so that lava and water don’t flow into the tunnel should it get built next to these liquids. We have achieved this with two loops. The first loop goes the full length of the tunnel from xmin to xmax. The second loop which adds air and torches starts and ends one position in from the ends. Torches are added every 4 positions by using the modulo (%) operator.

nano stonefloortunnel.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
xmin = -224
xmax = -200
y = 72
z = -222
for x in range(xmin,xmax+1):
    mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)
    mc.setBlocks(x,y,z-1,x,y,z+1,block.STONE)
for x in range(xmin+1,xmax):
    mc.setBlocks(x,y+1,z-1,x,y+5,z+1,block.AIR)
    if x % 4 == 0:
        mc.setBlock(x,y+1,z+1,block.TORCH.id,5)

python3 stonefloortunnel.py

Categories: CoderDojo