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
Before we can begin using the turtle we have to import the turtle library so we have access to all of the functions.
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:
In order to make the turtle do anything we will call different functions from the turtle library.
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.
The done() function must come after all of the turtle functions that you want to see on the window.
At this point you have all the commands needed to draw a square! Try and attempt this before moving onto the next section.
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.
Using the loop that we just made, try converting it into a triangle before moving on to the next section.
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?
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!
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.
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.
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.
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.
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.
Any of the color commands can either be used with a color string or with RGB values.
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!
Before finishing the rest of the tutorial we may want to speed up our drawing as well as hide the turtle.
Now we have all of the basic commands needed for creating interesting patterns: