Turtle Graphics

A turtle is an drawing device for creating lines and shapes, using colors, angles, and screen positions. It was originally developed by Seymour Papert and others using a language called Logo, and has since been implemented in many different environments.

The turtle instructions and brief explanations shown below on this page are a subset of the full set of instructions available on the official python site here.

Drawing Space

The "turtle" starts at x,y = 0,0 which is in the middle of the drawing space

Growing Triangles Demo

(See below for the code for this)

Code for the Image Above

"""

This is the Template Repl for Python with Turtle.

Python with Turtle lets you make graphics easily in Python.

Check out the official docs here: https://docs.python.org/3/library/turtle.html

Author: Dale Reed, 2/13/22, for BTT CS 111

"""


import turtle

t = turtle.Turtle()


# Change the values in this section to affect the drawing

t.speed(0) # Speed, where higher numbers are faster, 0 = no animation

size = 20 # Starting size

increment = 1.007 # How much bigger to make each successive side

number_of_repetitions = 255 # How many times to repeat the pattern

turn_amount = 2 # How many degrees to turn after each triangle

# Color initial values (0..255) and increments

red = 0

red_increment = 1

green = 200

green_increment = -1

blue = 127

blue_increment = 1


# Set the initial color

t.color(red,green,blue)


# Repeat the pattern

iteration = 1

while iteration < number_of_repetitions:

# Draw a single growing triangle

repeats = 1

while repeats < 3:

t.forward( size)

t.left(120)

size *= increment

repeats += 1


# Adjust the color

red += red_increment

if (red == 255) or (red == 0):

red_increment = red_increment * -1


green += green_increment

if (green == 255) or (green == 0):

green_increment = green_increment * -1


blue += blue_increment

if (blue == 255) or (blue == 0):

blue_increment = blue_increment * -1


t.color(red,green,blue)


# After the triangle, turn slightly

t.left( turn_amount)

# Increment so we go on to draw the next triangle

iteration += 1

Assume our Python program starts with:

import turtle

t = turtle.Turtle()

Many of the instructions below have alternative short-hand versions, shown after the | character

Characteristics

  1. t.penup() | up() | pu(): Pen is not drawing

  2. t.pendown() | down() | pd(): Pen is drawing

  3. t.hideturtle() | ht(), turtle.showturtle() | st(): Hides or shows the turtle

  4. t.xcor(), t.ycor(): Returns the x and y pen coordinate

  5. t.position() | pos(): Returns the x,y position

  6. t.bye(): Close the turtle drawing window

Direction

  1. t.right( degrees) | rt( degrees): Rotate turtle clockwise by the number of degrees

  2. t.left( degrees) | lt( degrees): Rotate turtle clockwise by the number of degrees

  3. t.setheading( angle) | seth( angle): Point the turtle in the angle (0..359) direction. [e.g. 0 (east), 90 (north), 180 (west), 270 (south)]

Movement

  1. t.forward( distance) | fd(): Move turtle forward distance pixels

  2. t.backward( distance) | bk( distance) | back( distance): Move turtle backward distance pixels

  3. t.goto( x,y) | setpos( x,y) | setposition( x,y): Move turtle to the x,y coordinates


Shapes

  1. t.color( R,G,B), t.color( s)

t.fillcolor( R,G,B), t.fillcolor( s) Set line / fill color until the color is changed. The three RGB values must either be in the 0..1 range or in the 0..255 range. If using a ‘color string’, it must be the name of a Tk color (e.g., 'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta') The default pen and fill color is 'black' [ Shortcut to colors is: bit.ly/turtlecolors ]

  1. t.pencolor( s): Return or set the pen color

  2. t.pensize() | width(): Return or set the pen thickness

  3. t.circle( radius): Draws a circle of the indicated radius, tangent to the direction the turtle is facing

  4. t.begin_fill(), t.end_fill(): To fill a figure, use t.begin_fill() before you start drawing, draw the figure, then t.end_fill(). The figure will be filled with the present color setting.

Reference: https://docs.python.org/3/library/turtle.html#overview-of-available-turtle-and-screen-methods