Welcome to the Minecraft coding tutorial! Follow the steps below to build a dynamic structure in Minecraft Education.
Press T in Minecraft to open the chat, and type run
. This command will trigger our code. Start by adding this:
def on_on_chat():
pass
player.on_chat("run", on_on_chat)
What do you think will happen when we type run
in the game? Test it out!
Now, define the size of your structure and where it will appear in the game. Add this inside the chat command:
size = 10 # Change this to adjust the size
base_x, base_y, base_z = 1, 0, 0
What happens if you make size
bigger? Predict where the structure will appear!
Create a function to build a single pillar. Paste this inside the chat command:
def build_pillar(x, z):
blocks.place(POLISHED_BLACKSTONE, pos(x, base_y, z))
blocks.place(BLACKSTONE_WALL, pos(x, base_y + 1, z))
blocks.place(OAK_FENCE, pos(x, base_y + 2, z))
blocks.place(POLISHED_BLACKSTONE, pos(x, base_y + 3, z))
What kind of blocks would YOU use for your pillar? Customize it and try it out!
Call the build_pillar
function to place four pillars in a square:
build_pillar(base_x, base_z)
build_pillar(base_x + size, base_z)
build_pillar(base_x, base_z + size)
build_pillar(base_x + size, base_z + size)
Can you predict where the four pillars will appear? Let’s see if you’re right!
Create a function to connect the pillars with glass:
def connect_with_glass(x1, z1, x2, z2):
if x1 == x2: # Vertical connection
z3 = min(z1, z2) + 1
while z3 < max(z1, z2):
blocks.place(GLASS, pos(x1, base_y + 3, z3))
z3 += 1
elif z1 == z2: # Horizontal connection
x3 = min(x1, x2) + 1
while x3 < max(x1, x2):
blocks.place(GLASS, pos(x3, base_y + 3, z1))
x3 += 1
Now call the function to connect all sides:
connect_with_glass(base_x, base_z, base_x + size, base_z)
connect_with_glass(base_x, base_z + size, base_x + size, base_z + size)
connect_with_glass(base_x, base_z, base_x, base_z + size)
connect_with_glass(base_x + size, base_z, base_x + size, base_z + size)
What happens if you use a different block instead of glass? Try it out!
Decorate the middle of the square with BIG DRIPLEAF:
blocks.fill(
BIG_DRIPLEAF,
pos(base_x + 1, base_y, base_z + 1),
pos(base_x + size - 1, base_y + 1, base_z + size - 1),
FillOperation.REPLACE)
What other blocks would look cool in the center? Experiment and share your ideas!
Here’s the complete code to copy and run in MakeCode:
def on_on_chat():
size = 10
base_x, base_y, base_z = 1, 0, 0
def build_pillar(x, z):
blocks.place(POLISHED_BLACKSTONE, pos(x, base_y, z))
blocks.place(BLACKSTONE_WALL, pos(x, base_y + 1, z))
blocks.place(OAK_FENCE, pos(x, base_y + 2, z))
blocks.place(POLISHED_BLACKSTONE, pos(x, base_y + 3, z))
def connect_with_glass(x1, z1, x2, z2):
if x1 == x2:
z3 = min(z1, z2) + 1
while z3 < max(z1, z2):
blocks.place(GLASS, pos(x1, base_y + 3, z3))
z3 += 1
elif z1 == z2:
x3 = min(x1, x2) + 1
while x3 < max(x1, x2):
blocks.place(GLASS, pos(x3, base_y + 3, z1))
x3 += 1
build_pillar(base_x, base_z)
build_pillar(base_x + size, base_z)
build_pillar(base_x, base_z + size)
build_pillar(base_x + size, base_z + size)
connect_with_glass(base_x, base_z, base_x + size, base_z)
connect_with_glass(base_x, base_z + size, base_x + size, base_z + size)
connect_with_glass(base_x, base_z, base_x, base_z + size)
connect_with_glass(base_x + size, base_z, base_x + size, base_z + size)
blocks.fill(
BIG_DRIPLEAF,
pos(base_x + 1, base_y, base_z + 1),
pos(base_x + size - 1, base_y + 1, base_z + size - 1),
FillOperation.REPLACE
)
player.on_chat("run", on_on_chat)
Now, press T in Minecraft, type run
, and watch your structure come to life!