Python in Minecraft 4 – Creating a solid tunnel

Our next exercise it to create a tunnel of glass filled with air. Glass is useful because it is transparent. To start with the tunnel will be horizontal heading east which means the x coordinate will be increasing and the y and z coordinates will keep constant values. We could use the setBlocks command for the entire length of the tunnel. However we want to keep the code flexible enough that later on the tunnel could start ascending or descending, i.e. the y coordinate could start increasing or decreasing. The profile of the tunnel which will be a wall of glass 5 blocks across and 7 blocks high. Here is the code to create a profile of the tunnel for one value of the x coordinate.

nano tunnelprofile.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
x = -200
y = 72
z = -222
mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)

python3 tunnelprofile.py

A loop can be added which will draw the profile of the tunnel over and over again for a range of different x values. Note how important indentation is in python. I am indenting 4 spaces when in a loop.

nano solidglasstunnel.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)

python3 solidglasstunnel.py

Categories: CoderDojo