Programming Minecraft on the RaspberryPi
Minecraft is a sort of digital Lego. Players explore a blocky computer generated world and have the ability to build new terrain, buildings and other features. Many users have generated creative and complicated structures within their games by carefully placing individual bricks (just like Lego). A quick Google image search for Minecraft will return lots of amazing results.
The RaspberryPi is a cheap computer designed to encourage kids (of all ages) to learn how to program. Apparently, the "pi" part of the name is a misspelled reference to the Python programming language - the RaspberryPi's principal programming language for education.
Minecraft is amazingly popular among kids (my own included). I guess, like Lego, it's the potential for creative generative play that is so appealing. Wouldn't it be great if there were a version of Minecraft that you could program in Python that ran on the RaspberryPi..?
I'm currently at PyconUS 2013 in California, every attendee has been given a RaspberryPi and there's a RaspberryPi lab in which attendees are encouraged to try their new piece of kit. I just spent a fun morning programming Minecraft with Python. What follows is a quick "how to" guide to get you started.
I'll assume you've got a RaspberryPi to hand and that it's connected to the internet. (If you're unsure how to do this I suggest you visit the main RaspberryPi website for more information.)
First you should download the RaspberryPi version of Minecraft and follow the instructions at the web page to which I just linked.
Next, once you have Minecraft running on your RaspberryPi I suggest you play for a while. Create a world and have a look around. Here's a run-down of the commands you can use:
- Keyboard
- W,A,S,D - Move (or navigate inventory)
- SPACE - Jump, double tap to start/stop flying, hold to fly higher
- SHIFT - Sneak, hold to fly lower
- E - Open inventory
- 1-8 - Select inventory slot item to use
- ESC - Show/hide menu
- TAB - Release mouse without showing menu
- ENTER - Confirm menu selection
- Mouse
- Steer - Look/turn around
- Left mouse button - Remove block (hold)
- Right mouse button - Place block, hit block with sword
- Mouse wheel - Select inventory slot item to use
Cool huh..?
But wouldn't it be fun to be able to program and automate the manipulation of the game world with Python..?
Here's how to get started...
Open a terminal and change in to the Minecraft directory
(mcpi
) within which you'll see a child directory called
api
within which is yet another called python
. From
within the python
directory start Python by typing,
python
.
Now, type in the following:
>>> from mcpi import minecraft
>>> mc = minecraft.Minecraft.create("127.0.0.1")
>>> mc.postToChat("Hello, World!")
You should see the message Hello, World! pop up in the Minecraft world. Congratulations, you've just programmed Minecraft!
The first command imported all the things that Python needs to talk to the
Minecraft game. The second line creates an object called mc
that
represents a connection to your game. The third line sends the chat message
to the game.
How about building things within the Minecraft world..? Check this out...
>>> from mcpi import block
>>> mc.player.getTilePos()
Vec3(37,0,-39)
>>> mc.setBlock(38, 0, -38, block.STONE_BRICK.id)
On the first line I import information Python needs about the blocks that
you can place within the Minecraft world. Next, I ask the game where in the
world my player is located. The game responds with an x, y
and z based location expressed as a vector. Finally, I use the
setBlock
method to place a stone brick on an adjacent square.
If you take a look around you'll see it right next to you!
Actually, you will see different results to me because your player will be stood in a different location. See if you can change the final line to place a block close to your own player's location.
That's it (for now) and covers pretty much all I found out in the first 15
minutes of hacking about. If you want to explore the API further I suggest you
look at the mcpi_protocol_spec.txt
file in the
mcpi/api/spec
directory.
Personally, I think this is a gift horse that teachers everywhere should be looking at in the mouth.
I'll blog some more about this when I get back to the UK.