WEBVTT

0
00:00.330 --> 00:05.250
So in the last lesson, we saw how to create a turtle that does a random walk.

1
00:05.760 --> 00:07.020
Now in this lesson,

2
00:07.080 --> 00:12.080
I want to show you how you can create a random color for your turtle drawing

3
00:12.540 --> 00:16.890
rather than just using the named colors and then picking a random one out of the

4
00:16.890 --> 00:21.390
list. When you take a look at the pen color of the turtle,

5
00:21.810 --> 00:26.810
you can see that you can use a color string such as what we've been doing using

6
00:26.940 --> 00:30.420
red, yellow, or a hex string that represents the color,

7
00:31.020 --> 00:33.690
or we can use a RGB color.

8
00:34.170 --> 00:39.170
And the RGB color is going to be represented by a tuple of different amounts of

9
00:39.510 --> 00:43.560
red, green, and blue. So what exactly is a tuple?

10
00:44.310 --> 00:44.760
Well,

11
00:44.760 --> 00:49.760
a tuple is a data type in Python and it looks like this.

12
00:50.130 --> 00:52.830
It has round brackets around it

13
00:53.190 --> 00:56.970
and then each of the items inside are separated by a comma.

14
00:57.570 --> 00:59.970
So does this remind you of another data type?

15
01:00.600 --> 01:04.110
Does it not look a little bit like a list? Well,

16
01:04.110 --> 01:08.880
indeed a tuple is very similar to a list. Each of the items that go into the

17
01:08.880 --> 01:10.260
tuple are ordered.

18
01:10.740 --> 01:15.720
So let's pull up my Python console here and let's create my tuple 

19
01:17.130 --> 01:21.060
which is denoted by a set of round parentheses

20
01:21.450 --> 01:25.410
and then let's put in three items, 1, 3, and 8,

21
01:26.010 --> 01:28.320
and there we have created a new tuple.

22
01:28.950 --> 01:32.370
Now in order to get hold of any of those items in there,

23
01:32.460 --> 01:36.540
I can write the name of the variable and then using square brackets

24
01:36.570 --> 01:38.460
I can access the index.

25
01:38.730 --> 01:43.710
So 1 is going to be at position zero, 3 is going to be at position one and

26
01:43.740 --> 01:47.220
8 is going to be at position two. So if I hit enter on this,

27
01:47.280 --> 01:49.350
then you can see that's going to be equal to 8.

28
01:50.820 --> 01:52.860
And if you click this drop down on the right,

29
01:52.890 --> 01:56.700
you can see the same representation; position 0, 1,  2,

30
01:57.060 --> 02:00.510
and those pieces of data and the total length is three.

31
02:01.110 --> 02:05.760
So at this point you might be asking yourself, well, I already know about lists,

32
02:05.820 --> 02:10.320
so why do I need to know about tuples? What's the difference anyways? Well,

33
02:10.350 --> 02:14.220
a tuple is going to be carved in stone

34
02:14.460 --> 02:18.390
so you can't change the values like you can with lists.

35
02:19.500 --> 02:22.020
For example, if I wanted to say

36
02:22.020 --> 02:26.220
get hold of my tuple and instead of the 8 at position two,

37
02:26.610 --> 02:30.630
I want to change it to 12. Now, if I go ahead and hit enter,

38
02:30.660 --> 02:34.350
you can see I get a error and this is a type error.

39
02:34.890 --> 02:39.390
This particular type, tuple, does not support item assignment.

40
02:39.600 --> 02:42.720
So I can't just change it willingly like this.

41
02:43.290 --> 02:46.380
And I also can't remove items from the tuple,

42
02:46.620 --> 02:51.330
I can't change it in any way. Once you've created your tuple 

43
02:51.360 --> 02:56.360
you can consider it as carved in stone and it is what we call immutable.

44
02:57.150 --> 03:01.900
It cannot be changed. So why would you use a tuple then?

45
03:02.620 --> 03:03.880
Well, think about the times

46
03:04.150 --> 03:07.990
like say if you're creating a color scheme for your website or are you creating

47
03:07.990 --> 03:12.220
some sort of list that you want to stay constant and you don't want somebody to

48
03:12.220 --> 03:16.360
accidentally change it or accidentally mess it up, well,

49
03:16.360 --> 03:18.610
then you might want to consider using a tuple.

50
03:20.050 --> 03:23.800
And if you find yourself creating a tuple and then realizing, Oh,

51
03:23.830 --> 03:26.020
actually I need to change it well,

52
03:26.020 --> 03:29.650
then you can simply put your tuple inside a list like this,

53
03:30.010 --> 03:32.800
and you can actually convert it into a list.

54
03:34.150 --> 03:36.010
Coming back to our documentation,

55
03:36.400 --> 03:41.400
the color is represented by a tuple because if you define a color,

