Turtle Graphics
Last updated
Last updated
Turtle graphics was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966 to teach students to code.
You can use the CS50 Sandbox with the X Window option to use Turtle Graphics.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.
By combining together these and similar commands, intricate shapes and pictures can be drawn. Here's an example:
The first step in using Python's turtle graphics system is to import the turtle module. Let's try using turtle in Python's interactive mode.
The Python turtle is initially position in the center of the graphics window. To display the turtle in the window, you can use the turtle.showturtle() command, to hide the turtle, you can use the turtle.hideturtle() command.
The Python turtle is initially positioned in the center of the graphics window. The arrowhead shows the direction that the turtle is currently facing. The default heading is 0 degree.
The turtle.shapes command sets the turtle shape to one of these shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.
The turtle.forward(distance) or turtle.fw(distance) and turtle.backward(distance) or turtle.bk(distance) moves the turtle forward or backward by the specified distance, in the direction the turtle is headed.
You can turn the turtle to face a different direction by using either the turtle.right(angle) or turtle.left(angle) command.
These are the angles in a Cartesian or x-y plane.
The turtle.penup() command pulls the pen up so there will be no drawing when the pen moves. The turtle.pendown() command pulls the pen down.
The turtle.circle(radius) command makes the turtle draw a circle with a radius you specify. For example.
The turtle.dot(size, color) command draws simple dots.
You can use the turtle.pensize(width) command to change the width of the turtle's pen, in pixels.
You can use the turtle.pencolor(color) command to change the turtle's drawing color. The color argument is the name of a color, as a string.
You can use the turtle.bgcolor(color) to change the background color of the turtle's graphics window. The color argument is the name of a color, as a string.
The pixel in the center of the graphics window is at the position (0,0)
You can use the turtle.goto(x, y) command to move the turtle from its current location to a specific position in the graphics window.
If the turtle's pen is down, a line will be drawn as the turtle moves.
You can use the turtle.pos() command to display the turtle's current position.
You can use the turtle.write(text) command to display text in the graphics window.
The turtle.reset() command erases all drawings that currently appear in the graphics window, resets the drawing color to black and resets the turtle to its original position at the center of the screen.
The turtle.clear() command erases all drawings that currently appear in the graphics window.
The turtle.clearscreen() command erases all drawings that currently appear in the graphics window.
The turtle.undo() command undoes the previous command.
Python 3's Turtle Graphics Reference Page
Command
Description
turtle.forward(distance)
Move the turtle forward by the specified distance, in the direction the turtle is headed.
turtle.backward(distance)
Move the turtle backward by distance, opposite to the direction the turtle is headed.
turtle.home()
Move turtle to the origin – coordinates (0,0) – and set its heading to its start-orientation.
turtle.penup()
Pull the pen up – no drawing when moving.
turtle.pendown()
Pull the pen down – drawing when moving.
turtle.pencolor(colorstring)
Set pencolor to colorstring, which is a Tk color specification string, such as "red", "yellow" etc.
turtle.circle(radius)
Draw a circle with given radius.
turtle.shape("turtle")
Sets the turtle shape to turtle.
turtle.undo()
Undo (repeatedly) the last turtle action(s)
turtle.clear()
Erases all drawings that currently appear in the graphics window.