WEBVTT

0
00:00.540 --> 00:02.700
As I mentioned, in this section

1
00:02.700 --> 00:06.030
we're going to be working heavily with the Python turtle module.

2
00:06.630 --> 00:08.070
And as we've seen before,

3
00:08.130 --> 00:13.130
this turtle module is essentially a way for us to be able to draw graphics onto

4
00:13.380 --> 00:14.213
the screen.

5
00:14.520 --> 00:19.320
And you can imagine it as just this little wandering turtle with a pen on its

6
00:19.320 --> 00:23.160
back and everywhere it goes, it leaves a trail of line.

7
00:23.970 --> 00:28.230
So let's take a closer look at the turtle graphics module. Now here,

8
00:28.260 --> 00:33.240
I've already created a brand new PyCharm project called day-18-start,

9
00:33.630 --> 00:37.800
and I've created a blank main.py. Inside this project,

10
00:37.890 --> 00:42.450
we're going to import our turtle. So as you saw from previous lessons,

11
00:42.450 --> 00:47.070
we can import the turtle module and get hold of the turtle class

12
00:47.100 --> 00:50.220
by simply saying from the turtle module,

13
00:50.520 --> 00:55.520
import the Turtle class with the capital T. And now we can work with that class

14
00:56.760 --> 01:01.230
and we're going to use it to create a new turtle object.

15
01:01.680 --> 01:03.540
Let's call it timmy_the_turtle,

16
01:05.310 --> 01:09.210
and this is going to be created from the turtle class.

17
01:09.240 --> 01:13.410
So this is going to be a new turtle object saved inside this variable.

18
01:14.070 --> 01:16.680
Now, when we actually run our project,

19
01:18.630 --> 01:21.450
you'll see a brief flash of a window

20
01:21.510 --> 01:25.950
and then it will disappear. In order for that window to stay where it is

21
01:26.040 --> 01:29.430
and only exit when we actually click on the window,

22
01:29.730 --> 01:33.510
we have to create another object, which we'll call our screen.

23
01:34.020 --> 01:36.870
And this comes from the total module as well.

24
01:36.960 --> 01:39.690
And it's a class called Screen.

25
01:40.140 --> 01:42.720
We also have to import that if we want to use it.

26
01:43.260 --> 01:45.390
So now that we've got hold of the screen,

27
01:45.420 --> 01:49.110
we can use one of the functions called exitonclick.

28
01:50.520 --> 01:52.560
So this way, when we run our code,

29
01:52.590 --> 01:57.590
we can see our turtle show up in the middle as this little arrow and our window

30
01:58.200 --> 02:02.490
won't disappear until we click on it. That's what exitonclick does.

31
02:02.970 --> 02:06.210
So we need to keep this bit of the code at the very bottom.

32
02:06.330 --> 02:09.810
It has to happen after we've done all this stuff with turtle.

33
02:09.930 --> 02:13.920
So I'm going to move it to the bottom of the file. Now here's a question.

34
02:13.950 --> 02:18.850
How do I know how to do that? Is it because I am super experienced 

35
02:18.850 --> 02:21.450
and I remember everything off the top of my head? No.

36
02:21.780 --> 02:25.920
Is it because I'm super clever and I can just work things out? No,

37
02:26.070 --> 02:27.750
that's definitely not the answer.

38
02:28.170 --> 02:32.730
The way that programmers go about figuring out how to use these modules or

39
02:32.730 --> 02:36.240
packages is through the use of documentation.

40
02:36.570 --> 02:41.570
So let's take a look at the turtle graphics documentation.

41
02:42.810 --> 02:46.530
And this is what the documentation looks like. It's a very,

42
02:46.530 --> 02:51.530
very long document that goes through all of the things that you can do with this

43
02:52.560 --> 02:55.260
module. Now, if we scroll down,

44
02:55.260 --> 03:00.260
you've got a contents page here and you can see the various things you can do

45
03:00.550 --> 03:05.550
with the turtle. Control it's motion or control the pen or change its visibility

46
03:06.910 --> 03:11.380
or appearance. Now let's take a look at this shape function,

47
03:11.890 --> 03:16.240
and you can see the documentation explains to us that this particular function

48
03:16.570 --> 03:20.800
will set the turtles shape to one with a given name.

