I’ve made a cheat sheet for the game controls in Minecraft Pi Edition. It provides a quick and easy way for beginners to familiarise themselves with the game controls. Check it out in the link below:
Controlling blocks is one of the most powerful things you can do with the Minecraft Pi API. You can access blocks that are not usually available to the player or build complex structures with a few lines of code instead of placing each block by hand. In this post we will cover the API methods that allow you to place blocks using you Python programs. If you need to set up Minecraft Pi check out my post here.
All block methods in Minecraft Pi use co-ordinates to determine the location of blocks. In the top left of the game window you can see the current co-ordinates of the player. Three variables represent the coordinates x, y and z; x and z represent the horizontal plane and y represents vertical height. This is shown in the following diagram:
All block methods use block IDs that identify the type of block you want to place at a location or already exists at a location. The block ID is an integer value and represents the blocks like soil, air, melon, lava and so on. You can find the IDs of the blocks on this cheat sheet, however not every block exists in Minecraft Pi Edition and most don’t function (for example you can’t use the chest to store things).
When building anything in Minecraft the setBlock() is the most method you’ll be using. It takes four arguments – x, y and z co-ordinates and a block ID – and places a block at that location. The basic syntax for this method is:
setBlock(x, y, z, blockID)
The following example creates a melon block (block ID 103) at coordinates (10, 11, 12):
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x = 10 y = 11 z = 12 blockID = 103 mc.setBlock(x, y, z, blockID)
Placing blocks with setBlock() is powerful, yet can be inefficient when you want to cover large areas. When we want to place blocks across a large area we use the setBlocks() method (yes, that is a different method to setBlock(), it has an “s” at the end). The setBlocks() method places a single block type in a cuboid shape between two sets of co-ordinates.
The setBlocks() method takes seven arguments: two sets of co-ordinates and a block ID. The basic syntax of setBlocks() is:
setBlocks(x1, y1, z1, x2, y2, z2, blockID)
For example the following code will place a cuboid of melon blocks (block ID 103) between coordinates (6, 5, 18) and (12, 10, 32):
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x1 = 6 y1 = 5 z1 = 18 x2 = 12 y2 = 10 z2 = 32 blockID = 103 mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID)
Both the setBlock() and setBlocks() methods can take an optional extra argument. This argument can be described as the block state. For example the wool block (block id 35) has 16 different states for 16 different colours. The red wool block for example has a state of 14. The TNT block is explosive in state 1. To use this we just place the extra argument at the end of the arguments in both setBlock() and setBlocks():
mc.setBlock(x, y, z, blockID, blockState) mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID, blockState)
The following code uses the optional argument to create an explosive TNT block (block ID 46 and state 1):
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x = 10 y = 11 z = 12 blockID = 46 blockState = 1 mc.setBlock(x, y, z, blockID, blockState)
The next example uses the optional argument with setBlocks() to create a cuboid of red wool (block ID 35 state 14):
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x1 = 6 y1 = 5 z1 = 18 x2 = 12 y2 = 10 z2 = 32 blockID = 35 blockState = 14 mc.setBlocks(x1, y1, z1, x2, y2, z2, blockID, blockState)
Have fun using these methods, they will save you loads of time when you’re building in Minecraft Pi. If you want to practice these methods while creating some useful programs check out my free book.