1 year ago

Minecraft Pi Edition: how to program Minecraft on a Raspberry Pi

Get off to a good start with Minecraft Pi Edition. Play the game and write your first program using the API

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:

Playing Minecraft Pi

Minecraft API

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:

  1. Go to the Minecraft menu with ESC, but leave Minecraft Pi the game running (you can minimise Minecraft Pi it).
  2. Open IDLE by clicking Minecraft Pi Menu > Programming > Python
    3 (IDLE).
  3. Use File > New File to create a new program and save it as hellominecraftworld.py.
  4. 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
  5. On the next line, create a connection from your program to Minecraft and call it mc:
    mc = minecraft.Minecraft.create()
  6. 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”)
  7. 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.

Minecraft Pi (x, y, z) coordinates

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:

  1. Teleport the player by setting their position:
    mc.player.setPos(0, 50, 0)
  2. Run your program by clicking Run > Run Module.
  3. 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.