Hardware Reference
In-Depth Information
API
stands for application programming interface. An API gives you a way to
safely access parts of an application program from within your programs. It is
the Minecraft API that gives you access to the Minecraft game from within your
Python programs. The Minecraft game must be running before you can connect
your Python programs to it via this API. The
mcpi
in the line
import mcpi.
minecraft as minecraft
stands for
Minecraft Pi
, because the very first
version of the API only ran on the Raspberry Pi.
An
interface
is a set of rules that explain how, as a programmer, you can access
some other part of a computer system. The API is a set of rules explaining how
you can communicate with a running program—in this case, the Minecraft game.
All your Minecraft programs will access the running Minecraft game through the
Minecraft API.
All of the Python programs you create will need to access the Minecraft API.
For this to work properly, your programs must be able to access the
mcpi.
minecraft
module, which is stored in the
mcpi
folder of your
MyAdventures
folder. The easiest way to make sure that this always works is always to create
your Python programs inside your
MyAdventures
folder.
DIGGING INTO THE CODE
Congratulations—you have just used a variable in your program!
pos.x
is a vari-
able, and so are
pos.y
and
pos.z
.
A
variable
is just a name for a location in the computer's memory. Whenever
you put a variable name to the left of an equals sign, it stores a value in that vari-
able, for example:
a = 10
Whenever you use a variable inside a
print
statement
, the program will dis-
play the value of that variable on the Python Shell. So, if
a = 10
, the following
print(a)
will print out 10, the value of the variable
a
.
The
pos
variable that you use from
pos = mc.player.getTilePos()
is a
special type of variable. Just think of it as a box with three sub-compartments
labelled x, y and z:
print(pos.x)