WEBVTT

0
00:00.360 --> 00:03.330
Now, our next challenge is our press up.

1
00:04.170 --> 00:09.170
And this challenge is going to require you to draw a random walk.

2
00:10.080 --> 00:14.850
Well, it's basically your turtle making random movements North, East,

3
00:14.850 --> 00:19.620
South, or West, each time it progresses by the same distance,

4
00:19.950 --> 00:24.810
but it can at any point choose which direction it wants to go out of the four.

5
00:25.290 --> 00:29.130
And also you can see we've kept the color palette from the previous challenge,

6
00:29.460 --> 00:34.410
and I've just applied it to the random walk so that each time it walks,

7
00:34.470 --> 00:36.090
it's going to pick a different color.

8
00:37.110 --> 00:42.110
Now this random walk is something that's used often in mathematics and other

9
00:42.630 --> 00:43.463
disciplines.

10
00:43.650 --> 00:48.650
So it's a way that can be used to model various real life situations.

11
00:49.380 --> 00:50.700
Like for example

12
00:50.700 --> 00:55.700
the financial status of a gambler or things like the path of a molecule in a

13
00:57.270 --> 01:01.320
liquid or gas or the search path of a foraging animal,

14
01:01.380 --> 01:06.000
and a whole bunch of other things. Take a look at this Wikipedia article

15
01:06.030 --> 01:09.600
which I'll link to and take a look at all the applications

16
01:09.630 --> 01:14.630
including sculpture making or Physics or brain research and a whole lot more.

17
01:16.560 --> 01:17.820
So once you're ready,

18
01:18.030 --> 01:23.030
go ahead and see if you can program your turtle to create a random walk using

19
01:23.310 --> 01:24.210
random colors.

20
01:24.480 --> 01:29.480
And I want you to think about how you can get the thickness of the drawing seen

21
01:30.180 --> 01:33.600
here, so each of the lines is a lot thicker than before.

22
01:34.140 --> 01:39.140
And also see if you can figure out how you can speed up the turtle so that it

23
01:39.330 --> 01:43.050
draws much faster. And everything is of course

24
01:43.050 --> 01:47.430
going to be in the documentation somewhere for you to find. Pause the video and

25
01:47.430 --> 01:48.660
complete the challenge now.

26
01:52.820 --> 01:56.240
Alright. So I'm going to delete all the code from our previous challenge,

27
01:56.510 --> 01:59.150
but I'm going to keep my list of random colors,

28
01:59.450 --> 02:01.610
which I'm going to use for my random walk here.

29
02:02.510 --> 02:04.370
Now I'm going to keep the random import

30
02:04.400 --> 02:08.180
even though it's currently unused because, as you guessed it, our random

31
02:08.190 --> 02:10.760
walk is going to require some randomness.

32
02:11.780 --> 02:16.640
So how do we actually get our robot to move in the four different directions?

33
02:17.090 --> 02:20.240
Well, let's define another list called directions

34
02:20.750 --> 02:24.560
and this is going to have all of the different directions

35
02:24.590 --> 02:27.800
the turtle can face. So we'll start from 0

36
02:28.190 --> 02:32.240
which will be facing East and then 90,

37
02:32.240 --> 02:37.240
which will be facing North, 180 will be West and 270 will be South.

38
02:40.220 --> 02:42.800
So now that we've got these random directions,

39
02:42.830 --> 02:46.160
we can get Tim, our little turtle, to start moving.

40
02:46.610 --> 02:49.940
So every time we're going to move forward by the same distance,

41
02:50.630 --> 02:53.360
and it doesn't really matter how far you move your turtle

42
02:53.780 --> 02:56.480
as long as you set a reasonable number.

43
02:56.750 --> 03:01.750
So I'm just going to choose 30 and then I'm going to get my turtle to turn in a

44
03:02.560 --> 03:03.820
random direction.

45
03:04.330 --> 03:09.330
So I don't want to just use right all the time, so you can use left or right,

46
03:10.390 --> 03:14.710
and pick a random direction from the list. But alternatively,

47
03:14.710 --> 03:18.490
you can actually use this set heading. So for example,

48
03:18.520 --> 03:21.100
we can set it to those four directions.

49
03:21.640 --> 03:26.560
All we have to do is call setheading and then pass in one of those angles.

50
03:27.250 --> 03:28.210
That's what we're going to do here.

51
03:28.240 --> 03:33.240
We're going to use the random module and the random choice method to pick a

52
03:34.900 --> 03:36.340
random direction.

53
03:37.660 --> 03:42.660
So now this robot is going to move forwards and then it's going to turn a random

54
03:43.420 --> 03:47.230
direction. But of course, we want this to loop a few times.

