Meeting 2020-02-04

Player Position and Comments

In the last couple weeks we talked about Python variable and math. The variable types we talked about were pretty simple - just a single number or text value - but they can get much more complicated in Python. Many of the programs we'd like to do in Minecraft will need to find the player's location, and the Python Minecraft code library we are using returns the coordinates using a more complicated data type that combines the X, Y, and Z coordinates into a single variable. We won't learn how to create our own data types for a while, but using them is easy enough and is something we need to learn now so we can write some fun programs.

Getting the Player Position

Once we have given ourselves access to the Minecraft programming code library with:

from mcpi.minecraft import Minecraft

and created a Minecraft world object with:

mc = Minecraft.create()

we can save the current XYZ location of the player into a variable with the statement:

playerPos = mc.player.getTilePos()

The variable playerPos has a user-created type called "Vec3" that is defined in the Minecraft library code we imported. It has three integer elements x, y, and z we can access using "dot notation":

playerX = playerPos.x

playerY = playerPos.y

playerZ = playerPos.z

We will learn more about dot notation later (maybe next term?) but it is used throughout Python (and other programming languages) to access information that's organized in a hierarchy. In our case, x y and z are sub-elements of the playerPos variable and the Vec3 data type.

Project: Jump Boost

Now that we know how to get the player's current position, let's use that to write a short program Python that boosts the player up into the air at their current location whenever it is run. Add the above code lines to the lowelltele.py program we wrote last time and teleport the player to their current position, except add a big number (like 64) to the playerY position. This will boost the player straight up and have them fall back to their previous position.

Save the program to %appdata%/.minecraft/mcpipy/lowelljump.py (or pick your own name) and in Minecraft enter "/py lowelljump" to run it.

If you need help, you can ask me, or download my version of the program from the bottom of this web page.

Programming Tip: Comments

When we talked about variables, we suggested you pick variable names that describe what the variable does in your program, to make it easier for someone else (or yourself in a couple months) to understand how things work. Another important way of documenting your program code is by using comments. Comments are helpful text in your program source that explain or describe parts of the code, but are ignored by the computer when running your program.

In Python, any text in your program after a # (number sign) is ignored to the end of that line, so # is the comment character. You can start a line with a # so the whole line is a comment, or you can put a # at the end of a line after your program code for a short comment on that line.

### Program to boost player up into the air

from mcpi.minecraft import Minecraft

mc = Minecraft.create() # create Minecraft object

# Get player's current position

playerPos = mc.player.getTilePos()

playerX = playerPos.x

playerY = playerPos.y # Y is height, +Y is up

playerZ = playerPos.z

From now on I will be using comments in my example programs to help explain it.

Note that putting a # character inside a string doesn't create a comment, that character just becomes part of the string.

One handy use for comments is when you want to temporarily remove code from your program for testing. Instead of deleting the lines of code you want to skip, just put a comment character in from of them. They will be ignored when the program runs, and you don't have to remember what you had typed and recreate it once you have finished your debugging.

Python also has a block comment symbol """ (three quote characters). All the program lines after a """ are treated as comments, until the next line with a """. This can be useful when you have long multi-line comments and you don't want to have to add a # at the beginning of each line, and it can also be used to "comment out" a bunch of program code lines the same way.