Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.
The turtle module in Python’s standard library provides a classic implementation which moves a triangular turtle around a graphical window drawing geometric shapes.
The picraft turtle module is similar, but instead of a two-dimensional graphical window, its canvas is the Minecraft world. The module provides an object-oriented interface for advanced users that want to control multiple turtles and a simpler procedural interface for newer programmers.
When the turtle is initially created or shown, its default position is beneath the player’s feet:
>>> from picraft.turtle import *
>>> showturtle()
The turtle’s shape indicates its “forward” direction. Various simple commands can be used to control its orientation and motion:
>>> right(180)
>>> forward(5)
Every operation can be undone, and commands can be built up to construct whole shapes:
>>> undo()
>>> penup()
>>> forward(5)
>>> left(90)
>>> pendown()
>>> fillblock('diamond_block')
>>> fill(True)
>>> forward(3)
>>> left(90)
>>> forward(4)
>>> left(135)
>>> forward(4)
>>> fill(False)
Parameters: | distance (float) – the number of blocks to move forward. |
---|
Move the turtle forward by the specified distance, in the direction the turtle is headed:
>>> position()
Vector(x=2, y=-1, z=13)
>>> forward(5)
>>> position()
Vector(x=2, y=-1, z=18)
>>> forward(-2)
>>> position()
Vector(x=2, y=-1, z=16)
Parameters: | distance (float) – the number of blocks to move back. |
---|
Move the turtle backward by the specified distance, opposite to the direction the turtle is headed. Does not change the turtle’s heading:
>>> heading()
0.0
>>> position()
Vector(x=2, y=-1, z=18)
>>> backward(2)
>>> position()
Vector(x=2, y=-1, z=16)
>>> heading()
0.0
Parameters: | angle (float) – the number of degrees to turn counter clockwise. |
---|
Turns the turtle left (counter-clockwise) by angle degrees:
>>> heading()
90.0
>>> left(90)
>>> heading()
0.0
Parameters: | angle (float) – the number of degrees to turn clockwise. |
---|
Turns the turtle right (clockwise) by angle degrees:
>>> heading()
0.0
>>> right(90)
>>> heading()
90.0
Parameters: | angle (float) – the number of degrees to increase elevation by. |
---|
Turns the turtle’s nose (its elevation) up by angle degrees:
>>> elevation()
-45.0
>>> up(45)
>>> elevation()
0.0
Parameters: | angle (float) – the number of degrees to reduce elevation by. |
---|
Turns the turtle’s nose (its elevation) down by angle degrees:
>>> elevation()
0.0
>>> down(45)
>>> elevation()
-45.0
Parameters: |
---|
Moves the turtle to an absolute position. If the pen is down, draws a line between the current position and the newly specified position. Does not change the turtle’s orientation:
>>> tp = pos()
>>> tp
Vector(x=2, y=-1, z=16)
>>> setpos(4, -1, 16)
>>> pos()
Vector(x=4, y=-1, z=16)
>>> setpos((0, -1, 16))
>>> pos()
Vector(x=0, y=-1, z=16)
>>> setpos(tp)
>>> pos()
Vector(x=2, y=-1, z=16)
If y and z are None, x must be a triple of coordinates, a Vector, or another Turtle.
Parameters: | x (float) – the new x coordinate |
---|
Set the turtle’s first coordinate to x; leave the second and third coordinates unchanged:
>>> position()
Vector(x=2, y=-1, z=16)
>>> setx(5)
>>> position()
Vector(x=5, y=-1, z=16)
Parameters: | y (float) – the new y coordinate |
---|
Set the turtle’s second coordinate to y; leave the first and third coordinates unchanged:
>>> position()
Vector(x=2, y=-1, z=16)
>>> sety(5)
>>> position()
Vector(x=2, y=5, z=16)
Parameters: | z (float) – the new z coordinate |
---|
Set the turtle’s third coordinate to z; leave the first and second coordinates unchanged:
>>> position()
Vector(x=2, y=-1, z=16)
>>> setz(5)
>>> position()
Vector(x=2, y=-1, z=5)
Parameters: | to_angle (float) – the new heading |
---|
Set the orientation of the turtle on the ground plane (X-Z) to to_angle. The common directions in degrees correspond to the following axis directions:
heading | axis |
---|---|
0 | +Z |
90 | +X |
180 | -Z |
270 | -X |
>>> setheading(90)
>>> heading()
90.0
Parameters: | to_angle (float) – the new elevation |
---|
Set the elevation of the turtle away from the ground plane (X-Z) to to_angle. At 0 degrees elevation, the turtle moves along the ground plane (X-Z). At 90 degrees elevation, the turtle moves vertically upward, and at -90 degrees, the turtle moves vertically downward:
>>> setelevation(90)
>>> elevation()
90.0
Move the turtle to its starting position (this is usually beneath where the player was standing when the turtle was spawned), and set its heading to its start orientation (0 degrees heading, 0 degrees elevation):
>>> heading()
90.0
>>> elevation
45.0
>>> position()
Vector(x=2, y=-1, z=16)
>>> home()
>>> position()
Vector(x=0, y=-1, z=0)
>>> heading()
0.0
>>> elevation()
0.0
Undo (repeatedly) the last turtle action(s):
>>> for i in range(4):
... fd(5) ... lt(90) ... >>> for i in range(8): ... undo()
Return the turtle’s current location (x, y, z) as a Vector:
>>> pos()
Vector(x=2, y=-1, z=18)
Parameters: |
---|
Return the angle between the line from the turtle’s position to the position specified within the ground plane (X-Z):
>>> home()
>>> forward(5)
>>> towards(0, 0, 0)
-180.0
>>> left(90)
>>> forward(5)
>>> towards(0, 0, 0)
135.0
If y and z are None, x must be a triple of coordinates, a Vector, or another Turtle.
Return the turtle’s current heading (its orientation along the ground plane, X-Z):
>>> home()
>>> right(90)
>>> heading()
90.0
Return the turtle’s current elevation (its orientation away from the ground plane, X-Z):
>>> home()
>>> up(90)
>>> elevation()
90.0
Return the turtle’s x coordinate:
>>> home()
>>> xcor()
0 >>> left(90) >>> forward(2) >>> xcor() 2
Return the turtle’s y coordinate:
>>> home()
>>> ycor()
-1 >>> up(90) >>> forward(2) >>> ycor() 1
Return the turtle’s z coordinate:
>>> home()
>>> zcor()
0 >>> forward(2) >>> zcor() 2
Parameters: |
---|
Return the distance from the turtle to (x, y, z), the given vector, or the given other turtle, in blocks:
>>> home()
>>> distance((0, -1, 5))
5.0
>>> forward(2)
>>> distance(0, -1, 5)
3.0
Put the “pen” down; the turtle draws new blocks when it moves.
Put the “pen” up; movement doesn’t draw new blocks.
Returns True if the pen is down, False if it’s up.
Return or set the block that the turtle draws when it moves. Several input formats are allowed:
>>> penblock()
<Block "stone" id=1 data=0>
>>> penblock('diamond_block')
>>> penblock()
<Block "diamond_block" id=57 data=0>
>>> penblock(1, 0)
>>> penblock()
<Block "stone" id=1 data=0>
Return or set the block that the turtle fills shapes with. Several input formats are allowed:
>>> fillblock()
<Block "stone" id=1 data=0>
>>> fillblock('diamond_block')
>>> fillblock()
<Block "diamond_block" id=57 data=0>
>>> fillblock(1, 0)
>>> fillblock()
<Block "stone" id=1 data=0>
Parameters: | flag (bool) – True if beginning a fill, False if ending a fill. |
---|
Call fill(True) before drawing the shape you want to fill, and fill(False) when done. When used without argument: return the fill state (True if filling, False otherwise).
Call just before drawing a shape to be filled. Equivalent to fill(True).
Fill the shape drawn after the last call to begin_fill(). Equivalent to fill(False).
Make the turtle visible:
>>> showturtle()
Make the turtle invisible:
>>> hideturtle()
Return True if the turtle is shown, False if it’s hidden:
>>> hideturtle()
>>> isvisible()
False >>> showturtle() >>> isvisible() True
Return number of entries in the undobuffer:
>>> while undobufferentries():
... undo()
Return the Turtle object itself. Only reasonable use: as a function to return the “anonymous” turtle:
>>> pet = getturtle()
>>> pet.fd(50)
>>> pet
<picraft.turtle.Turtle object at 0x...>
Return the TurtleScreen object the turtle is drawing on:
>>> ts = getscreen()
>>> ts
<picraft.turtle.TurtleScreen object at 0x...> >>> ts.world.say(“Hello world!”)