Chapter 13

Combine Minecraft and Python

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?

image

Get Started with Minecraft

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:

  1. Launch the desktop with startx if it isn’t already running.
  2. 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.

  3. 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.

warning The Pi edition of Minecraft is very simple. It doesn’t have most of the features of the full version, which include hell areas, dangerous chickens, witches, ocelots, slime, and flying pigs. For more on Minecraft’s features, see http://minecraft.net.

Explore the World

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

Change the view

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.

tip To change the view, press Esc and click the second button at the top left of the window. When the icon changes to a rectangle with lines leading to a pair of blocks, click Back to Game.

Now the camera is behind your player. Some people prefer this view because it’s easier to see what’s happening.

Change the world

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.

warning It’s hard to aim in Minecraft. You have to practice to work out which block your sword is going to hit.

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.

Understand APIs

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:

  • Make useful things happen automatically. For example, you can use the Twitter API to send tweets automatically at set times. You don’t have to be anywhere near a computer or phone to Tweet!
  • Collect information. For example, you can use the API to ask Twitter once a day how many followers you have and then draw a graph of the numbers.
  • Add smart new features. Do you want to follow everyone who posts a word you’re interested in, like the name of a band or a hot news topic? You can use the API to make it happen.

Understand the Minecraft API

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:

  • Find out where your player is.
  • Teleport your player to a new location.
  • Build complicated shapes with blocks.
  • Remove blocks with code — maybe to clear a big area.

Look at the Minecraft API

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.

tip Never try to learn all of an API before using it. You’d need a brain the size of a planet — which would make it hard to get out of bed in the morning — and a perfect memory. It’s fine to keep looking stuff up. If you use an API a lot, you often learn the most useful calls without really trying.

Use the Minecraft API

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.

technicalstuff The API works by sending special messages to Minecraft — a bit like emails or texts. APIs are good at hiding details. They make it easy to concentrate on what you want to happen without worrying about technical details.

Use an API call

Here’s a simple example showing how to use an API call:

  1. Choose Menu⇒Programming⇒Python 2 to launch the Python 2 editor.
  2. Choose File⇒New Window to make a new file.
  3. 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?

  4. Save the file as wherami.py.
  5. Launch Minecraft if it’s not already running.
  6. Move around inside the game for a bit.
  7. Then click the code window in the Python editor again and press F5 to run the code.
  8. 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

warning There’s no big arrow pointing north in Minecraft, so x and z are just for guidance. Even so, north is always in the same direction. It’s not usually the direction you’re facing.

Teleport in Minecraft

You can use a different API call to move around in Minecraft:

  1. 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.

  2. 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.

  3. 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.

tip You don’t die when you crash. Your player is made of very tough blocks.

Remove blocks

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.

tip The 0 at the end of the setBlock() call makes the block an air block. Minecraft has lots of different block types, and they all have a different number. If you scroll through the API reference, you can find a list of block types, with the number of each.

Make buildings

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.

Try a Few More Things

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.

  • Remove blocks as you walk. This project is harder than it looks. How many layers of blocks do you need to get rid of before you can walk through the hole you make? How wide does the hole need to be? The easy option is to use lots of separate calls to setBlock(). Is there a neater way to make the code work with loops or conditionals?
  • Build bigger shapes. Cubes and the like are easy. You can use a special API call for big square shapes with lots of blocks. But how about making a square, or a pyramid, or a grid of lines? If you have high school math, try making circles or spheres. Or try making a house.
  • Build a rocket. Take the fountain/tower and make it fly into the sky. How do you keep the blocks together? Is there a simpler way to do it?
  • Make a firework. At a certain height — say, 100 blocks — make all the blocks in the rocket explode in different directions. How can you keep track of the position of every block?
  • Build a maze. This project is really hard to work out on your own, unless the maze is very simple. For help, see 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.155.49