Raspberry Pi, Minecraft, & Python

mc3

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.

mc1

  • 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.
        mc.setBlock(x+x1,h,z+z1,3)
  • Continuing this pattern while moving the player upward results in a 3×3 pillar of blocks, as below.

mc2

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)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s