Alright, now that we’ve made sure everything is set up correctly, let’s start some coding!
Type the code into your mc.py file as shown to the right.
On line 1 we have some green text, beginning with a “#“.
This is called a comment. Comments are ignored by Python when running the code. So why do they exist?
Comments are used as notes for both yourself and other programmers to help remember/understand how code works. They are probably of the most useful programming tools, and one of the easiest!
On line 2 we have a keyword called import and one called from (keyowrds are important built-in words of a programming language, they usually have colors to help them stand out.).
Think of their being a giant library with thousands of books. From is grabbing a book from that library and import is reading a chapter from it.
Here we are grabbing the mcpi.minecraft “book” and reading the Minecraft “chapter” of it. This will give our program the ability to use all of the code and resources from that Minecraft “chapter” or file.
Since our program now knows what Minecraft is, it will be able to code inside it!
Now that we can code in Minecraft, let add some more “Minecraft specific” code.
On line 4 we have actually quite a bit going on. Here we have a variable called “mc” and a function called “Minecraft.create”.
A function is a piece of code that has a purpose/job to do. Here its job is to create a connection to Minecraft and allow us to save that connection.
Here we save the connection into a variable called “mc”. A variable allows us to save something and use it later.
On line 5 we have a function called “mc.player.getPos()” and we are saving what it gives us into 3 variables “x, y and z”.
This function goes into Minecraft and gets the position of our player in the game, and returns it as 3 numbers, 3 coordinates, x, y and z.
The three variables, x, y and z are called coordinates and this is how many games keep track of the positions of objects and players in a game.
The X coordinate is our left/right position.
The Y coordinate is our up/down position.
The Z coordinate is our forward/back position.
We can also see these change live by pushing F3 inside Minecraft. Try doing this and moving around to see how the coordinates change!