So in the last lesson, we saw how to create a turtle that does a random walk.
Now in this lesson,
I want to show you how you can create a random color for your turtle drawing
rather than just using the named colors and then picking a random one out of the
list. When you take a look at the pen color of the turtle,
you can see that you can use a color string such as what we've been doing using
red, yellow, or a hex string that represents the color,
or we can use a RGB color.
And the RGB color is going to be represented by a tuple of different amounts of
red, green, and blue. So what exactly is a tuple?
Well,
a tuple is a data type in Python and it looks like this.
It has round brackets around it
and then each of the items inside are separated by a comma.
So does this remind you of another data type?
Does it not look a little bit like a list? Well,
indeed a tuple is very similar to a list. Each of the items that go into the
tuple are ordered.
So let's pull up my Python console here and let's create my tuple
which is denoted by a set of round parentheses
and then let's put in three items, 1, 3, and 8,
and there we have created a new tuple.
Now in order to get hold of any of those items in there,
I can write the name of the variable and then using square brackets
I can access the index.
So 1 is going to be at position zero, 3 is going to be at position one and
8 is going to be at position two. So if I hit enter on this,
then you can see that's going to be equal to 8.
And if you click this drop down on the right,
you can see the same representation; position 0, 1, 2,
and those pieces of data and the total length is three.
So at this point you might be asking yourself, well, I already know about lists,
so why do I need to know about tuples? What's the difference anyways? Well,
a tuple is going to be carved in stone
so you can't change the values like you can with lists.
For example, if I wanted to say
get hold of my tuple and instead of the 8 at position two,
I want to change it to 12. Now, if I go ahead and hit enter,
you can see I get a error and this is a type error.
This particular type, tuple, does not support item assignment.
So I can't just change it willingly like this.
And I also can't remove items from the tuple,
I can't change it in any way. Once you've created your tuple
you can consider it as carved in stone and it is what we call immutable.
It cannot be changed. So why would you use a tuple then?
Well, think about the times
like say if you're creating a color scheme for your website or are you creating
some sort of list that you want to stay constant and you don't want somebody to
accidentally change it or accidentally mess it up, well,
then you might want to consider using a tuple.
And if you find yourself creating a tuple and then realizing, Oh,
actually I need to change it well,
then you can simply put your tuple inside a list like this,
and you can actually convert it into a list.
Coming back to our documentation,
the color is represented by a tuple because if you define a color,
you're unlikely to change it.
And then we have a r, g, and b,
which is in the range between zero and the color mode.
So the mode can be between 0 and 1 or 0 and 255. In the course resources,
I've linked to this really handy RGB calculator tool,
which demonstrates how RGB colors work so perfectly.
You can see that if we have zero amount of red, zero amounts of green,
zero amounts of blue, we get black.
But if we mix different amounts of red and different amounts of green and
different amounts of blue, then we can get all of the colors in the rainbow.
The three primary colors can create any color that you dream of.
And each of these sliders,
you can see, goes from 0 all the way up to 255.
So more red, more green or more blue.
And this is what the RGB color ends up looking like.
I find it easier to think about the range between 0 and 255.
So in turtle,
we actually have to change the color mode to 255.
So to do this, it's a little bit tricky
which is why I didn't want to put this as part of the challenge.
But we have to tap into the actual turtle module and not the turtle object,
and then change the color mode for that module. Back in our code,
we can tap into our turtle module
which is known as 't' in our code.
And then we can tap into the color mode to make it go from 0 to 255.
So now that we've changed the color mode, we can now create a random color.
So let's get rid of this list of colors. Instead,
I'm going to create a new function called a random_color,
and this function is going to return a random color using RGB.
So let's first create a random red.
So we use the random module and then use the randint and then generate a number
between 0 and 255.
And then we can repeat this process for the other two color spaces,
so green and for blue.
Now we've got three random numbers,
one each representing each of the color spaces,
and then we can generate our tuple. Here's the challenge for you.
See if you can return from this function
a tuple that consists of the three random integers, r, g and b,
and then use that random color to color the turtle drawing instead of this broken
code which relied on that previous list of colors.
Pause the video and see if you can complete this challenge.
All right. So we know that to create a tuple
we need a set of round brackets or parentheses,
and then we're going to put each of these in order,
so r, g and b. So here's our tuple created,
and this is basically our new random color.
And then this function is going to return that random color as the output.
So now instead of using random.choice and using that previous list,
all we have to do is get hold of a random color
that's going to be outputted from this function and then use it to color Tim's
drawing. So let's run this code again,
and you should be able to see that in this random walk,
the color is completely random.
Pretty much every single turn is going to draw something completely different in
terms of color.
And this is achieved by learning a little bit about how colors work and how we
can generate them using the random RGB colors.