Wednesday, 21 May 2014

Minecraft Graphics Turtle

Spiral built using the Minecraft Turtle
I have had this idea of creating a 3d graphics turtle in Minecraft for a little while so one Sunday morning while I was waiting for the boy to finish a swimming lesson this is what I did.

The Minecraft Graphics Turtle allows you to use the Minecraft world as your drawing studio and unlike most graphics turtle's you aren't confined to 2d space, you can go up and down as well as left and right, and when your master piece is finished, you can walk around it!



The MinecraftTurtle is really easy to install and use, you only need a single python module (minecraftturtle.py), which needs to be copied to the same folder as the minecraft python api (minecraft.py, connection.py, block.py, etc), so if your using a Pi, its usually ~/mcpi/api/python/mcpi.

If you want to get started quickly through, I would download the complete code from my github, as it contains some examples and all the files you need to have go.

Download Minecraft Turtle Code:
sudo apt-get install git-core
cd ~
git clone https://github.com/martinohanlon/minecraft-turtle.git

Try the 'squares' example:
Startup Minecraft and load a game.
cd ~/minecraft-turtle
python example_squares.py

Create your own turtle program:
The turtle is really easy to program, Open IDLE (not IDLE3), create a new file and save it in the ~/minecraft-turtle directory.
import minecraftturtle
import minecraft
import block

#create connection to minecraft
mc = minecraft.Minecraft.create()

#get players position
pos = mc.player.getPos()

#create minecraft turtle at the players position and give it a name
steve = minecraftturtle.MinecraftTurtle(mc, pos)

#tell the turtle to go forward 15 blocks
steve.forward(15)

#tell the turtle to go right 90 degrees
steve.right(90)

#tell the turtle to go up by 45 degress
steve.up(45)

tell the turtle to go forward 25 blocks
steve.forward(25)

Run the program and you should see the turtle draw 2 lines, one straight ahead, the other at a 90 degree angle right and a 45 degree angle up.

The turtle supports lots of other commands:
#create the turtle
#  position is optional, without it gets created at 0,0,0
steve = minecraftturtle.MinecraftTurtle(mc, pos)

#rotate right, left by a number of degrees
steve.right(90)
steve.left(90)

#pitch up, down by a number of degress
steve.up(45)
steve.down(45)

#go forward, backward by a number of blocks
steve.forward(15)
steve.backward(15)

#get the turtles position
turtlePos = steve.position
print turtlePos.x
print turtlePos.y
print turtlePos.z

#set the turtles position
steve.setposition(0,0,0)
steve.setx(0)
steve.sety(0)
steve.setz(0)

#set the turtles headings (angle)
steve.setheading(90)
steve.setverticalheading(90)

#put the pen up/down
steve.penup()
steve.pendown()

#get if the pen is down
print steve.isdown()

#change the block the pen writes with
steve.penblock(block.DIRT.id)

#change the turtles speed (1 - slowest, 10 - fastest, 0 - no animation, it just draws the lines)
steve.speed(10)

#return the turtle to the position it started in
steve.home()

Have a look at the other examples and see what you can create.




8 comments:

  1. Cool stuff.
    By the way, the 'import minecraftturtle' statement is outside the code block.

    ReplyDelete
  2. Awesome! Also, you forgot to share this on G+. I never would have seen it if I hadn't seen it on reddit/r/mcpi.

    ReplyDelete
  3. Hi!
    Awesome stuff you got there! I'm a C# programmer that wants to learn Python with my kid. I'm so glad that I found your blog, packed with cool stuff to get my kids curiosity up. I just got one question. I noticed that you are in Windows with Minecraft 1.6.4 when you run your scripts. Are you going against MineCraftPi or is it possible to run the Python scripts against the PC version of Minecraft?

    Again, great work!

    ReplyDelete
    Replies
    1. Yes, you need to setup a bukkit server and use a plugin called "raspberryjuice" but its a pretty simple setup. Check out http://www.stuffaboutcode.com/2013/06/programming-minecraft-with-bukkit.html

      Delete
    2. Ah, great!
      After some tinkering, connecting to the server, (NOT changing the port :) ) I think i got a connection to MC. But nothing happens. The script runs but I can't see anything. No errors ether.
      I'm I missing something?

      /kalle

      Delete
    3. Never mind, I restarted the server and ran the script again and its working like a charm!
      In one hand this is so simple, but in the other so empowering and awesome. Thanks!

      Delete