MINECRAFT IS A computer game that allows you to build any computer world you like, by using virtual building blocks (see Figure 6-1). You can let your imagination run riot—there are no limits! The game was created by Markus Persson (who also goes by the gamer tag Notch). Players collect (or mine) blocks from the world around them, using nothing but a trusty axe, while avoiding monsters who might be set on eliminating them. You can learn more about Minecraft and register to play an online demo version at https://minecraft.net.
Minecraft Pi is pre-installed on the Raspbian operating system. You can run it by using the menu system and navigating to Games and selecting Minecraft Pi, as shown in Figure 6-2. It is similar to the Minecraft Pocket Edition. If your version of Raspbian does not include Minecraft Pi, if you are running an older version for example, you will need to follow these steps to download and install it onto your Raspberry Pi.
cd ~
wget https://s3.amazonaws.com/assets.minecraft.net/pi
/minecraft-pi-0.1.1.tar.gz --no-check-certificate
It will take a few seconds, depending on your Internet connection speed, for the download to start. A progress bar will appear in the terminal window for you to check how long the download will take, as shown in Figure 6-3. When it has reached 100 percent you can move on to Step 3.
tar -zxvf minecraft-pi-0.1.1.tar.gz
This command will extract the Minecraft Pi files into your home directory on the Raspberry Pi. You will only need to do this once—to load Minecraft Pi in future, you only need to use Step 4.
minecraft-pi
Run the command minecraft-pi to start the game. You’ll see a screen similar to Figure 6-1.
Clicking Start Game displays a list of Minecraft worlds that you can join. On your first go, however, this list will be empty. Click Create New to generate a Minecraft world in build mode. Minecraft has two modes, survival and build. In build mode, you are able to construct objects without having to avoid monsters who may end your life. Play around a bit and familiarize yourself with the controls for playing Minecraft in build mode, shown in Table 6-1.
Table 6-1 Minecraft Pi Controls |
|
Key press/mouse movement |
Action |
W |
Moves player forward. |
A |
Moves player left. |
S |
Moves player back. |
D |
Moves player right. |
Spacebar |
Makes player jump. Tap the spacebar twice to make the player fly. |
Tab |
Releases the mouse so you can click on other windows. |
Esc |
Returns you to the menu. |
Move the mouse |
Allows you to see around the player and point the player in a particular direction. |
Left mouse click |
Breaks the blocks around you. |
Right mouse click |
Places a block. |
Being able to play Minecraft is one fun aspect of Minecraft Pi, but what is more exciting is being able to use Python code to manipulate the Minecraft environment. Time to dive in.
Now that you have checked that Minecraft Pi is installed on your Raspberry Pi by running the game, it’s time to see what adventures you can have with code in the Minecraft world. In this project, you will run a Minecraft game and write a Python script from the command line in LXTerminal to test that your connection to the game works. You’ll do this by displaying a message in the game.
minecraft-pi
nano testmcpi.py
This will open a text editor from the command line.
import mcpi.minecraft as minecraft
As in previous Python programs you have created, here you are importing a module that you will need in your program—in this case, the minecraft module. Now type the following command (being sure to use the correct capitalization):
mc = minecraft.Minecraft.create()
This line connects your program to Minecraft and allows you to start interacting with it. Remember, you must have Minecraft running and be in a game for your program to work.
msg = "I am starting my Minecraft Pi Adventures"
Then type the following line to post your message to the Minecraft chat window:
mc.postToChat(msg)
python testmcpi.py
You will see your message displayed in the Minecraft game window open on your screen, as shown in Figure 6-4.
You can see how easy it is to make something interesting happen in the Minecraft game environment using Python code. Minecraft gives the appearance of three dimensions. To achieve this, Minecraft Pi uses x, y and z coordinates to generate a 3D environment, with x representing forward and back, y representing up and down and z representing left and right (see Figure 6-5).
To understand coordinates within Minecraft, in this part of the adventure you will find out the current coordinates of your player and then transport her to a different location by changing the coordinates. Begin by writing a program to locate your player.
nano location.py
import mcpi.minecraft as minecraft
import time
mc = minecraft.Minecraft.create()
time.sleep(1)
pos = mc.player.getPos()
In the last line, you use the command getPos or get position of your Minecraft player. Next, you want to display that information in the Minecraft chat window so you can see it in the game. To do that, type the following code:
mc.postToChat("You are located x=" +str(pos.x) + ", y="
+str(pos.y) +", z=" +str(pos.z))
pos.x will give the x coordinate, pos.y will give the y coordinate, and pos.z will give the z coordinate.
python location.py
The coordinates for the player’s location are displayed in the chat window (see Figure 6-6).
Now that you can easily detect your player’s position within Minecraft, why not change her location? Amend location.py by typing nano location.py to open the file and adding the following lines of code at the end of your program:
time.sleep(2)
mc.postToChat("Get ready to fall from the sky!")
time.sleep(5)
mc.player.setPos(pos.x, pos.y + 60, pos.z)
When you run this code, the player will suddenly change position. The last line of code—mc.player.setPos(pos.x, pos.y + 60, pos.z)—adds 60 only to the y axis (up and down). The result is that the player is suddenly transported from her current position to the middle of the sky—and because the player has nothing to stand on, she starts to fall! In the next section, you’ll discover how to give your player something to stand on.
Traditionally, Minecraft is played by building structures such as shelters, homes and other buildings. In fact, you can build whole cities if you are good at the game and have spent lots of time farming and creating the different levels of material blocks. However, if you use a version of Minecraft that you can manipulate with code, you don’t need to spend hours building a structure; you can simply program it to happen.
nano placeblock.py
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
mc.setBlock(1, 10, 1, block.STONE)
The three numbers in the last line refer to the x, y and z coordinates (see Figure 6-5). This is followed by the block type you wish to use—STONE in this example (Figure 6-7). The result in Minecraft Pi will be a single block of stone hanging in the sky above the player.
Placing one block at a time is still going to be time-consuming and not very helpful if you want to build a bigger structure. But with the addition of one letter, you can place more than one block at a time.
So far, with setBlock you have been using one set of coordinates to tell the game where to place a single block. The setBlocks command (note the added s at the end) works in much the same way as setBlock. You start with a set of x, y and z coordinates that indicate where in the Minecraft world you want the blocks to be placed. Then the second set of x, y and z coordinates represent the number of blocks needed to make the shape you want to create:
setBlocks(x1, y1, z1, x2, y2, z2, blocktype)
For example, to generate a cube you would type:
setBlocks(0, 0, 0, 10, 10, 10, block.MELON)
The first three numbers are the location for where the blocks should be placed. The last three are the number of blocks. So the preceding code would place 10 MELON type blocks on the x axis, 10 on the y axis and 10 on the z axis, forming a cube (see Figure 6-8).
However, if you try to use this script you may not be able to find your cube of melons, as Minecraft Pi will set the blocks from the center location (0, 0, 0) of the world. Depending on the terrain, this could be inside a mountain!
It makes more sense to place the blocks where the player is located, so that you can find a nice empty spot for your cube. To do this, you need to get the player’s position using the following code:
pos = mc.player.getTilePos()
You can then use setBlocks, adding the coordinates of the player’s location to indicate where you would like the blocks to be placed:
mc.setBlocks(pos.x, pos.y, pos.z, pos.x + 10,
pos.y + 10, pos.z + 10, block.MELON)
You may find that you end up inside this cube because the first set of coordinates is the player’s position! You could change the location coordinates in the same way:
mc.setBlocks(pos.x + 5, pos.y + 5, pos.z, pos.x + 10,
pos.y + 10, pos.z + 10, block.MELON)
Try this yourself by modifying your existing code, and see if you can get it to work.
When you are playing Minecraft in build mode, it takes a lot of time to move from one side of the world to the other. You can speed things up by creating a diamond block transporter to whisk you from location to location. To do this, you will use setBlock, getPos and setPos.
nano transporter.py
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
mc = minecraft.Minecraft.create()
mc.postToChat("A Transporter Adventure")
time.sleep(5)
The time delays are important in this code, so that you can move the player around the Minecraft world before the transporter blocks are placed.
transporter1 = mc.player.getTilePos()
mc.setBlock(transporter1.x, transporter1.y - 1,
transporter1.z, block.DIAMOND_BLOCK)
mc.postToChat("1st Transporter created")
time.sleep(2)
mc.postToChat("Find another location in 30 seconds")
time.sleep(30)
transporter2 = mc.player.getTilePos()
mc.setBlock(transporter2.x, transporter2.y -1,
transporter2.z, block.DIAMOND_BLOCK)
mc.postToChat("2nd Transporter created")
time.sleep(2)
while True:
time.sleep(1)
pos = mc.player.getTilePos()
if(pos.x == transporter1.x) and (pos.y ==
transporter1.y) and (pos.z == transporter1.z):
mc.player.setPos(transporter2.x, transporter2.y,
transporter2.z)
if(pos.x == transporter2.x) and (pos.y ==
transporter2.y) and (pos.z == transporter2.z):
mc.player.setPos(transporter1.x, transporter1.y,
transporter1.z)
Figure 6-9 shows the code, and Figure 6-10 shows the diamond transporter in position.
When the Minecraft community meets the Raspberry Pi community, good things happen. Many people enjoy sharing the programs they have made for Minecraft Pi so that you can clone them or make copies of them to use on your own Raspberry Pi. You can find shared Minecraft Pi programs on the Minecraft Pi forum (www.minecraftforum.net/forum/216-minecraft-pi-edition) or the Raspberry Pi forum (www.raspberrypi.org). Many programmers who share their code use an online repository such as GitHub (https://github.com) so that you can easily download their code, try it out, and help improve it. Why not have a go at downloading Martin O’Hanlon’s Minecraft Cannon program? This program places a cannon where your player is positioned in Minecraft Pi. You use the LXTerminal command line to move the cannon up or down before firing blocks. Check out the video at www.youtube.com/watch?v=6NHorP5VuYQ to see it in action.
sudo apt-get update
sudo apt-get install git-core
This command installs an application called git-core that lets you clone the code that Martin has placed on a repository called github.
cd ~
git clone
https://github.com/martinohanlon/minecraft-cannon.git
cd minecraft-cannon
python minecraft-cannon.py
Now you’re ready to play with the cannon! To control the cannon, shown in Figure 6-11, you can use the following commands in LXTerminal window:
Minecraft Pi allows you to be really creative. As well as finding programs created by other people, you can find online tutorials to generate things like rainbows, bridges and other games like Snake inside your Minecraft world. Here’s a list to get you started:
Minecraft Pi Command Quick Reference Table |
|
Command |
Description |
cd mcpi |
Changes directory to mcpi. |
import mcpi.minecraft as minecraft |
Imports the Minecraft modules. |
mc = minecraft.Minecraft.create() |
Connects to Minecraft Pi by creating the Minecraft object. |
./minecraft-pi |
Opens Minecraft Pi from the LXTerminal or command line. |
pos = mc.player.getPos() |
Returns the players position with floats. |
pos = mc.player.getTilePos() |
Returns the players position with integers. |
postToChat(msg) |
Posts a message to chat in Minecraft Pi. |
setBlock |
Sets a block at coordinates. |
setBlocks |
Sets blocks between two sets of coordinates. |
setPos |
Sets the position of a player. |
wget |
Used to get content, such as an archive file (.tar) from a web server where it is stored. |