Turtle Graphics

Introduction

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.

$ python
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

Turtle Commands

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.

Show 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.

>>> turtle.showturtle()

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.

Shapes

The turtle.shapes command sets the turtle shape to one of these shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.

>>> turtle.shape("turtle")

Forward and Backward

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.

>>> turtle.forward(100)

Turning

You can turn the turtle to face a different direction by using either the turtle.right(angle) or turtle.left(angle) command.

>>> turtle.right(90)

These are the angles in a Cartesian or x-y plane.

Pen Up and Down

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.

>>> turtle.penup()
>>> turtle.pendown()

Drawing Circles

The turtle.circle(radius) command makes the turtle draw a circle with a radius you specify. For example.

>>> turtle.circle(100)

Drawing Dots

The turtle.dot(size, color) command draws simple dots.

>>> turtle.dot(100, "red")

Changing the Pen Size

You can use the turtle.pensize(width) command to change the width of the turtle's pen, in pixels.

>>> turtle.pensize(5)

Changing the Drawing Color

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.

>>> turtle.pencolor('red')

Changing the Background Color

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.

>>> turtle.bgcolor('green')

Moving the Turtle to a Specific Location.

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.

>>> turtle.goto(100,100)

If the turtle's pen is down, a line will be drawn as the turtle moves.

Getting the Turtle's Current Position

You can use the turtle.pos() command to display the turtle's current position.

>>> turtle.pos()

Displaying Text in the Graphics Window

You can use the turtle.write(text) command to display text in the graphics window.

>>> turtle.write("Hello, World!")

Resetting the Screen

  • 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.

>>> turtle.reset()
>>> turtle.clear()
>>> turtle.clearscreen()

Undo

The turtle.undo() command undoes the previous command.

>>> turtle.undo()

Reference

Python 3's Turtle Graphics Reference Page

https://docs.python.org/3.3/library/turtle.html

Last updated