Chapter 13
Minecraft is the world’s most popular block mining game. Python is one of the world’s most popular computer languages. How could they not go together?
You can find the Pi edition of Minecraft in the Games heading of the main menu, as shown in Figure 13-1.
To use Minecraft:
Click the Menu button and choose Games and Minecraft Pi.
As Figure 13-1 shows, Minecraft launches in a small window and asks you to start a new game or join a game.
Click Start Game, select world, and click Create New.
Minecraft shows a Generating world window as it makes a new world. This takes a while.
The Minecraft world is made of blocks arranged in a 3D grid. Figure 13-2 is your first view of this new world.
Drag the mouse to turn so that you can see different parts of the world. To move, press the keys listed in Table 13-1.
Table 13-1 Moving in Minecraft
Key |
New Direction |
W |
Move forward |
A |
Shuffle left without turning |
D |
Shuffle right without turning |
S |
Move back |
E |
Show the blocks window |
Esc after E |
Hide the block window |
Space |
Jump once |
Double-tap Space |
If not flying, start to fly If flying, stop and fall |
Tab |
Release the mouse so you can use it on the desktop |
Esc |
Go to the game menu |
You may find it hard to use the default first-person view, which puts the camera in front of your player’s eyes. It’s kind of clumsy, and it’s hard to see where you are.
Now the camera is behind your player. Some people prefer this view because it’s easier to see what’s happening.
Minecraft is all about changing your world. To remove a block, click the left mouse button to swing your sword. After you hit a block a few times, it breaks into pieces and disappears.
If nothing happens when you left-click, move your player until a block is in front of you.
To build something new, press E to see all the blocks and other items you can use. Click one to select it. If it’s a block and not a weapon, right-click to add it to the world. You can keep right-clicking, turning, and moving to add more and more blocks, as shown in Figure 13-3.
In computerland, an API (application program interface) is a software control panel for a website, game, or other app. Instead of clicking a mouse and pressing keys to make things happen, you can send software commands using code. APIs can also tell you what’s happening inside a website, game, or app.
APIs are everywhere. Twitter, Facebook, and other big websites have their own APIs.
As an example, here are a few things you can do with the Twitter API. This isn’t the full list, but it does give you some hints:
The Minecraft API built into the Pi version is much simpler than the Twitter API, but you can still do some cool things with it. Here are some possibilities:
All APIs have a reference website with a list of commands — also known as API calls, or just calls for short.
The Minecraft API Reference is at www.stuffaboutcode.com/p/minecraft-api-reference.html
.
As shown in Figure 13-4, API reference pages often look complicated, but often that’s because you get a list of calls and not quite enough explanation of what each call does, or how to use it, with examples.
The Minecraft API Reference has good examples, but the explanations are not very detailed. You have to do some guessing — which is normal when you’re using an API.
Another missing feature is priority. All the calls get equal space, so you have no idea which calls are used a lot and which are hardly used at all.
That’s normal, too. It’s up to you to decide which calls you want to use. It’s a good idea to look through the list and copy/paste or make a note of calls that look interesting or useful. Then you can experiment with them. If you need to know more about them, you can look online for other examples.
Most projects that use an API start with boilerplate code that sets up the API so that you can use it later. For Minecraft, the boilerplate code looks like this:
from mcpi import minecraft
mc = minecraft.Minecraft.create()
The code makes a variable called mc, which works like an invisible Minecraft control robot. When you send commands to mc, it passes them to Minecraft. Minecraft does the stuff you tell it to, or reports back with information you asked for, or both.
Here’s a simple example showing how to use an API call:
Type the following code:
from mcpi import minecraft
mc = minecraft.Minecraft.create()
x, y, z = mc.player.getPos()
print x, y, z
Can you guess what this does?
Move the player again and run the code again.
Figure 13-5 shows what you can get.
The code tells you where your player is in the Minecraft world! It uses three numbers. Table 13-2 shows you what they mean.
Table 13-2 Finding Yourself in Minecraft
Letter |
What It Means |
x |
East/west on the grid |
y |
Up/down for flying and digging |
z |
North/south on the grid |
You can use a different API call to move around in Minecraft:
Change the code so that it looks like this:
from mcpi import minecraft
mc = minecraft.Minecraft.create()
x, y, z = mc.player.getPos()
mc.player.setPos(x, y + 100, z)
The setPos() API call moves your player to a new location.
Move to another location, if you’d like.
You can do some basic math to the x, y, and z numbers to move from your current location to a new one. Or you can use some other numbers — maybe random numbers — to jump to some other place.
Save the file as jump.py and press F5 to run it.
Figure 13-6 shows an example. The code makes you jump straight up by 100 grid blocks. If you’re flying, you stay there. If you’re not flying, you crash straight back down again.
Hacking away at blocks with a sword is a really slow way to change the world. Is there a faster way?
If you look through the API, you won’t find a deleteBlock() call, but there is a setBlock() call. It turns a block into a different kind of block.
Can you work out how to make blocks disappear now? You have to use a trick. In Minecraft, the entire world is made of blocks, across, left, right, up and down, in every direction, so there are no missing blocks.
The trick? Empty blocks are made of air. You can use setBlock() to turn stone and other blocks into air blocks to make them disappear.
Here’s some code:
from mcpi import minecraft
from time import sleep
mc = minecraft.Minecraft.create()
while True:
x, y, z = mc.player.getPos()
mc.setBlock(x, y - 1, z, 0)
sleep (0.1)
The game is called Minecraft, so you can make a mine. The code removes the block your player is standing on. It loops forever, so it keeps removing blocks. The player falls into a bottomless shaft that gets deeper and deeper.
Save it as death_dig.py and run it. You may need to nudge your character into a hole to make the mineshaft appear. Eventually, you see the screen in Figure 13-7.
If you dig deep enough. Minecraft decides you’ve fallen out of the bottom of the earth and kills you.
Of course, you can also take an air block and turn it into a stone or water block. Try the following:
from mcpi import minecraft
import random
mc = minecraft.Minecraft.create()
x, y, z = mc.player.getPos()
x = x + random.randint(-10, 10)
z = z + random.randint(-10, 10)
for i in range(0, 21):
mc.setBlock(x, y + i, z, random.randint(1, 8))
Can you work out what this code does? The random.randint(a, b) calls produce a random — unpredictable — number between the first and second numbers in the brackets. For example,
random.randint(-10, 10)
makes a number between –10 and 10. You get a different number every time you run this code. It’s supposed to be unpredictable, so you never know what you’ll get.
Random code is a good way to add surprises. You can give the code an outline of something, and it fills in the details in surprising ways.
Here, the x and z positions are random so they stay close to the player, but not likely to be on top of the player.
Why does this matter? Because the rest of the code makes a simple building on the random position. It turns air blocks into other blocks.
The small 1, 8 range of randomness more or less guarantees that one of the blocks will be flowing water. Figure 13-8 shows what happens when you put everything together — you get a fountain!
Minecraft makes the water pour off the fountain onto the ground, and into the sea. Because the blocks are random, the fountain is a little different every time you run the code.
Here are some projects to try. As usual, some projects are harder than others. A few are very hard, but see how far you get with them anyway. You will need to research and learn more about Python to make some of them work.
http://en.wikipedia.org/wiki/Maze_generation_algorithm
. Try taking the Python code there and making it work in Minecraft. Make the walls so tall that your player is trapped inside them.18.190.155.49