Altering the Turtle
Here are resources about Turtle and TK symbolic colors name:
00:00
Altering the Turtle. So far, the turtle has been in its default state, but nearly every aspect of its appearance can be changed. In this section, you’ll see how to do this, starting off by taking a look at color. Before we look at the code, a quick explanation of the three ways you can specify colors in turtle
is in order. The first is a named color, such as "blue"
, seen here.
00:26
The full list of the Tk graphic toolkit colors is linked at the bottom of this slide and has over 700 named colors in there, so you may find this useful if you have favorites that you want to use often. The second is using a hex code, commonly used in websites. Here, each channel of red, green, and blue is specified as a two-digit hexadecimal number ranging from 00
to FF
. Don’t worry about using this if you don’t understand how hex codes work, but if you have a favorite color from a web page, you may be able to use it with this method.
01:03
The third method is specifying a tuple of color intensities from 0.0
to 1.0
. Again, each one of these represents a color channel—red, green, and blue—but it’s easier to understand than the hex code seen above as the intensities range from 0.0
—off—to 1.0
—fully on.
01:20
It’s also easier to program variables to have these values, allowing variation in the colors onscreen to be controlled by the code you write. Let’s take a look at those three systems in action across a few different turtle
commands. First up, the turtle.color()
command.
01:37 As you can see, it takes some arguments. We can set the pen color and the fill color. If you enter a single value, then both pen color and fill color will be set to it.
01:55 Giving two colors allows them to be set separately.
02:04
It’s also possible to set them separately using the .pencolor()
and .fillcolor()
methods.
02:13 It’s also possible to change the background color, but this is a function of the screen and not the turtle. Here, instead of using a named color, we’re going to use three numbers as seen in the previous slides.
02:26
Then we get an interesting shade of green. Filling in a shape. Coloring in an image often improves it and, handily, turtle
can do all of the work for you.
02:39
You just need to tell it when you’re starting, what you want to be filled in with .begin_fill()
, and then when to do the filling in with .end_fill()
. So here, the fill is begun and now the shape is drawn.
02:51
In this case, it will be a triangle. Now the fill is performed using .end_fill()
.
03:02
Changing the turtle’s shape. The turtle’s shape can be altered from the classic shape to others. Firstly, the size has been changed using .shapesize()
to make it clearer onscreen.
03:15
With that complete, it’s possible to use the .shape()
method to alter the turtle into a 'square'
, 'arrow'
, 'circle'
, 'triangle'
, 'classic'
, and even 'turtle'
, as seen here.
03:31
It’s also possible to produce your own shapes for the turtle if you need to. We’ll change it back to a normal size and 'classic'
for the remainder of the video.
03:43
Changing the pen speed. By default, the turtle draws at a leisurely pace, allowing its movement to be followed as your code runs. There may be times when this default is too fast or too slow, and this can be altered with the .speed()
function, providing the turtle with a speed, either a number from 1
to 10
or a string chosen from 'slowest'
, 'slow'
, 'normal'
, 'fast'
, or 'fastest'
.
04:08
There it is at the default. And if we slow it down, you can see it’s slower. However, it’s possible to make it extremely quick with a speed of 10
.
04:24 There, the drawing is instantaneous. Customizing all in one line.
04:33
It’s possible to customize the turtle in a single line using the .pen()
function.
04:46
As you can see, the turtle
has changed, and if we rotate it and move forward, you can see the line has also changed as has the speed. Once you have an idea of how you want to customize your turtle, this can often be the way to go.
05:02
Having seen the many ways you can alter the turtle’s appearance and behavior, next you’ll see some other skills needed to achieve mastery of the Python turtle
module.
Become a Member to join the conversation.