Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Introduction
  • Turtle Commands
  • Show Turtle
  • Shapes
  • Forward and Backward
  • Turning
  • Pen Up and Down
  • Drawing Circles
  • Drawing Dots
  • Changing the Pen Size
  • Changing the Drawing Color
  • Changing the Background Color
  • Moving the Turtle to a Specific Location.
  • Getting the Turtle's Current Position
  • Displaying Text in the Graphics Window
  • Resetting the Screen
  • Undo
  • Reference

Was this helpful?

Turtle Graphics

PreviousmicrobitNextTurtle Examples

Last updated 5 years ago

Was this helpful?

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
Circles with different colors
Result of the turtle.showturtle() command
Result of the turtle.shape("turtle") command
Result of the turtle.forward(100) command
Result of the turtle.right(90) command
Cartesian Plane
Result of the turtle.circle(100) command
Result of the turtle.dot(100, "red") command
Coordinate Plane
Result of the turtle.write("Hello, World!") command