Now that we've used colorgram to extract all of the colors from our image
and we've created a list from that extraction and we've gotten rid of all the
white shades,
then we're now ready to go ahead and start using this color list to create our Hirst
painting.
So you can either delete the rest of the code or leave it commented out.
But the goal of the challenge is to use turtle and those colors that you've just
extracted to create a spot painting like this.
Here's the requirements of the program.
You're going to paint a painting with 10 by 10 rows of spots.
So that's going to be 10 along this side and 10 along this side.
Each of your dots should be around 20 in size and spaced apart by around 50
paces.
So you're going to need to look at the documentation for turtle to figure out
how to draw these dots.
You're going to have to figure out how to move your turtle so that it creates
this 10 by 10 pattern and paint our hundred dots using the color palette that
we generated. This is your chance to shine,
and it's time to pause the video and complete the final project.
So the first thing we're going to do is we're of course going to need to import
our turtle.
And I'm going to give it an alias just to make it more clear what it is that I'm
referring to. I'm going to create my turtle, which I'll call Tim again.
And it's going to be created from the turtle module and inside there,
there's a turtle class.
Now that we have our object created and saved inside Tim,
we're ready to go ahead and do some things with this turtle.
If you take a look at the documentation for turtle,
you can see that instead of just drawing circles or just moving forwards or left
or right, you can also draw a dot.
Now a dot takes two parameters,
its size and its color.
So we mentioned that we were going to draw a 20-size dot and then we're going to
pass in a random color. So let's create our dot.
Let's say tim.dot,
and then let's give it a size of 20 and then let's give it a random color. Now
because we're using the RGB color that has the r, g and b values between 0 and
255,
we first have to get the turtle module to change its color mode to
255. And we saw this in previous lessons already.
So now we can go into our color list and pick a random color using the random
module.
So let's tap the random module and get hold of the choice method and pass in our
color_list.
So now it's going to choose a random color from that list and then create a dot
hopefully. Now we don't want our painting to disappear once we hit run,
because right now if I run this code,
you can see it paints a dot and then it quickly flashes away.
So let's create a new screen object from the turtle module and the class is
called a screen.
And then we can get our screen to change its behavior by calling exitonclick.
This way when we run our code, you can see our dot painted,
and then only when we click does it disappear.
So we've managed to get our first dot onto the screen.
The next step is figuring out how we can get this process repeated.
What if we get our Tim to move forwards by say,
50 paces, and then we draw another dot.
So let's put this into a loop,
let's get it to run 10 times,
and then let's indent this block so that it goes inside the loop.
And now our turtle is going to draw 10 dots.
But notice how it goes off the screen and it kind of starts bang in the middle.
And there's also other problems like we're drawing the path as the turtle is
traveling. Let's address one thing at a time.
Let's try to move our starting dot so it starts somewhere down here.
Now there's quite a few ways of solving this. One way is simply by turning our
Tim, the turtle, before we go into the loop and setting its heading so that it
points in this direction.
You can have a play around with the heading and through my experimentation or
playing around with the code,
I've realized that I need to get this heading somewhere between 180 and
270.
So what's halfway between 270 and 180? Well,
it's going to be 225.
So let's change our heading and let's try again.
And you can see that our turtle is now moving roughly in the right direction.
So we know that each time the turtle moves by 50.
So if we want it to start about here, ish, then we need to move it one,
two, three, four, five times.
So 50 times five is 250.
So we'll set our heading for Tim and then we'll get him to move forward by 250
paces. So that way we get started right over here.
Now, at this point
it's still going to be pointing in that direction that we set over here.
So we'll have to set the heading back to zero
if we want it to continue in this direction.
Now at this stage,
you might realize you need to do a little bit of tweaking. So we could maybe move
our turtle forwards a little bit more so that it starts around here and we get
enough space for our dots.
But the next important thing we need to solve before we get rid of the lines
because we can still see the path of our turtle
which is really helpful, is we need to figure out how to get the turtle to
turn left, move up by 50, turn left again
and then start creating the second row.
That means we need to turn Tim so that it faces up
which is a heading of 90,
or you could also just turn it left by 90 degrees.
And then we're going to get it to move forwards by 50 paces.
And then we're going to get it to turn left again.
So we'll set the heading to 180, which is facing left.
It's through these step-wise changes to your code
that you can actually get a good sense of what it's doing
because you can see it drawing and then see if it's doing what you want it to
do. Now we know that we have our for loop
which is able to draw a line of dots going from left to right.
Why don't we move our turtle so that it starts out here again,
and then it can move along and create another line?
So after turning it to the left,
we're going to get it to go forwards by one, two, three, four,
five, six, seven, eight, nine,
ten, by 10 times 50 paces.
So that's going to be 500.
So now the final thing we need to do is to change its heading so that it faces
right again. And now we're pretty close.
All we need to do is to get this
to repeat every single time we draw 10 dots.
How can we do that? Well, firstly,
this needs to go inside the for loop. Well,
let's say that we create a new variable called number_of_dots,
and we set it to a hundred. We're going to use that instead of the range
and we're going to create a range from 1 to the total number of dots we need
to create.
And then we can replace this underscore with an actual variable that we'll use
inside the for loop. So we'll call it
dot_count because this is going to represent the current number of dots that
have been painted. Now that we've got the dot count,
we can then go ahead and use an if statement to check and see if the dot_count
% 10 is equal to zero.
Then this means that the dot count is either 10 or it's 20, 30,
40, 50, 60, 70, 100.
And under those circumstances,
we want our tim to move so that it can go to a new line.
So now, if I run this,
you can see that Tim draws one row of dots and then swings back around,
start at the beginning and draws another row of dots. And then keeps on repeating
this until it gets to the point where it's drawn hundred dots.
But this is obviously very slow
so we could speed things up a little bit by changing the speed of Tim at the
very beginning to fastest.
So this way it'll animate a lot quicker and we can see our dots show up and it
takes a little bit less time to see it in action.
Once our painting is completed, you can see there's one other bug.
Our final dot is not painted. And this is of course
because our range operator goes from 1 to 100
and that means it's only going to run 99 times because 100 - 1 is equal to
99. So we can add one to this.
Yeah.
So once we've changed that, then we managed to get our hundredth dot,
which of course, again, triggers a new line. Now,
a lot of these lines were really useful when
we were trying to visualize how our turtle is moving.
But now that we've got our painting in place,
we need to try and get rid of these lines.
So we know that from the documentation we can use pen up and pen down. At the
very beginning and the moment where we've created Tim,
we can already get the pen to come up so that we don't draw those lines that you
see. By drawing dots we don't actually need the pen to go down.
It's just going to leave a dot and then move on.
So just by writing pen up and no pen down,
we already to get our dots being drawn.
Now the very last improvement that I can think of is once our painting is
completed, I'd really like to get rid of the shape of the turtle,
which in our case is a little arrow.
Now that's really helpful when we're trying to figure out which direction it's
facing or how it's moving. But at the very end, we don't really want this.
So again, back to the documentation, how can we get rid of our turtle?
Well, there's show turtle and hide turtle.
And this is how we make the turtle
invisible. It even tells us that you can do this while in the middle of a drawing because
hiding the turtle will speed up the drawing observably.
So let's go ahead and hide our turtle as well as moving the pen up.
And let's re-visualize this.
And you can see our dots being drawn without our turtle
and we end up with a beautiful Hirst painting that we've created completely
from scratch. Well,
maybe with a little bit of help from Damien Hirst's color palette.
So I hope you enjoyed this project. And I look forward to seeing you tomorrow.