56
03:41.680 --> 03:43.330
you're unlikely to change it.

57
03:44.020 --> 03:46.870
And then we have a r, g, and b,

58
03:46.960 --> 03:50.380
which is in the range between zero and the color mode.

59
03:50.860 --> 03:55.860
So the mode can be between 0 and 1 or 0 and 255. In the course resources,

60
03:58.030 --> 04:01.810
I've linked to this really handy RGB calculator tool,

61
04:02.290 --> 04:06.190
which demonstrates how RGB colors work so perfectly.

62
04:06.610 --> 04:10.060
You can see that if we have zero amount of red, zero amounts of green,

63
04:10.090 --> 04:11.920
zero amounts of blue, we get black.

64
04:12.430 --> 04:17.430
But if we mix different amounts of red and different amounts of green and

65
04:18.670 --> 04:22.030
different amounts of blue, then we can get all of the colors in the rainbow.

66
04:22.360 --> 04:26.020
The three primary colors can create any color that you dream of.

67
04:26.620 --> 04:29.110
And each of these sliders,

68
04:29.140 --> 04:34.000
you can see, goes from 0 all the way up to 255.

69
04:34.570 --> 04:38.230
So more red, more green or more blue.

70
04:39.220 --> 04:42.730
And this is what the RGB color ends up looking like.

71
04:43.930 --> 04:48.790
I find it easier to think about the range between 0 and 255.

72
04:49.330 --> 04:50.200
So in turtle,

73
04:50.200 --> 04:54.880
we actually have to change the color mode to 255.

74
04:55.510 --> 04:57.340
So to do this, it's a little bit tricky

75
04:57.370 --> 04:59.920
which is why I didn't want to put this as part of the challenge.

76
05:00.310 --> 05:04.750
But we have to tap into the actual turtle module and not the turtle object,

77
05:04.990 --> 05:09.910
and then change the color mode for that module. Back in our code,

78
05:09.940 --> 05:12.490
we can tap into our turtle module

79
05:12.820 --> 05:15.910
which is known as 't' in our code.

80
05:17.080 --> 05:22.080
And then we can tap into the color mode to make it go from 0 to 255.

81
05:24.610 --> 05:29.590
So now that we've changed the color mode, we can now create a random color.

82
05:30.040 --> 05:33.280
So let's get rid of this list of colors. Instead,

83
05:33.280 --> 05:36.880
I'm going to create a new function called a random_color,

84
05:37.960 --> 05:42.960
and this function is going to return a random color using RGB.

85
05:44.440 --> 05:47.410
So let's first create a random red.

86
05:48.280 --> 05:53.280
So we use the random module and then use the randint and then generate a number

87
05:53.620 --> 05:56.740
between 0 and 255.

88
05:57.710 --> 06:02.420
And then we can repeat this process for the other two color spaces,

89
06:02.750 --> 06:05.900
so green and for blue.

90
06:06.920 --> 06:08.930
Now we've got three random numbers,

91
06:08.960 --> 06:11.480
one each representing each of the color spaces,

92
06:11.840 --> 06:15.590
and then we can generate our tuple. Here's the challenge for you.

93
06:15.950 --> 06:18.290
See if you can return from this function

94
06:18.350 --> 06:23.350
a tuple that consists of the three random integers, r, g and b,

95
06:24.710 --> 06:29.710
and then use that random color to color the turtle drawing instead of this broken

96
06:30.920 --> 06:34.070
code which relied on that previous list of colors.

97
06:34.610 --> 06:37.220
Pause the video and see if you can complete this challenge.

98
06:40.150 --> 06:42.460
All right. So we know that to create a tuple 

99
06:42.460 --> 06:45.460
we need a set of round brackets or parentheses,

100
06:45.940 --> 06:48.610
and then we're going to put each of these in order,

101
06:48.610 --> 06:53.380
so r, g and b. So here's our tuple created,

102
06:53.410 --> 06:56.860
and this is basically our new random color.

103
06:57.460 --> 07:02.460
And then this function is going to return that random color as the output.

104
07:03.850 --> 07:08.350
So now instead of using random.choice and using that previous list,

105
07:08.860 --> 07:11.770
all we have to do is get hold of a random color

106
07:12.160 --> 07:16.840
that's going to be outputted from this function and then use it to color Tim's

107
07:16.840 --> 07:19.780
drawing. So let's run this code again,

108
07:20.320 --> 07:23.380
and you should be able to see that in this random walk,

109
07:23.500 --> 07:25.780
the color is completely random.

110
07:26.110 --> 07:31.110
Pretty much every single turn is going to draw something completely different in

111
07:31.540 --> 07:32.373
terms of color.

112
07:33.190 --> 07:38.080
And this is achieved by learning a little bit about how colors work and how we

113
07:38.080 --> 07:41.860
can generate them using the random RGB colors.