Lesson 2.4: Coloring with Turtle
Up until this point, our Turtle drawings were all just black lines. Let’s make them more fun and add colors!
Colors in Turtle Graphics
A typical graphics software supports two kinds of color – pen color and fill color. In Turtle Graphics, you can set these colors by calling the pencolor() / color() and fillcolor() functions.
The statements below all do the same thing – they set the pen color to blue and the fill color to aqua:
As you can imagine, the fill color is used to fill the inside of shapes and drawings. If you want your shape filled, you have to use the commands begin_fill() (before the drawing starts) and end_fill() (at the end of your drawing). Interestingly, the fill feature works for both closed shapes (like square or triangle) and open shapes (like half-circle).
Let’s illustrate this on an example.
As you can see, Turtle filled the closed square. And it also filled the half-square the best way it could. Notice that, at each point, the outline of the turtle has the pen color and body of the turtle has the fill color.
Copy the code above into a file named square_and_a_half.py and experiment with different shapes and colors. Can you draw a square that is filled half way with one color and half way with another color?
We have added color to our drawings and they look real nice. To make them look even nicer, we can change the width of the lines we are drawing by calling the function pensize() with the desired line width. The default line width is 1, the thinnest line possible.
Let’s look at an example that will demonstrate different line widths. Note that the function range(12) gives us 12 different integer values starting at 0 and assigns them one-by-one to the variable i on each iteration. So, in the first iteration of the for-loop, i = 0. In the second iteration, i = 1, etc. Finally, in the last iteration, i = 11. You can see the values of i and size in the output of the Python Shell.
The picture is really not that pretty but you can clearly see that the line is getting thicker and thicker as pen size increases.
Draw two side-by-side squares that are filled with 4 different colors like on the picture below.
The outline of the first square should be purple and the fill colors should be pink and magenta. The outline of the second square should be blue and the fill colors should be aqua and yellow. The outlines should be 4 pixels wide. At the end of your program, your turtle should have a black outline and a green fill and should face up.
Store your program in a file named half_squares.py. You can use square_and_a_half.py as a starting point.
Draw the 72-point red-and-blue star from the end of
Unit 2: Turtle Graphics page.
Hint: Use two for-loops to do the drawing.
Another lesson complete, another set of tools learned! At this point, you should be quite familiar with the Turtle Graphics package and the functionality it provides. You can draw triangles, squares, stars. You can use different colors and different fills. You can change the size of Turtle pen. If you’ve followed our lessons and did all the exercises up to this point, you have truly mastered Turtle Graphics at the beginners level.
Keep in mind, however, that our main goal in this class is to teach Python and programming skills. Turtle is just a tool that helps us visualize newly learned material. We will continue to use it in our examples and explanations but our main focus from now on will be Python language and general programming skills.