Minecraft Pi Edition: how to program Minecraft on a Raspberry Pi
By Russell Barnes. Posted

Get off to a good start with Minecraft Pi Edition. Play the game and write your first program using the API
Advertisement
Introducing the smarter desktop: use cloud apps, thin clients, and get real work done on a Raspberry Pi. Also in the magazine we’re building a digital jukebox, controlling a robot arm, storing…
If you’ve never played Minecraft Pi, and want to learn how to program Minecraft using a Raspberry Pi, then this is how to be a master block builder.
We’ll help you get stuck into Minecraft on the Rasbperry Pi, build a house, and get started with the API.
Minecraft is a game which has achieved monumental success; more than 120 million copies have been sold across all its versions. Not bad for a game which doesn’t really have a point! If it does have a point, as an indie sandbox game, it’s to make stuff. And people have really made stuff, from fully functioning computers to scale models of the Starship Enterprise.
The best things about Minecraft Pi Edition are that it’s free, and that it comes with an API. You don’t get this with any other version of Minecraft.
Minecraft is installed by default on Raspbian. If you have an older version, you can get it by opening a Terminal (Menu > Accessories > Terminal) and typing these commands, pressing ENTER after each:
sudo apt-get update sudo apt-get install minecraft-pi
Playing Minecraft Pi
Click Menu > Games > Minecraft: Pi Edition to run the game. Minecraft: Pi Edition offers one playing mode, Classic, which is all about exploring and building. Click Start Game, then click Create New (or choose an existing world) to enter a world.
- The mouse changes where you look
- Holding the left button destroys blocks
- Right button places blocks
- W, S, A, D move you forward, backward, left, and right
- 1, 2, 3, 4, 5, 6, 7, 8 change what you are holding
- E opens the inventory
- ESC takes you back and to the Menu
- SPACE is jump. Double-tapping it makes you fly or stop flying
- The Minecraft Pi API
The API (application programming interface Minecraft Pi) allows you to
write programs which control, alter and interact Minecraft Pi with the
Minecraft world, unlocking a whole load of Minecraft Minecraft Pi
hacking. How about creating massive houses at the click of a button; writing a game which uses Minecraft Pi a LED and buzzer to help you
find a block; or recreating Nintendo’s Minecraft Pi Splatoon in
Minecraft?
The API works by changing the world Minecraft Pi as the game is being played, allowing you to:
- Get the player’s position
- Change (or set) the player’s position
- Discover the type of block at a specific location
- Change a block
- Change the camera angle
- Post messages to the player
Minecraft Pi: Hello World
The first program all programmers create when learning something new is called Hello World Minecraft Pi, which puts the phrase ‘Hello World’ on
the screen. You’re going to do the same Minecraft Pi, but in Minecraft:
- Go to the Minecraft menu with ESC, but leave Minecraft Pi the game running (you can minimise Minecraft Pi it).
- Open IDLE by clicking Minecraft Pi Menu > Programming > Python
3 (IDLE). - Use File > New File to create a new program and save it as hellominecraftworld.py.
- At the top of your program, type the following code to import the minecraft module, which will allow you to use the API and talk to the game:
import mcpi.minecraft as minecraft - On the next line, create a connection from your program to Minecraft and call it mc:
mc = minecraft.Minecraft.create() - Use your Minecraft connection and the function postToChat() to put a message in the chat window on a third line:
mc.postToChat(“Hello Minecraft World”) - Run your program by clicking Run > Run Module.
Switch back to Minecraft, click ‘Back to game’, and you should see the message ‘Hello Minecraft World’ on the screen. Be quick, though, as the message only stays on the screen for ten seconds.
Any errors will appear in red text in the Python Shell window. Check your code carefully for spelling mistakes, and ensure that you have used the right upper- or lower-case letters.
When you have successfully made the message appear on the screen, try changing it and running the program again.
Teleportation in Minecraft Pi
Using your new Python programming skills and the Minecraft API, you can teleport Steve around the world by adding just one more line of code to your program.
Minecraft is a world of blocks, all about 1m × 1m × 1m. The player and every block in the world has a position made up of x, y, and z: x and z are the horizontal positions and y is the vertical. By changing the player’s x, y, and z position, you can teleport them wherever you want.
The player starts at position x = 0, y = 0, z = 0, which is the spawn point, and the player’s current position is shown at the top left of the screen.
Add the following code to your ‘Hello Minecraft World’ program to teleport the player to position x = 0, y = 50, z = 0, which will put your player 50 blocks up in the air:
- Teleport the player by setting their position:
mc.player.setPos(0, 50, 0) - Run your program by clicking Run > Run Module.
- Quickly switch back to Minecraft to see your player fall to the floor (unless in flying mode).
Try changing the values in setPos() to teleport your player to different places around the world. Use values -125 to 125 for x and z and -64 to 64 for y, otherwise the player will be teleported outside the world.

Russell runs Raspberry Pi Press, which includes The MagPi, Hello World, HackSpace magazine, and book projects. He’s a massive sci-fi bore.
Subscribe to Raspberry Pi Official Magazine
Save up to 37% off the cover price and get a FREE Raspberry Pi Pico 2 W with a subscription to Raspberry Pi Official Magazine.
More articles

Picocomputer 6502
The Picocomputer 6502 is truly open-source hardware: just download the design files from GitHub, send them to the PCB manufacturing service of your choice, slot in the components (which are all common and available from, among others, Mouser), and you’ve got a machine that will bring a tear to the eye of anyone who played […]
Read more →

CrowPi 3 review
An electronics playground with added AI lessons for Raspberry Pi, Pico, and more.
Read more →

Network mapper
It’s not as efficient as reading a table of figures, but we love this display anyway.
Read more →
Sign up to the newsletter
Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.