2. USING COLORS

 

 

INTRODUCTION

 

The turtle draws its trail using a colored pen, for which it knows some additional instructions. As long as the pen is down, the turtle draws a trail. Using the statement penUp(), it moves its pen up and stops drawing. With penDown(), the pen is brought back down to the drawing area, so that a trail is drawn again.

Using setPenColor(color) you can select the color of the pen. It is important that you put the name of the color inside quotation marks. As always in programming, the turtle knows only English color names. The following list is not complete, but here are some examples: yellow, gold, orange, red, maroon, violet, magenta, purple, navy, blue, skyblue, cyan, turquoise, lightgreen, green, darkgreen, chocolate, brown, black, gray, white.

PROGRAMMING CONCEPTS: Drawing with colors

 

 

COLOR AND PEN WIDTH

 

This program makes the turtle draw a candle with a wide red line. You can set the line width in pixels using the command setLineWidth().

You can draw the yellow flame with the command dot(). There is one part of the program where the turtle moves without drawing a line, because the pen was lifted with the command penUp(). After penDown() is called, the turtle draws again.

hideTurtle() makes the turtle invisible.

 

 
from gturtle import *

makeTurtle()

setLineWidth(60)
setPenColor("red")
forward(100) 
penUp()
forward(50)
penDown()
setPenColor("yellow")
dot(40)
setLineWidth(5)
setPenColor("black")
back(15) 
hideTurtle()
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

The drawing pen of the turtle can change color with use of the statement setPenColor(color). With penUp() the turtle stops drawing, and with penDown() it continues to draw again. You can control the width of the line using setLineWidth(width).

The turtle knows the so-called X11 colors. There are a few dozen names of colors which you can find on the Internet http://cng.seas.rochester.edu/CNG/docs/x11color.html . You can select all of these colors with the setPenColor(color) statement.

 

 

FILLED AREAS

 

You can fill almost any area with colors using the turtle. With the command startPath(), you tell the turtle that you intend to fill an area. The turtle remembers its current position as the starting point of a sequence of lines. You then move around the area with the turtle and finally call the command fillPath(), which connects the start point to the end point and fills in the resulting area with color. You can adjust the fill color with setFillColor(color).

Lines starting with the hash symbol (#) are called comments, which are ignored during the execution of the program. You can add these to make notes for yourself or others.

 
For example, you could specify under which program name the file is stored, or add text that explains your code.

from gturtle import *

makeTurtle()

setPenColor("sandybrown")
setFillColor("sandybrown")
startPath()
forward(100)
right(45)
forward(72)
right(90)
forward(72)
right(45)
forward(100)
fillPath()
hideTurtle() 
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

If you want to fill an area defined by a sequence of lines, you begin drawing with the command startPath(). Using fillPath(), the start point and the end point are connected and the enclosed area is filled.

You can write comments by starting a code line with the # symbol.

 

 

EXERCISES

 

1.

Draw a regular hexagon with the turtle and make each side a different color.
 
2.
Draw a traffic light. You can draw the black rectangle with a pencil width of 80 and the circles with dot(40).  
3.
The turtle should draw the adjacent image.