Tutorial
First, start up Minecraft Pi under Menu->Games. Click Start Game and create a new world. Try walking around and destroying blocks.
Next, start up Python 2 under Menu->Programming. Once the Python Shell starts, try the following commands:
>>> from mcpi import minecraft
>>> mc = minecraft.Minecraft.create()
>>> mc.postToChat("Hello world")
To find your location:
>>> x, y, z = mc.player.getPos()
Note that “x” is your east-west position, “z” is your north-south position, and “y” is your up-down position.
Let’s teleport ourselves 100 squares in the air:
>>> mc.player.setPos(x, y+100, z)
Now let’s create a stone block, then change it to diamond:
>>> mc.setBlock(x, y, z+1, 1)
>>> mc.setBlock(x, y, z+1, 57)
See the next page for the list of possible blocks.
We can create a whole cube of blocks with a single call:
>>> x, y, z = mc.player.getPos()
>>> mc.setBlocks(x+1, y+1, z+1, x+11, y+11, z+11, 1)
Let’s completely surround ourselves with TNT. Note that we need to use an 8th argument (set to 1) to “arm” the TNT:
>>> TNT = 46
>>> x, y, z = mc.player.getPos()
>>> mc.setBlocks(x-5, y-1, z-5, x+5, y-1, z+5, TNT, 1)
Go ahead and destroy the TNT…
Enough destruction, let’s drop some flowers while we walk. This is easier to type in a text editor. In the Python Shell, choose File -> New Window to create a new text editor window.
from mcpi import minecraft
from time import sleep
mc = minecraft.Minecraft.create()
flower = 38
while True:
x, y, z = mc.player.getPos()
mc.setBlock(x, y, z, flower)
sleep(0.1)
Save the program as myflower.py, then choose Run -> Run Module.
Next, start up Python 2 under Menu->Programming. Once the Python Shell starts, try the following commands:
>>> from mcpi import minecraft
>>> mc = minecraft.Minecraft.create()
>>> mc.postToChat("Hello world")
To find your location:
>>> x, y, z = mc.player.getPos()
Note that “x” is your east-west position, “z” is your north-south position, and “y” is your up-down position.
Let’s teleport ourselves 100 squares in the air:
>>> mc.player.setPos(x, y+100, z)
Now let’s create a stone block, then change it to diamond:
>>> mc.setBlock(x, y, z+1, 1)
>>> mc.setBlock(x, y, z+1, 57)
See the next page for the list of possible blocks.
We can create a whole cube of blocks with a single call:
>>> x, y, z = mc.player.getPos()
>>> mc.setBlocks(x+1, y+1, z+1, x+11, y+11, z+11, 1)
Let’s completely surround ourselves with TNT. Note that we need to use an 8th argument (set to 1) to “arm” the TNT:
>>> TNT = 46
>>> x, y, z = mc.player.getPos()
>>> mc.setBlocks(x-5, y-1, z-5, x+5, y-1, z+5, TNT, 1)
Go ahead and destroy the TNT…
Enough destruction, let’s drop some flowers while we walk. This is easier to type in a text editor. In the Python Shell, choose File -> New Window to create a new text editor window.
from mcpi import minecraft
from time import sleep
mc = minecraft.Minecraft.create()
flower = 38
while True:
x, y, z = mc.player.getPos()
mc.setBlock(x, y, z, flower)
sleep(0.1)
Save the program as myflower.py, then choose Run -> Run Module.