49
03:21.520 --> 03:26.440
And we can choose from the various shapes, including arrow, turtle, circle,

50
03:26.450 --> 03:29.370
square, triangle, and classic. So 

51
03:29.380 --> 03:34.380
let's use this knowledge and change timmy_the_turtle to have a different shape.

52
03:35.440 --> 03:38.050
So inside here, we can put any of those strings.

53
03:38.080 --> 03:42.490
So I'm going to change it to a turtle shape. This way when I run my code,

54
03:42.520 --> 03:45.610
you can see an actual turtle appear on the screen.

55
03:45.670 --> 03:49.510
It just makes it a little bit easier to visualize what's going on with this

56
03:49.510 --> 03:53.290
module. Now, of course you can change it to any of the other ones,

57
03:53.380 --> 03:57.610
test it out and see if it works. In an ideal world

58
03:57.640 --> 04:02.640
you would read through the entire documentation of a module before you start

59
04:02.650 --> 04:05.170
using it. But of course, in the real world,

60
04:05.170 --> 04:08.110
we all know that that's not quite possible.

61
04:08.680 --> 04:13.030
Very often you'll need the help of Stack Overflow. For example,

62
04:13.060 --> 04:18.060
if we wanted to look at turtle graphics and we wanted to change the shape

63
04:19.980 --> 04:21.420
of the turtle.

64
04:22.500 --> 04:26.310
<v 0>Then you can see that when you actually search for this inside the Stack</v>

65
04:26.310 --> 04:30.690
Overflow search bar, your results are not very relevant.

66
04:31.260 --> 04:36.260
But if I put the same query into google.com and at the end I tag on the keyword

67
04:37.530 --> 04:41.490
Stack Overflow, we get results that are a lot more relevant.

68
04:41.610 --> 04:43.950
So if we take a look at this post,

69
04:44.010 --> 04:48.090
you can see that it's starting to talk about this shape method.

70
04:48.600 --> 04:52.740
And down here, this answer even links to the relevant part of the documentation

71
04:53.100 --> 04:57.150
which we've already seen previously, this shape method.

72
04:57.600 --> 05:02.600
The idea is that you might need to Google around to see how to achieve a certain

73
05:02.940 --> 05:03.960
thing you want to do.

74
05:04.410 --> 05:07.710
And then once you've seen some of the methods that they mentioned in the

75
05:07.710 --> 05:08.543
answers,

76
05:08.580 --> 05:13.470
then go to the documentation and read up on it in order to actually understand

77
05:13.470 --> 05:18.120
what it does and how to use it. Coming back to our turtle,

78
05:18.240 --> 05:22.200
what if we wanted to change the color of the turtle? Well,

79
05:22.200 --> 05:26.550
this should be quite easy. We can see that in the color control section,

80
05:26.850 --> 05:31.850
we can use this color method to set the pen color and fill color.

81
05:32.880 --> 05:36.600
So for example, we could say timmy_the_turtle.color,

82
05:36.870 --> 05:39.180
and we can put in a string here like red.

83
05:39.690 --> 05:42.000
So now when I run my code,

84
05:42.330 --> 05:47.330
you can see that my turtle timmy is now red instead of the previous default

85
05:48.660 --> 05:50.760
color of black. Now,

86
05:50.760 --> 05:55.140
how do I know which colors I can use other than the ones shown in the example,

87
05:55.170 --> 05:57.350
red or green? Well,

88
05:57.350 --> 06:02.350
it says that when we want to change the color using a color string or an RGB

89
06:02.690 --> 06:07.190
color, then it accepts inputs as in pen color.

90
06:07.220 --> 06:12.220
So it basically says it's using this method to set the color. And this method

91
06:12.560 --> 06:16.880
gets the color from a Tk color specifications string.

92
06:17.300 --> 06:19.700
So let's Google and see what that is.

93
06:21.950 --> 06:23.300
If we click on the first link,

94
06:23.330 --> 06:28.330
you can see it takes us to the strings and we've got a whole bunch of names and

95
06:28.610 --> 06:31.160
their corresponding RGB values.

96
06:31.640 --> 06:34.610
So what is this Tk exactly?

97
06:35.390 --> 06:39.170
Tk is short for the module tkinter,

98
06:39.200 --> 06:40.820
which is the Tk interface.

