Turtle Tutorial


Python With Turtle Tutorial

Python’s turtle graphics library will allow us to use different coding concepts and algorithms we learn throughout the semester to create fun patterns and designs. The turtle is essentially an invisible pen that we can code to move around our screen and draw.

This tutorial will run through some common commands used with the turtle and you will have a fun mini-project at the end!

Begin by creating a Python program file in your lessons directory named ls13-turtle.py

Importing and Setting Up the Turtle

Before we can begin using the turtle we have to import the turtle library so we have access to all of the functions.

  • To import a specific function: from turtle import [function_name]

As you continue through the tutorial, make sure to keep adding each new function to the import statement. Start by importing the following:

Lines and Turning

In order to make the turtle do anything we will call different functions from the turtle library.

  • To make the turtle move forward and draw a line: forward(<distance>)
  • To make the turtle move turn right: right(<degrees>)
  • To make the turtle move turn left: left(<degrees>)

If you try running these commands, you may notice that a window pops up and then almost immediately disappears. We have to add an extra command to prevent this from happening.

  • To make sure the window does not close until this function: done()

The done() function must come after all of the turtle functions that you want to see on the window.

Exercise One:

At this point you have all the commands needed to draw a square! Try and attempt this before moving onto the next section.

Simplifying with Loops

After finishing exercise one you should have code that looks something like the following:

It should already be clear that this repetion is annoyingly tedious. Luckily, we can easily simplify this process.

  • To repeat something a certain number of times: while (<counter_variable> < <number_of_times>):

Exercise Two:

Using the loop that we just made, try converting it into a triangle before moving on to the next section.

GoTo, Penup and Pendown

After finishing exercise two you should have code that looks like the following:

Now, what if we wanted to position our triangle in a different spot on the screen?

  • To move the turtle to a new x, y position: goto(<x_coordinate>,<y_coordinate>):

If you try running the above code before drawing the triangle you will notice an unwanted line is drawn. We need to lift the turtle off the page before using the goto!

  • To prevent the turtle from drawing: penup() || up()
  • To allow the turtle to draw: pendown() || down()

Before moving onto the next section change reposition your triangle so that it looks centered and make sure the size of the triangle is relatively large.

Pen and Fill Color

Pen Color

There are actually two common ways to change the color of the turtle! To complete this next section, open https://htmlcolorcodes.com/color-picker/ in a new tab.

  • To change the color with a string value: color(“<color>”)

While this is useful for the basic colors it doesn’t leave much room for creativity. In order to have more control over our colors we can use RGB (red, green, blue) values instead. Using the color picker, use the site to pick out your favorite color and copy the three numbers next to RGB.

  • To enable RGB mode, add under the import statement: colormode(255)
  • To change the color with RGB values: color(<red>, <green>, <blue>)

Red, Green and Blue are the primary colors of the digital world! By choosing different amounts of each color you are essentially mixing together a new color like you would mix paint.

Fill Color

To fill in a shape we just have to tell the turtle when to start filling and end filling. It will fill with whatever the current color is.

  • To begin fill: begin_fill()
  • To end fill: end_fill()

Other Useful Color Commands

Any of the color commands can either be used with a color string or with RGB values.

  • To set only pen color: pencolor(<color>)
  • To set only fill color: fillcolor(<color>)
  • To set fill and pen color: color(<pencolor>, <fillcolor>)

Exercise Three:

See if you can use the color picker to determine the maximum and minimum values for red, green and blue. What values give a pure white and pure black? Pick a grey color. What do you notice about the three numbers?

Fill in your triangle with your color of choice and make sure to end fill after the triangle is drawn!

Speed, Visibility and the Fun Part

Before finishing the rest of the tutorial we may want to speed up our drawing as well as hide the turtle.

  • To change the speed: speed(<speed>)
  • To end fill: hideturtle() || ht()

Mini-Project

Now we have all of the basic commands needed for creating interesting patterns:

  1. After your first triangle is created, change the pen color to black or a darker version of the color you picked earlier.
  2. Below the code for your first triangle, add another triangle with the same size and location specifications as the first. You should now see a darker outline around the filled in triangle.
  3. Turn the side length of the triangle into a variable and use it for both triangles. For instance:
  1. Inside the second outline triangle loop, multiply the side length variable by a number from 0.95 to 0.98. This will decrease the length of the side each time it is drawn.
  1. Right now this will look like it is not changing much. Try increasing the number of loops of the second outline triangle to a much higher number. Mess around with this until it seems to fully recede!
  2. For one last magic touch, try changing the degrees of the second outline loop to just over 120. Mess around with a range of 120.5 to 123 until you get a result you like!
  3. By slightly changing the different number values at each of these steps you will get a different design. Have some fun seeing what you can make!