55
03:47.320 --> 03:50.350
So let's say that we get it to loop 200 times.

56
03:50.410 --> 03:53.740
I think that should be enough to draw a decent sized image.

57
03:54.400 --> 03:58.600
Let's set the range to maybe 200 times for this loop to run.

58
03:58.930 --> 04:01.480
I think that should get us a decent image. Now,

59
04:01.480 --> 04:05.770
if you chose 300 or 500 or whatever the number, it doesn't matter.

60
04:06.190 --> 04:10.240
As long as you can get a decent image on screen, then it's perfectly fine.

61
04:10.960 --> 04:14.170
So we've now satisfied the basic constraints of a random walk

62
04:14.350 --> 04:18.130
and you can see our turtle is now wandering around this alleyway,

63
04:18.160 --> 04:21.820
completely aimlessly and modeling a real life process,

64
04:21.880 --> 04:23.650
like an animal that's hunting for food.

65
04:25.120 --> 04:27.370
Now let's implement the next part of this.

66
04:27.490 --> 04:32.490
Let's go ahead and change our code so that it generates a random color for each

67
04:32.560 --> 04:33.393
step.

68
04:33.850 --> 04:37.870
That's pretty simple cause we did it before in the last lesson.

69
04:38.200 --> 04:42.640
So we're going to get Tim to change his color and we're going to use the random

70
04:42.640 --> 04:43.473
module

71
04:45.100 --> 04:50.100
and the choice method to pick from all the colors that we've already got in this

72
04:50.800 --> 04:55.390
list here. Now that it's pretty much random colors,

73
04:55.540 --> 04:58.510
random walking, the next thing we want to do

74
04:58.780 --> 05:03.780
if you saw my demo of it is we want to make each of the lines a bit thicker.

75
05:04.570 --> 05:09.040
So how do we do that? Well, we have to head back to the documentation.

76
05:09.670 --> 05:12.370
So it's probably going to be one of these methods.

77
05:12.760 --> 05:15.100
And if we just read through the names of the methods,

78
05:15.400 --> 05:20.400
they should give us a bit of an indication of which one might be the one that we

79
05:20.680 --> 05:21.513
want.

80
05:22.540 --> 05:26.020
If we take a look at the drawing control for our pen,

81
05:26.290 --> 05:31.290
you can see there's a method called pen size and you can call this either pen

82
05:31.480 --> 05:35.350
size or width, but it does the same thing. Essentially

83
05:35.350 --> 05:39.130
it just sets the size of the pen which the turtle draws with.

84
05:40.900 --> 05:45.070
If we set the pen size to something a little bit wider,

85
05:47.380 --> 05:50.710
say maybe 15 in width,

86
05:51.370 --> 05:53.830
then you can see when our turtle draws,

87
05:53.890 --> 05:57.130
it's now a lot wider and it's a lot easier to

88
05:57.260 --> 05:59.420
see what it's doing. Now,

89
05:59.420 --> 06:03.290
the final trick I want to show you is how you can speed up the turtle's

90
06:03.290 --> 06:07.100
animation. So if I was to wait for it to finish drawing here,

91
06:07.100 --> 06:08.630
that's perfectly possible,

92
06:08.840 --> 06:13.840
but I can also speed it up by tapping into the speed method for turtle.

93
06:15.020 --> 06:20.020
And I can either specify this speed as an integer or as a string.

94
06:22.070 --> 06:25.940
So the strings are fastest, fast, normal, slow, slowest,

95
06:26.240 --> 06:30.950
and this determines the animation speed of the turtle as it draws.

96
06:31.370 --> 06:35.900
So I'm going to go for fastest so we can say tim.

97
06:35.900 --> 06:40.340
speed. And we'll put in that string, fastest.

98
06:41.480 --> 06:46.040
And now when we run it, you can see it animates way faster

99
06:46.400 --> 06:48.500
and it's drawing at the speed of light.

100
06:49.070 --> 06:52.580
Our little turtle is completely going nuts,

101
06:53.780 --> 06:58.610
but this means that it will take much less time to complete our 200 cycles as

102
06:58.630 --> 06:59.620
we requested. 

103
07:00.970 --> 07:04.870
So I hope you enjoyed this lesson and you managed to get your turtle to do a random

104
07:04.870 --> 07:06.130
walk. Now,

105
07:06.250 --> 07:09.760
many of you would have done this challenge and you might've thought, wouldn't it

106
07:09.760 --> 07:14.760
be nice if I could generate a completely random color rather than just selecting

107
07:15.070 --> 07:17.800
a random one from this list? Well,

108
07:17.920 --> 07:21.520
we can do that and I'll show you how in the next lesson.