Hardware Reference
In-Depth Information
2. In the new text ile, irst import the modules you will need in this program by
typing the following code:
import mcpi.minecraft as minecraft
import time
mc = minecraft.Minecraft.create()
time.sleep(1)
pos = mc.player.getPos()
In the last line, you use the command getPos or get position of your Minecraft
player. Next, you want to display that information in the Minecraft chat window
so you can see it in the game. To do that, type the following code:
mc.postToChat(“You are located x=” +str(pos.x) + “, y=” ;
+str(pos.y) +”, z=” +str(pos.z))
pos.x will give the x coordinate, pos.y will give the y coordinate, and pos.z
will give the z coordinate.
3. Press CTRL+X on the keyboard to exit the nano text editor, ensuring that you
press y (for yes) and Enter to save your code.
4. Run the program by typing the following command:
python location.py
he coordinates for the player's location are displayed in the chat window (see
Figure 6-5).
DIGGING INTO THE CODE
postToChat requires a string, and pos.x is a number. str() converts the
number into a string, and Python allows you to add different strings together to
make one long string.
Changing the Player's Location
Now that you can easily detect your player's position within Minecraft, why not change
her location? Amend location.py by typing nano location.py to open the ile and
adding the following lines of code at the end of your program:
time.sleep(2)
mc.postToChat(“Get ready to fall from the sky!”)
time.sleep(5)
mc.player.setPos(pos.x, pos.y + 60, pos.z)
Search WWH ::




Custom Search