1
00:00:00,360 --> 00:00:03,330
Now, our next challenge is our press up.

2
00:00:04,170 --> 00:00:09,170
And this challenge is going to require you to draw a random walk.

3
00:00:10,080 --> 00:00:14,850
Well, it's basically your turtle making random movements North, East,

4
00:00:14,850 --> 00:00:19,620
South, or West, each time it progresses by the same distance,

5
00:00:19,950 --> 00:00:24,810
but it can at any point choose which direction it wants to go out of the four.

6
00:00:25,290 --> 00:00:29,130
And also you can see we've kept the color palette from the previous challenge,

7
00:00:29,460 --> 00:00:34,410
and I've just applied it to the random walk so that each time it walks,

8
00:00:34,470 --> 00:00:36,090
it's going to pick a different color.

9
00:00:37,110 --> 00:00:42,110
Now this random walk is something that's used often in mathematics and other

10
00:00:42,630 --> 00:00:43,463
disciplines.

11
00:00:43,650 --> 00:00:48,650
So it's a way that can be used to model various real life situations.

12
00:00:49,380 --> 00:00:50,700
Like for example

13
00:00:50,700 --> 00:00:55,700
the financial status of a gambler or things like the path of a molecule in a

14
00:00:57,270 --> 00:01:01,320
liquid or gas or the search path of a foraging animal,

15
00:01:01,380 --> 00:01:06,000
and a whole bunch of other things. Take a look at this Wikipedia article

16
00:01:06,030 --> 00:01:09,600
which I'll link to and take a look at all the applications

17
00:01:09,630 --> 00:01:14,630
including sculpture making or Physics or brain research and a whole lot more.

18
00:01:16,560 --> 00:01:17,820
So once you're ready,

19
00:01:18,030 --> 00:01:23,030
go ahead and see if you can program your turtle to create a random walk using

20
00:01:23,310 --> 00:01:24,210
random colors.

21
00:01:24,480 --> 00:01:29,480
And I want you to think about how you can get the thickness of the drawing seen

22
00:01:30,180 --> 00:01:33,600
here, so each of the lines is a lot thicker than before.

23
00:01:34,140 --> 00:01:39,140
And also see if you can figure out how you can speed up the turtle so that it

24
00:01:39,330 --> 00:01:43,050
draws much faster. And everything is of course

25
00:01:43,050 --> 00:01:47,430
going to be in the documentation somewhere for you to find. Pause the video and

26
00:01:47,430 --> 00:01:48,660
complete the challenge now.

27
00:01:52,820 --> 00:01:56,240
Alright. So I'm going to delete all the code from our previous challenge,

28
00:01:56,510 --> 00:01:59,150
but I'm going to keep my list of random colors,

29
00:01:59,450 --> 00:02:01,610
which I'm going to use for my random walk here.

30
00:02:02,510 --> 00:02:04,370
Now I'm going to keep the random import

31
00:02:04,400 --> 00:02:08,180
even though it's currently unused because, as you guessed it, our random

32
00:02:08,190 --> 00:02:10,759
walk is going to require some randomness.

33
00:02:11,780 --> 00:02:16,640
So how do we actually get our robot to move in the four different directions?

34
00:02:17,090 --> 00:02:20,240
Well, let's define another list called directions

35
00:02:20,750 --> 00:02:24,560
and this is going to have all of the different directions

36
00:02:24,590 --> 00:02:27,800
the turtle can face. So we'll start from 0

37
00:02:28,190 --> 00:02:32,240
which will be facing East and then 90,

38
00:02:32,240 --> 00:02:37,240
which will be facing North, 180 will be West and 270 will be South.

39
00:02:40,220 --> 00:02:42,800
So now that we've got these random directions,

40
00:02:42,830 --> 00:02:46,160
we can get Tim, our little turtle, to start moving.

41
00:02:46,610 --> 00:02:49,940
So every time we're going to move forward by the same distance,

42
00:02:50,630 --> 00:02:53,360
and it doesn't really matter how far you move your turtle

43
00:02:53,780 --> 00:02:56,480
as long as you set a reasonable number.

44
00:02:56,750 --> 00:03:01,750
So I'm just going to choose 30 and then I'm going to get my turtle to turn in a

45
00:03:02,560 --> 00:03:03,820
random direction.

46
00:03:04,330 --> 00:03:09,330
So I don't want to just use right all the time, so you can use left or right,

47
00:03:10,390 --> 00:03:14,710
and pick a random direction from the list. But alternatively,

48
00:03:14,710 --> 00:03:18,490
you can actually use this set heading. So for example,

49
00:03:18,520 --> 00:03:21,100
we can set it to those four directions.

50
00:03:21,640 --> 00:03:26,560
All we have to do is call setheading and then pass in one of those angles.

51
00:03:27,250 --> 00:03:28,210
That's what we're going to do here.

52
00:03:28,240 --> 00:03:33,240
We're going to use the random module and the random choice method to pick a

53
00:03:34,900 --> 00:03:36,340
random direction.

54
00:03:37,660 --> 00:03:42,660
So now this robot is going to move forwards and then it's going to turn a random

55
00:03:43,420 --> 00:03:47,230
direction. But of course, we want this to loop a few times.