99
06:40.910 --> 06:45.200
And this is one of the ways that you can use Python to create a graphical user

100
06:45.200 --> 06:48.350
interface, which is also known as a GUI.

101
06:48.680 --> 06:52.520
If you think back to the beginning of computer history,

102
06:53.060 --> 06:55.340
if you think about the Apple Lisa one,

103
06:55.400 --> 06:59.270
that was the first computer that had a graphical user interface,

104
06:59.270 --> 07:01.430
where it had a mouse and you can point and click.

105
07:01.820 --> 07:04.100
And this was huge back in the day.

106
07:04.640 --> 07:08.480
But before then we had text interfaces like MS-

107
07:08.480 --> 07:12.170
DOS or like our console when we're using Python.

108
07:12.770 --> 07:17.540
The text interfaces accept text commands and the graphical user interfaces can

109
07:17.540 --> 07:22.540
show images and allows you to click and drag and do all of those things by

110
07:23.150 --> 07:28.150
looking instead of just typing commands. And tkinter is what the turtle module

111
07:29.810 --> 07:34.460
actually relies on under the hood to create these graphics,

112
07:34.700 --> 07:36.860
like our turtle showing up here.

113
07:37.790 --> 07:40.520
So now that we've seen where these names come from,

114
07:40.970 --> 07:45.970
a much easier way of using these colors is through a page where they have all

115
07:46.550 --> 07:50.030
been rendered on screen. So in the course resources,

116
07:50.060 --> 07:54.680
we link to this page where you can see all the colors next to their names,

117
07:54.920 --> 07:57.290
and you can pick and choose whichever one you want.

118
07:57.980 --> 08:02.980
Have a go at changing the color of the turtle by using one of these colors that

119
08:03.050 --> 08:04.040
you see on screen.

120
08:05.390 --> 08:09.710
Now that we've seen how we can create a new turtle and change its appearance,

121
08:09.980 --> 08:14.750
it's time to look at how we can make it do certain things. In the documentation,

122
08:14.780 --> 08:18.620
you can see there's a whole bunch of things you can do in terms of moving and

123
08:18.620 --> 08:23.600
drawing. A very simple one would be to move it forward or to move it backwards.

124
08:26.570 --> 08:30.590
So we can tell it to move forwards by a hundred paces.

125
08:31.310 --> 08:33.980
And when we rerun the code, this is what we see.

126
08:35.060 --> 08:39.110
So we can get it to move forwards, move backwards, and also move left

127
08:39.110 --> 08:43.400
and right. So here, the input that it takes is an angle,

128
08:43.490 --> 08:48.490
a number between zero and 360 to tell it what angle

129
08:48.740 --> 08:51.530
to turn right or to turn left. For example,

130
08:51.560 --> 08:56.100
currently our turtle is facing East. So if we wanted to face

131
08:56.310 --> 08:56.960
<v 2>South,</v>

132
08:56.960 --> 09:01.670
then we have to get it to turn right by 90 degrees. So we can say,

133
09:01.910 --> 09:05.090
timmy_the_turtle.right and then we put 90 in here.

134
09:06.380 --> 09:10.400
And that's what it looks like. In the upcoming lessons

135
09:10.430 --> 09:13.820
we've got a whole bunch of turtle challenges for you

136
09:14.300 --> 09:18.290
that's going to test not only your knowledge of programming that you've learned so

137
09:18.290 --> 09:23.150
far, but also how well you can read and understand documentation.

138
09:23.630 --> 09:28.630
This is a key skill to acquire as a seasoned developer because after all,

139
09:29.390 --> 09:32.030
we can't always rely on other people telling us what to do.

140
09:32.060 --> 09:34.070
We have to be able to get it from the source.

141
09:34.490 --> 09:38.420
So make sure that you've got the documentation pulled up and have a glance

142
09:38.450 --> 09:39.283
through it.

143
09:39.320 --> 09:42.950
Then you can head over to the next lesson and start tackling some of these

144
09:42.950 --> 09:43.783
challenges.

145
09:44.390 --> 09:48.770
Now remember that some of these challenges can be quite a bit of a struggle,

146
09:49.130 --> 09:53.180
but keep in mind that the struggle is good. The more that you struggle,

147
09:53.180 --> 09:55.940
the stronger you get. I'll see you on the next lesson.