Using Minecraft Pi Edition (Advanced)

This task will describe how to:

  • Download Minecraft Pi Edition
  • Install Minecraft
  • Create a new Minecraft world
  • Use the Minecraft API

Getting ready

You will need:

  • A Raspberry Pi
  • An SD card with the official Raspberry Pi OS, Raspbian, properly loaded
  • A USB keyboard
  • A USB mouse
  • A 5V 1A power supply with Micro-USB connector
  • A network connection
  • And a screen hooked up to your Raspberry Pi

How to do it...

For installing and running Minecraft, perform the following steps:

  1. Minecraft needs to be run from the GUI environment, so at the command line, type startx.
  2. From the desktop, double-click on LXTerminal.
  3. Download Minecraft by typing wget https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz into the terminal and pressing Enter.
  4. Uncompress the file by running tar zxfv minecraft-pi-0.1.1.tar.gz.
  5. Then change into that directory by typing cd mcpi.
  6. To run Minecraft type ./minecraft-pi.
    How to do it...

For creating a new Minecraft world, perform the following steps:

  1. With Minecraft running, click on Start Game.
  2. Select Create New.
  3. After a few moments, you will appear in a brand new Minecraft world environment.

For using the Minecraft Pi Edition Application Programming Interface (API), perform the following steps:

  1. In the terminal, you should still be in the mcpi folder.
  2. Change into the Minecraft Python API folder by typing cd api/python/mcpi /.
  3. Start the Python interactive shell by running python on the terminal.
  4. You will see >>>, which is where you will enter Python commands.
  5. Enter the following commands:
    • import minecraft
    • import block
    • mc = minecraft.Minecraft.create()
    • mc.postToChat("I am using the API!")
  6. You should now see the I am using the API message appear in your Minecraft window.
  7. Enter the following commands:
    • player = mc.player.getPos()
    • player
  8. A printout similar to Vec3(46.0,1.0,-14.0) should appear.
  9. Enter the command mc.setBlock(player.x +1, player.y, player.z, block.GOLD_BLOCK).
  10. Directly in front of you a golden block should appear.
    How to do it...

How it works...

Minecraft is a sandbox game from Studio Mojang. The goal of the game is to create, survive, and have fun. The game is only finished when you say so. The Raspberry Pi version of Minecraft is built on the Pocket Edition engine, which was used to power the Android and iOS version. Because of this, the Raspberry Pi edition can sometimes connect to worlds hosted by cell phones and tablets.

The controls are:

  • Arrows keys to move
  • Mouse movement to look around
  • Left-click to attack or dig
  • Right-click to place a block or eat
  • E to open inventory
  • Number keys to select item from main inventory slots
  • Space bar to jump (double jump to fly)

The biggest change that comes with the Raspberry Pi edition is the inclusion of an API. The API allows you to connect to your world with various programming languages, and modify the environment through various commands.

The first step needed to begin using the API is to go into the directory where the Python API files are stored. If you want to make your own programs, you would copy these files to your own project. When you run Python, you are brought into the interactive shell. The import command loads the file, otherwise known as a module, into memory and makes its functions available for you to use. Next, load the minecraft and block modules that will allow you to connect to your running Minecraft game and manipulate the blocks.

Running mc = minecraft.Minecraft.create() forms the connection to your Minecraft game and keeps the connection in the variable mc. When you want to send a command to your game, you will make use of the mc variable, because it's storing all the needed information. The postToChat() function sends a line of text to your game's screen. player.getPos() gets the current X, Y, and Z coordinates of your character in the game. We then store that into the variable player. By typing the variable name into the shell, we can see the current value that it is been stored. In this case, it shows the saved coordinates.

Now that you have the player's coordinates, you can manipulate the world around it. By running mc.setBlock(1,1,1, block.GOLD_BLOCK), a gold block will appear at the coordinates of (1, 1, 1). But let's say you wanted that gold block to appear right in front of your character. You can use the player variable, set earlier, to retrieve its coordinates. By entering mc.setBlock(player.x +1, player.y, player.z, block.GOLD_BLOCK) in the command line, you are telling the game to place that gold block at the spot in front of the character. If you didn't add 1 to player.x, the block would end up in the exact same spot you were in, so move it forward by one to avoid that.

The previous code only scratches the surface of what is available in the API.

There's more...

A few useful reference links:

..................Content has been hidden....................

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