56
00:03:47,320 --> 00:03:50,350
So let's say that we get it to loop 200 times.

57
00:03:50,410 --> 00:03:53,740
I think that should be enough to draw a decent sized image.

58
00:03:54,400 --> 00:03:58,600
Let's set the range to maybe 200 times for this loop to run.

59
00:03:58,930 --> 00:04:01,480
I think that should get us a decent image. Now,

60
00:04:01,480 --> 00:04:05,770
if you chose 300 or 500 or whatever the number, it doesn't matter.

61
00:04:06,190 --> 00:04:10,240
As long as you can get a decent image on screen, then it's perfectly fine.

62
00:04:10,960 --> 00:04:14,170
So we've now satisfied the basic constraints of a random walk

63
00:04:14,350 --> 00:04:18,130
and you can see our turtle is now wandering around this alleyway,

64
00:04:18,160 --> 00:04:21,820
completely aimlessly and modeling a real life process,

65
00:04:21,880 --> 00:04:23,650
like an animal that's hunting for food.

66
00:04:25,120 --> 00:04:27,370
Now let's implement the next part of this.

67
00:04:27,490 --> 00:04:32,490
Let's go ahead and change our code so that it generates a random color for each

68
00:04:32,560 --> 00:04:33,393
step.

69
00:04:33,850 --> 00:04:37,870
That's pretty simple cause we did it before in the last lesson.

70
00:04:38,200 --> 00:04:42,640
So we're going to get Tim to change his color and we're going to use the random

71
00:04:42,640 --> 00:04:43,473
module

72
00:04:45,100 --> 00:04:50,100
and the choice method to pick from all the colors that we've already got in this

73
00:04:50,800 --> 00:04:55,390
list here. Now that it's pretty much random colors,

74
00:04:55,540 --> 00:04:58,510
random walking, the next thing we want to do

75
00:04:58,780 --> 00:05:03,780
if you saw my demo of it is we want to make each of the lines a bit thicker.

76
00:05:04,570 --> 00:05:09,040
So how do we do that? Well, we have to head back to the documentation.

77
00:05:09,670 --> 00:05:12,370
So it's probably going to be one of these methods.

78
00:05:12,760 --> 00:05:15,100
And if we just read through the names of the methods,

79
00:05:15,400 --> 00:05:20,400
they should give us a bit of an indication of which one might be the one that we

80
00:05:20,680 --> 00:05:21,513
want.

81
00:05:22,540 --> 00:05:26,020
If we take a look at the drawing control for our pen,

82
00:05:26,290 --> 00:05:31,290
you can see there's a method called pen size and you can call this either pen

83
00:05:31,480 --> 00:05:35,350
size or width, but it does the same thing. Essentially

84
00:05:35,350 --> 00:05:39,130
it just sets the size of the pen which the turtle draws with.

85
00:05:40,900 --> 00:05:45,070
If we set the pen size to something a little bit wider,

86
00:05:47,380 --> 00:05:50,710
say maybe 15 in width,

87
00:05:51,370 --> 00:05:53,830
then you can see when our turtle draws,

88
00:05:53,890 --> 00:05:57,130
it's now a lot wider and it's a lot easier to

89
00:05:57,260 --> 00:05:59,420
see what it's doing. Now,

90
00:05:59,420 --> 00:06:03,290
the final trick I want to show you is how you can speed up the turtle's

91
00:06:03,290 --> 00:06:07,100
animation. So if I was to wait for it to finish drawing here,

92
00:06:07,100 --> 00:06:08,630
that's perfectly possible,

93
00:06:08,840 --> 00:06:13,840
but I can also speed it up by tapping into the speed method for turtle.

94
00:06:15,020 --> 00:06:20,020
And I can either specify this speed as an integer or as a string.

95
00:06:22,070 --> 00:06:25,940
So the strings are fastest, fast, normal, slow, slowest,

96
00:06:26,240 --> 00:06:30,950
and this determines the animation speed of the turtle as it draws.

97
00:06:31,370 --> 00:06:35,900
So I'm going to go for fastest so we can say tim.

98
00:06:35,900 --> 00:06:40,340
speed. And we'll put in that string, fastest.

99
00:06:41,480 --> 00:06:46,040
And now when we run it, you can see it animates way faster

100
00:06:46,400 --> 00:06:48,500
and it's drawing at the speed of light.

101
00:06:49,070 --> 00:06:52,580
Our little turtle is completely going nuts,

102
00:06:53,780 --> 00:06:58,610
but this means that it will take much less time to complete our 200 cycles as

103
00:06:58,630 --> 00:06:59,620
we requested. 

104
00:07:00,970 --> 00:07:04,870
So I hope you enjoyed this lesson and you managed to get your turtle to do a random

105
00:07:04,870 --> 00:07:06,130
walk. Now,

106
00:07:06,250 --> 00:07:09,760
many of you would have done this challenge and you might've thought, wouldn't it

107
00:07:09,760 --> 00:07:14,760
be nice if I could generate a completely random color rather than just selecting

108
00:07:15,070 --> 00:07:17,800
a random one from this list? Well,

109
00:07:17,920 --> 00:07:21,520
we can do that and I'll show you how in the next lesson.

