Now the final challenge for today comes courtesy of a childhood toy that you
might've already seen. So this is going to be our training bike,
and it's going to be the last coding exercise before we tackle our final
project. So what are we doing? We're making a spirograph.
Do you remember those geometric gear shaped things
which you would draw and draw and it would make these funky circular shapes?
Well, that's what we're going to be doing, but with turtle.
We're going to get our turtle to draw a number of circles each with a radius of
a hundred in distance. And once it's done,
we should end up with something that looks like this.
So we're going to be using our random colors and we're going to be using the
documentation.
I want you to dig through the documentation and see if you can figure out how to
draw a circle and to determine how large the circle should be.
And then I want you to figure out how you can get it to repeatedly draw a circle
and change the tilt of the circle by a little bit each time,
so that you end up with that effect. Pause the video and give that a go now.
Alright. Again,
we're going to be scrolling through all of the available methods
looking for something that might help us.
I think it's going to be somewhere in the turtle motion's move and draw section.
And if you look down this list,
you can see there is a method that allows us to draw a circle with a given
radius.
So we can put in the radius as one of the inputs and draw our circle. Heading
back to our code,
I'm going to delete some of the previous parts where we've got the directions
and also changing the pen size.
I'm going to leave the speed as fastest because we're going to need it to go
pretty quick to draw all of those circles.
And I'm also going to delete the final for loop.
Now notice how I've got this warning here telling me that it doesn't really like
the idea of me calling a variable the same as the name of a function.
So why don't we just call this color instead
and then we can return that as the output and get rid of that warning.
Now we're ready to code up our spirograph.
So the first thing that we're going to try and do is to draw a circle.
Now the circle requires a radius,
so I'm going to give it a radius of a hundred. And when I hit run,
we can see briefly that it painted a circle.
So let's go ahead and create our screen from t.screen.
And then we can use that screen.exitonclick method so that when we run our
code, it doesn't actually remove the window until we click on it.
So I can see that I've painted my first circle
and I would like, if possible, to make my circle
have a different color each time.
So I'm going to change the color to use a random color that's produced by this
function. So now every time I run my code,
you'll see a different colored circle.
The turtle is basically gonna draw out this path and then go back to its
original position facing East.
If we wanted multiple circles to overlap with each other,
then we're going to have to change the heading or the pointing direction of our
turtle. If we look at this section,
the current state of our turtle,
you can see that you can get the current position,
the current Y coordinate and X coordinate,
but you can also get hold of the current heading,
so which direction it's pointing towards.
And all we have to do is just call this heading method.
So now if I print my tim.heading,
then you can see that when I run my code and I take a look inside the console,
it's pointing at 0.0.
What if we could change the heading to make it a little bit shifted?
Well,
there's actually a different method for changing the heading and getting the
heading. For example, if we want to set the heading,
then we have to use this method and give it a number.
So what if I get the current heading
and then maybe I add 5 or 10 degrees to it,
and then I change my turtle's heading using set
heading to the current heading plus 10.
And then we get a new circle drawn. So let's see what this looks like.
And you can see that I've drawn one circle and then I've tilted to the left a
little bit and then drawn a second circle. So extrapolating this,
you can imagine that if we had a loop that kept going,
then we could draw our final spirograph shape.
Let's create that loop.
So the parts that don't need to be in the loop is setting the speed.
But the color needs to change each time,
the circle needs to be drawn each time and the heading needs to be updated each
time.
Now I'm going to simplify this line of code to contract it into a single line.
So I'm setting Tim to a new heading
and that is from the current heading plus 10.
So now let's create our loop and let's say
we're going to loop a hundred times.
Now let's run our code and see what it looks like.
You can see it's drawing out our spirograph.
It's not as dense as it previously was,
but look at what it's doing.
It's drawing the same circle on top of a previous circle,
cause it doesn't know when to stop.
It's stopping only once it's gone through the hundred rotations.
So how can we figure out a way to get it to only draw as many circles as we
need?
Let's say that I created a new function called draw_spirograph,
and this function accepts a single parameter,
which is the size of the gap.
And that is going to be the size that's going to be in between each of these
circles that gets drawn.
And that's of course determined by the direction heading of my arrow.
So if I pass in this size of gap,
I'm going to use it here to change the tilt of my circle.
But I can also use that because it's a degree to determine how many times my
spirograph needs to be drawn.
So we know that there's 360 degrees in a full circle.
And for each circle that we're drawing,
we're basically giving it a different offset right? And
that's our size_of_gap.
So if we divide 360 by each offset,
then we'll repeat our code for as many times as we need to draw all the circles
and complete the final spirograph. Now,
the only problem is if I call this method,
dra, I think I meant to write draw.
So draw spirograph and I give it a size of five.
So a little bit smaller than last time,
tilting a little bit less each time. You can see that if I run this code,
we get an error.
And the error says that the float object can't be used as an integer
and it's telling us where we have that float object.
So remember that this number is going to go into here
and then 360 divided by five is actually going to give us a floating point
number, even though it divides cleanly
just because this is how division works in Python. Now,
unfortunately though our range function is going to need a whole number,
an integer.
It can't take a floating point number because then how many times are we
repeating it? 3.4 times? What does that even mean?
So this input has to be a whole number.
So let's turn it into an integer by creating an integer and then wrapping this
calculation inside the brackets. So now when we run our code,
you can see it paints our spirograph, shifting by five degrees each time.
And once it's gone through all of the iterations and the spirograph is
complete, then it actually stops.
Did you manage to get the solution for this challenge?
If not, be sure to review this lesson and fix your code as needed. Once you're
happy with everything that we've explained so far,
then it's time to head to the final challenge.
Let's go ahead and create a million dollar painting.
So for all of that and more, I'll see you on the next lesson.