Minecraft Pi API: Moving the Player

by arghbox

Using the Minecraft Pi Python API it is possible to change the position of the player in the game world. Effectively you can tell the game to teleport the player to a set of co-ordinates. There are many things you can do with this, from teleporting to your own castle, putting the player on top of a boat or trapping a friend in an unbreakable box.

There are two methods used for changing the position of the player: setPos() and setTilePos(). Both of them use x, y and z co-ordinates as arguments.

setTilePos()

Using the setTilePos() method you can change the position of the player in the game. The setTilePos() takes the co-ordinates x, y and z as arguments. These co-ordinates must be integers (in other words whole numbers). The setTilePos() method is part of the player class. This means you need to call it using dot notation like this player.setTilePos().

player.setTilePos()

The following example teleports the player to the co-ordinates (16, 1, -5):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 16
y = 1
z = -5
mc.player.setTilePos(x, y, z)

setPos()

The setPos() method is similar to the setTilePos() method in that it changes the position of the player. It also takes co-ordinates as arguments. There is a major difference as these arguments can be floats (or in other words they can have decimal places). This allows the player to be placed precisely in the game. The setPos() method is part of the player class. This means you need to call it using dot notation like this player.setPos().

player.setPos(x, y, z)

The following code will teleport the player to the co-ordinates (16.2, 1.6, -5.1):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 16.2
y = 1.6
z = -5.1
mc.player.setPos(x, y, z)