Now, our next challenge is our press up.
And this challenge is going to require you to draw a random walk.
Well, it's basically your turtle making random movements North, East,
South, or West, each time it progresses by the same distance,
but it can at any point choose which direction it wants to go out of the four.
And also you can see we've kept the color palette from the previous challenge,
and I've just applied it to the random walk so that each time it walks,
it's going to pick a different color.
Now this random walk is something that's used often in mathematics and other
disciplines.
So it's a way that can be used to model various real life situations.
Like for example
the financial status of a gambler or things like the path of a molecule in a
liquid or gas or the search path of a foraging animal,
and a whole bunch of other things. Take a look at this Wikipedia article
which I'll link to and take a look at all the applications
including sculpture making or Physics or brain research and a whole lot more.
So once you're ready,
go ahead and see if you can program your turtle to create a random walk using
random colors.
And I want you to think about how you can get the thickness of the drawing seen
here, so each of the lines is a lot thicker than before.
And also see if you can figure out how you can speed up the turtle so that it
draws much faster. And everything is of course
going to be in the documentation somewhere for you to find. Pause the video and
complete the challenge now.
Alright. So I'm going to delete all the code from our previous challenge,
but I'm going to keep my list of random colors,
which I'm going to use for my random walk here.
Now I'm going to keep the random import
even though it's currently unused because, as you guessed it, our random
walk is going to require some randomness.
So how do we actually get our robot to move in the four different directions?
Well, let's define another list called directions
and this is going to have all of the different directions
the turtle can face. So we'll start from 0
which will be facing East and then 90,
which will be facing North, 180 will be West and 270 will be South.
So now that we've got these random directions,
we can get Tim, our little turtle, to start moving.
So every time we're going to move forward by the same distance,
and it doesn't really matter how far you move your turtle
as long as you set a reasonable number.
So I'm just going to choose 30 and then I'm going to get my turtle to turn in a
random direction.
So I don't want to just use right all the time, so you can use left or right,
and pick a random direction from the list. But alternatively,
you can actually use this set heading. So for example,
we can set it to those four directions.
All we have to do is call setheading and then pass in one of those angles.
That's what we're going to do here.
We're going to use the random module and the random choice method to pick a
random direction.
So now this robot is going to move forwards and then it's going to turn a random
direction. But of course, we want this to loop a few times.
So let's say that we get it to loop 200 times.
I think that should be enough to draw a decent sized image.
Let's set the range to maybe 200 times for this loop to run.
I think that should get us a decent image. Now,
if you chose 300 or 500 or whatever the number, it doesn't matter.
As long as you can get a decent image on screen, then it's perfectly fine.
So we've now satisfied the basic constraints of a random walk
and you can see our turtle is now wandering around this alleyway,
completely aimlessly and modeling a real life process,
like an animal that's hunting for food.
Now let's implement the next part of this.
Let's go ahead and change our code so that it generates a random color for each
step.
That's pretty simple cause we did it before in the last lesson.
So we're going to get Tim to change his color and we're going to use the random
module
and the choice method to pick from all the colors that we've already got in this
list here. Now that it's pretty much random colors,
random walking, the next thing we want to do
if you saw my demo of it is we want to make each of the lines a bit thicker.
So how do we do that? Well, we have to head back to the documentation.
So it's probably going to be one of these methods.
And if we just read through the names of the methods,
they should give us a bit of an indication of which one might be the one that we
want.
If we take a look at the drawing control for our pen,
you can see there's a method called pen size and you can call this either pen
size or width, but it does the same thing. Essentially
it just sets the size of the pen which the turtle draws with.
If we set the pen size to something a little bit wider,
say maybe 15 in width,
then you can see when our turtle draws,
it's now a lot wider and it's a lot easier to
see what it's doing. Now,
the final trick I want to show you is how you can speed up the turtle's
animation. So if I was to wait for it to finish drawing here,
that's perfectly possible,
but I can also speed it up by tapping into the speed method for turtle.
And I can either specify this speed as an integer or as a string.
So the strings are fastest, fast, normal, slow, slowest,
and this determines the animation speed of the turtle as it draws.
So I'm going to go for fastest so we can say tim.
speed. And we'll put in that string, fastest.
And now when we run it, you can see it animates way faster
and it's drawing at the speed of light.
Our little turtle is completely going nuts,
but this means that it will take much less time to complete our 200 cycles as
we requested.
So I hope you enjoyed this lesson and you managed to get your turtle to do a random
walk. Now,
many of you would have done this challenge and you might've thought, wouldn't it
be nice if I could generate a completely random color rather than just selecting
a random one from this list? Well,
we can do that and I'll show you how in the next lesson.