Program Minecraft-Pi tutorial
Learn to program Minecraft on your Raspberry Pi
Minecraft is probably the biggest game on the planet right now. It’s available on just about any format you can imagine, from PCs to gaming consoles to mobile phones. It should probably come as no surprise that it’s also available on the Raspberry Pi. While at first glance Minecraft-Pi is a simplified version of the Pocket Edition (designed for tablets and smartphones), the Raspberry Pi edition is very special, in that it’s the only version of Minecraft to gives users access to its API (application programming interface).
In this project we’re going to show you how to set up Minecraft-Pi and configure it so you can interact with Minecraft in a way you’ve never done before. This small project is just the tip of the iceberg…
What you’ll need
Step 01 Requirements
Minecraft-Pi requires you to be running Raspbian on your Raspberry Pi, so if you’re not already running that, take a trip to raspberrypi.org and get it setup. It also requires you have X Window loaded too. Assuming you’re at the command prompt, you just need to type startx to reach the desktop.
Step 02 Installation
Make sure you’re already in your home folder and download the Minecraft- Pi package with the following commands in a terminal window:
$ cd ~ $ wget https://s3.amazonaws.com/ assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz
To use it we need to decompress it. Copy the following into the terminal window:
$ tar -zxvf minecraft-pi-0.1.1.tar.gz
Now you can move into the newly decompressed Minecraft-Pi directory and try running the game for the first time:
$ cd mcpi $./minecraft-pi
Step 03 Playing Minecraft-Pi
Have a look around the game. If you’re not familiar with Minecraft, you control movement with the mouse and the WASD keys. Numbers 1-8 select items in your quickbar, the space bar makes you jump and Shift makes you walk slowly (so you don’t fall off edges). ‘E’ will open your inventory and double-tapping the space bar will also toggle your ability to fly.
Step 04 Configuring the Python API
To take control of Minecraft with the Python API, you next need to copy the Python API folder from within the /mcpi folder to a new location. In the terminal, type the following:
$ cp -r ~/mcpi/api/python/mcpi ~/minecraft
In this folder, we want to create a ‘boilerplate’ Python document that connects the API to the game. Write the following into the terminal:
$ cd ~/minecraft $ nano minecraft.py
With nano open, copy the following and then save and exit with Ctrl+X, pressing Y (for yes), then Enter to return to the command prompt:
from mcpi.minecraft import Minecraft from mcpi import block from mcpi.vec3 import Vec3 mc = Minecraft.create() mc.postToChat(“Minecraft API Connected”)
Step 05 Testing your Python script
The short script you created contains everything you need to get started with hacking Minecraft-Pi in the Python language. For it to work, you need to have the game already running (and be playing). To grab control of the mouse while in-game, you can press Tab. Open a fresh terminal window, navigate into your minecraft folder and start the script with the following commands:
$ cd ~/minecraft $ python minecraft.py
You’ll see a message appear on screen to let you know the API connected properly. Now we know it works, let’s get coding!
Step 06 Hide & Seek
Follow along with our Hide & Seek code adapted from Martin O’Hanlon’s original creation. When you launch the script, you’ll be challenged to find a hidden diamond in the fastest time possible. We’ve used it to demonstrate some of the more accessible methods available in the API. But there’s much more to it than this demonstrates. Stay tuned – we’ll be back with more related guides in future issues.