15.01.2016 Views

YOUR OFFICIAL RASPBERRY PI MAGAZINE

6014BX9nm

6014BX9nm

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Feature<br />

The first step in hacking<br />

Pi is to program it in Python<br />

CODE:<br />

Teleport<br />

here are a few ways to<br />

program Minecraft on<br />

Raspberry Pi, and we’ll be<br />

talking about those in the rest of<br />

the feature. The most popular way<br />

to mod it, however, is by using the<br />

included Python library. If you’ve<br />

ever used Python, the syntax and<br />

logic of the code remains the same,<br />

only now there are functions that<br />

link directly into Minecraft!<br />

As we’re programming in<br />

Python, we can also connect<br />

Minecraft to the real world via<br />

the G<strong>PI</strong>O pins or any attached<br />

hardware that works with Python,<br />

such as the Pi Camera or Sense<br />

HAT. Here’s how to get started...<br />

USAGE:<br />

.setPos(x,y,z)<br />

EXAMPLE:<br />

mc.player.<br />

setPos(0.0,<br />

0.0, 0.0) will<br />

teleport the<br />

player to the<br />

centre of the map<br />

CODE:<br />

Block type<br />

USAGE:<br />

getBlock(x,y,z)<br />

EXAMPLE:<br />

block = mc.<br />

getBlock(0,0,0)<br />

sets variable block<br />

as the type of block<br />

at the centre of<br />

the map<br />

mc.player.setPos(10.0,12.0,5.0)<br />

mc.player.setPos(10.0,12.0,-5.0)<br />

Using the same coordinate system of X, Y<br />

and Z from getPos(), you can move the<br />

player around the map as you see fit. You’ll<br />

have to figure out the coordinates you want<br />

to move to via testing, or by using getPos()<br />

at the desired spot.<br />

print mc.getBlock(10.0,3.0,-5.0)<br />

3<br />

print mc.getBlack(5,-1,7)<br />

12<br />

Each block type has a name and<br />

identification number for it; for example,<br />

AIR has the ID of 0. Using the getBlock<br />

function, you can figure out what kind of<br />

block is at certain coordinates: this is useful<br />

for figuring out where a player is in your<br />

code if you need to trigger a certain event.<br />

CODE:<br />

Location<br />

USAGE:<br />

getPos()<br />

from mcpi.minecraft import Minecraft<br />

mc = Minecraft.create()<br />

print mc.player.getPos()<br />

Vec3(5.67561,0.0,6.55693)<br />

CODE:<br />

Create a block<br />

USAGE:<br />

.setBlock(x,y,z)<br />

mc.setBlock(10.0,0.0,-1.0, block.<br />

OBSIDIAN.id)<br />

mc.setBlock(10.0,0.0,-2.0, block.<br />

OBSIDIAN.id)<br />

mc.setBlock(9.0,2.0,-2.0, block.ICE.id)<br />

EXAMPLE:<br />

position =<br />

mc.player.<br />

getPos() will give<br />

a list of coordinates<br />

for the player’s<br />

current position<br />

This returns the player’s position in the world<br />

as three numbers. The first number on the<br />

X-axis indicates how far forward or back<br />

the player is from the centre. The second<br />

number is how far left or right they are, or<br />

the Y-axis. The last number, the Z-axis, is the<br />

height in blocks from ground level.<br />

EXAMPLE:<br />

mc.setBlock(0,0,0,<br />

block.OBSIDIAN.id)<br />

will create a block<br />

of obsidian in the<br />

centre of the map<br />

Create any kind of block wherever you want<br />

it in the world. You can see how this works in<br />

CrazySqueak’s code on the page before this,<br />

as he has created lava, water, and empty<br />

spaces as part of the Natural Disasters<br />

program. You can also create a chunk of<br />

blocks with a range of coordinates.<br />

raspberrypi.org/magpi<br />

January 2016<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!