Comment on page
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:
Circles with different colors
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.
Python 3.7.1 (default, Feb 20 2019, 02:45:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
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.
Result of the turtle.showturtle() command
The turtle.shapes command sets the turtle shape to one of these shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.
Result of the turtle.shape("turtle") command
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.
Result of the turtle.forward(100) command
You can turn the turtle to face a different direction by using either the turtle.right(angle) or turtle.left(angle) command.
Result of the turtle.right(90) 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.
Result of the turtle.circle(100) command
The turtle.dot(size, color) command draws simple dots.
>>> turtle.dot(100, "red")
Result of the turtle.dot(100, "red") command
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.
>>> turtle.write("Hello, World!")
Result of the turtle.write("Hello, World!") command
- 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