Minecraft Turtle Coded in Python

In this exercise you will create a program to draw lines on the Minecraft ground using Steve as the Turtle pen. As he walks around he will leave a trail of blocks behind him.

Open Geany and start a new file, Save As “minecraft_turtle.py” in the Code_Club directory as usual.

The first lines are the standard ones to get python to talk to Minecraft and understand the game.

from mcpi import minecraft

from mcpi import block

import time

# this next line creates the link into the game

mc=minecraft.Minecraft.create()

We now get the players position so that we can set up the camera to observe from above.

me=mc.player.getTilePos()

So that we can see what is happening lets clear the ground by placing grass down as the canvas to draw on and also erase anything above the ground by placing air. Your canvas could be a different block type if you prefer. Just change the 2 in the first setBlock line to a different number.

# clear a canvas to draw on

mc.setBlocks(me.x-50,me.y-1,me.z-50,me.z+50,me.y-1,me.z+50,2)

mc.setBlocks(me.x-50,me.y,me.z-50,me.z+50,me.y+20,me.z+50,0)

Now to fix the camera above the player. 22 blocks added to the players height is the maximum distance up that you can go before the fog starts to set in.

# fix the camera 22 blocks above player

mc.camera.setFixed()

mc.camera.setPos(me.x, me.y+22, me.z)

Now we need to continually check where the player is and place the blocks.

while True:

The next lines are indented in by four spaces or by pressing tab. This is important and it is how python knows that the commands are part of the while True.

The first line rechecks the player position. The second places a block at this position below the player’s feet (that is the me.y-1 bit). The 57 is a diamond block but you can choose something else if you like. The time.sleep just slows the program down a little.

me=mc.player.getPos()

mc.setBlock(me.x, me.y-1, me.z, 57)

time.sleep(0.1)

Now save the file again. Start Minecraft and open a world. Press Tab to leave the game so that you can now start the program.

Open an LXTerminal by clicking on the link on the tab bar at the top of the screen. Change to the Code_Club directory and start your program.

cd Code_Club

python minecraft_turtle

The view in Minecraft should change to above the player and the ground should level out. Click on the game with the mouse and use the game controls to move the player around. He should now start writing a line on the ground.

Minecraft turtle

Leave a Reply

Your email address will not be published. Required fields are marked *