Here’s a few little Python files that illustrate control of Minecraft. It starts with connecting to the Minecraft API and writing to the screen and then develops it using loops to create towers, stairs, stacks of TNT and beyond. Note that most of the blocks will be drawn at coordinates (0,0,0) so you’ll have to navigate around to find them. Occasionally Minecraft draws a mountain in the way, so the drawing might be underground.
Sending text to the screen:
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!")
Creating a stack of ten blocks:
This little code snippet uses a loop to count from 0 to 9. (It’s a feature of Python that the loop starts at zero and finishes one before the last number asked for). The setblock function then places a block at each of the Y locations from 0 up to 9.
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!") for stack in range(10): mc.setBlock(0,stack,0,1)
Creating a stack of one hundred blocks:
Here the range of values being created is much larger. This extends a long way up into the sky.
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!") for stack in range(100): mc.setBlock(0,stack,0,1)
Creating a huge stack of TNT ready to explode:
This version of the previous program uses setBlock but with the TNT block type (46). This is then followed with a 1 to ensure that the TNT can be triggered.
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!") tnt=46 for stack in range(100): mc.setBlock(0,stack,0,tnt,1)
Creating a staircase which extends into the sky:
This snippet moves the blocks along the X and Y axis at the same time to ensure that the blocks move up and forward at the same time. This creates a staircase effect.
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!") tnt=46 for stack in range(100): mc.setBlock(stack,stack,0,1)
Creating a wide staircase:
This modification of the staircase duplicates the stacking, making sure that the blocks are placed at Z=0, Z=1, Z=2 and Z=3. The staircase is much wider and more interesting.
from mcpi import minecraft mc = minecraft.Minecraft.create() mc.postToChat("Python is now in control!") tnt=46 for stack in range(100): mc.setBlock(stack,stack,0,1) mc.setBlock(stack,stack,1,1) mc.setBlock(stack,stack,2,1) mc.setBlock(stack,stack,3,1)
Using a nested loop to create a row of stacks:
Python allows one loop inside another. This means that a stack can then be placed multiple times. The output of this looks like a row of fence posts, although it could equally be a set of bridge supports over a wide gap.
from mcpi import minecraft mc = minecraft.Minecraft.create() #mc.postToChat("Python is now in control!") for row in range(0,50,5): for stack in range(10): print("Row ",row," : stack ",stack) mc.setBlock(row,stack,0,1)
Creating a grid of stacks:
This is another nested loop which creates a grid of stacked cubes. This might be ideal for the columns supporting the roof of a temple.
from mcpi import minecraft mc = minecraft.Minecraft.create() #mc.postToChat("Python is now in control!") for columns in range(0,50,5): for row in range(0,50,5): for stack in range(10): print("Row ",row," : stack ",stack) mc.setBlock(row,stack,columns,1)