I rediscovered my Raspberry Pi, the credit card-sized computer intended to make programming accessible to kids (and everyone), this weekend. After loading an SD card with a fresh copy of NOOBS (here), I was ready to go.
News to me, a free version of Minecraft is included in the NOOBS build, so I tried it out. Not surprisingly in my old age, I was unable to figure out the point of Minecraft without Googling a tutorial, which led me to discover that there is also a Minecraft Python API packaged in the Raspberry Pi, and here’s how I used it to insert a 3×3 pillar of new blocks below my player.
- With a fresh world open in Minecraft (above), open Python IDLE 2.x.
- Import the Minecraft Python library, which should have been installed with NOOBS.
from mcpi import minecraft
- Create a connection to the currently open Minecraft game
mc = minecraft.Minecraft.create()
- Get the player’s current position
x,y,z = mc.player.getPos()
- Loop through the nine adjacent pixels (along the x and z directions; y is vertical)
for x1 in range (-1,2,1): for z1 in range (-1,2,1):
- Get the height of the adjacent pixels – they may not all be at the same height as the player’s position
h = mc.getHeight(x+x1,z+z1)
- Set each of the adjacent pixels to a new block type. Block type IDs can be found in the API documentation. Below I set the new blocks to 3, which is dirt.
- Continuing this pattern while moving the player upward results in a 3×3 pillar of blocks, as below.
The entire script above would look like so:
from mcpi import minecraft mc = minecraft.Minecraft.create() x,y,z = mc.player.getPos() for x1 in range (-1,2,1): for z1 in range (-1,2,1): h = mc.getHeight(x+x1,z+z1) mc.setBlock(x+x1,h,z+z1,3)