Minecraft Pi Edition can be a very fun and engaging way to learn programming.
For beginners getting started can be the biggest hurdle. There are a number of brilliant sources that demonstrate some really cool examples of what can be achieved – check out Martin O’Hanlon’s Stuff About Code. However, it can be a bit overwhelming for most beginners to break apart the code to find out how it works and use the Minecraft Pi API for their own ideas.
In this series of posts I’ll cover the basics of using the Minecraft Pi API. This post will concentrate on setting up the API for use with Python and creating a basic program to teleport the player. Following posts will explain different components with simple examples.
First things first. We will be using the Python programming language. No idea what that is or where to learn about it? Programs are a way of giving a computer instructions. Python is one programming language that we can use to write these instructions. You will be able to follow the explanations in this series without knowledge of Python, however I’d recommend you check out Codecademy.com for excellent (and free) Python tutorials.
First off we need to install Minecraft Pi Edition on our Raspberry Pi. Follow the instructions on http://pi.minecraft.net/
Alert: the last instruction for installation is slightly wrong. You need to run this code instead to open the game:
After you’ve installed Minecraft and have familiarised yourself with the game, we can start start writing programs to interact with it.
Create a new directory to store your programs. Copy the mcpi directory located inside Python API directory into the directory you’ve just created. To achieve this in a terminal type in the following command after navigating to the directory you’ve just created:
cp -r ~/mcpi/api/python/* .
Alternatively to do this with a file browser, navigate to the above directory, copy the mcpi folder and paste it into the folder you just created. We are now ready to start programming.
Teleporting the Player
Open IDLE on your Raspberry Pi, create a new file and save it as teleport.py in the folder the folder you just created. We will write your first program in this file.
Every program that interacts with the Minecraft Pi API has the same two lines at the start. These lines connect our program to the game. Copy these two lines into your program:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
In the same file we’re going to write some code to teleport the player to a new location. Copy this code into your program after the first two lines:
x = 1
y = 24
z = 1
In IDLE click on the Run menu, then Run Module. Your character should now teleport to the coordinates (1, 24, 1). If this doesn’t work make sure you are in a Minecraft Pi game world. If you get a black screen, you’ve just teleported inside of a block – change the numbers next to x, y and z and rerun your program to fix this.
How this works: In our program we have created 3 variables x, y and z. A variable stores a piece of data, in this case a number. We then give these numbers to the setTilePos() function, which connects to your Minecraft game and tells it to move your player to the coordinates you have set as numbers in x, y and z.
Try playing around with different number values of x, y and z. Use negative numbers. See what happens if you teleport off the map (hint: you’ll either die or your game will crash). If you’ve built several amazing things in Minecraft and want a quick way to travel to them copy the code into a different a file for each and change the coordinates to match the building.
Have fun and check back in the future for more Minecraft Pi API tutorials.
If you are interested in learning more about Python programming with Minecraft Pi or using it in a classroom/club, check out my free book that is available here.