1
00:00:00,540 --> 00:00:02,700
As I mentioned, in this section

2
00:00:02,700 --> 00:00:06,030
we're going to be working heavily with the Python turtle module.

3
00:00:06,630 --> 00:00:08,070
And as we've seen before,

4
00:00:08,130 --> 00:00:13,130
this turtle module is essentially a way for us to be able to draw graphics onto

5
00:00:13,380 --> 00:00:14,213
the screen.

6
00:00:14,520 --> 00:00:19,320
And you can imagine it as just this little wandering turtle with a pen on its

7
00:00:19,320 --> 00:00:23,160
back and everywhere it goes, it leaves a trail of line.

8
00:00:23,970 --> 00:00:28,230
So let's take a closer look at the turtle graphics module. Now here,

9
00:00:28,260 --> 00:00:33,240
I've already created a brand new PyCharm project called day-18-start,

10
00:00:33,630 --> 00:00:37,800
and I've created a blank main.py. Inside this project,

11
00:00:37,890 --> 00:00:42,450
we're going to import our turtle. So as you saw from previous lessons,

12
00:00:42,450 --> 00:00:47,070
we can import the turtle module and get hold of the turtle class

13
00:00:47,100 --> 00:00:50,220
by simply saying from the turtle module,

14
00:00:50,520 --> 00:00:55,520
import the Turtle class with the capital T. And now we can work with that class

15
00:00:56,760 --> 00:01:01,230
and we're going to use it to create a new turtle object.

16
00:01:01,680 --> 00:01:03,540
Let's call it timmy_the_turtle,

17
00:01:05,310 --> 00:01:09,210
and this is going to be created from the turtle class.

18
00:01:09,240 --> 00:01:13,410
So this is going to be a new turtle object saved inside this variable.

19
00:01:14,070 --> 00:01:16,680
Now, when we actually run our project,

20
00:01:18,630 --> 00:01:21,450
you'll see a brief flash of a window

21
00:01:21,510 --> 00:01:25,950
and then it will disappear. In order for that window to stay where it is

22
00:01:26,040 --> 00:01:29,430
and only exit when we actually click on the window,

23
00:01:29,730 --> 00:01:33,510
we have to create another object, which we'll call our screen.

24
00:01:34,020 --> 00:01:36,870
And this comes from the total module as well.

25
00:01:36,960 --> 00:01:39,690
And it's a class called Screen.

26
00:01:40,140 --> 00:01:42,720
We also have to import that if we want to use it.

27
00:01:43,260 --> 00:01:45,390
So now that we've got hold of the screen,

28
00:01:45,420 --> 00:01:49,110
we can use one of the functions called exitonclick.

29
00:01:50,520 --> 00:01:52,560
So this way, when we run our code,

30
00:01:52,590 --> 00:01:57,590
we can see our turtle show up in the middle as this little arrow and our window

31
00:01:58,200 --> 00:02:02,490
won't disappear until we click on it. That's what exitonclick does.

32
00:02:02,970 --> 00:02:06,210
So we need to keep this bit of the code at the very bottom.

33
00:02:06,330 --> 00:02:09,810
It has to happen after we've done all this stuff with turtle.

34
00:02:09,930 --> 00:02:13,920
So I'm going to move it to the bottom of the file. Now here's a question.

35
00:02:13,950 --> 00:02:18,850
How do I know how to do that? Is it because I am super experienced 

36
00:02:18,850 --> 00:02:21,450
and I remember everything off the top of my head? No.

37
00:02:21,780 --> 00:02:25,920
Is it because I'm super clever and I can just work things out? No,

38
00:02:26,070 --> 00:02:27,750
that's definitely not the answer.

39
00:02:28,170 --> 00:02:32,730
The way that programmers go about figuring out how to use these modules or

40
00:02:32,730 --> 00:02:36,240
packages is through the use of documentation.

41
00:02:36,570 --> 00:02:41,570
So let's take a look at the turtle graphics documentation.

42
00:02:42,810 --> 00:02:46,530
And this is what the documentation looks like. It's a very,

43
00:02:46,530 --> 00:02:51,530
very long document that goes through all of the things that you can do with this

44
00:02:52,560 --> 00:02:55,260
module. Now, if we scroll down,

45
00:02:55,260 --> 00:03:00,260
you've got a contents page here and you can see the various things you can do

46
00:03:00,550 --> 00:03:05,550
with the turtle. Control it's motion or control the pen or change its visibility

47
00:03:06,910 --> 00:03:11,380
or appearance. Now let's take a look at this shape function,

48
00:03:11,890 --> 00:03:16,240
and you can see the documentation explains to us that this particular function

49
00:03:16,570 --> 00:03:20,800
will set the turtles shape to one with a given name.

50
00:03:21,520 --> 00:03:26,440
And we can choose from the various shapes, including arrow, turtle, circle,

51
00:03:26,450 --> 00:03:29,370
square, triangle, and classic. So 

52
00:03:29,380 --> 00:03:34,380
let's use this knowledge and change timmy_the_turtle to have a different shape.

53
00:03:35,440 --> 00:03:38,050
So inside here, we can put any of those strings.

54
00:03:38,080 --> 00:03:42,490
So I'm going to change it to a turtle shape. This way when I run my code,

55
00:03:42,520 --> 00:03:45,610
you can see an actual turtle appear on the screen.

56
00:03:45,670 --> 00:03:49,510
It just makes it a little bit easier to visualize what's going on with this

57
00:03:49,510 --> 00:03:53,290
module. Now, of course you can change it to any of the other ones,

58
00:03:53,380 --> 00:03:57,610
test it out and see if it works. In an ideal world

59
00:03:57,640 --> 00:04:02,640
you would read through the entire documentation of a module before you start

60
00:04:02,650 --> 00:04:05,170
using it. But of course, in the real world,

61
00:04:05,170 --> 00:04:08,110
we all know that that's not quite possible.

62
00:04:08,680 --> 00:04:13,030
Very often you'll need the help of Stack Overflow. For example,

63
00:04:13,060 --> 00:04:18,060
if we wanted to look at turtle graphics and we wanted to change the shape

64
00:04:19,980 --> 00:04:21,420
of the turtle.

65
00:04:22,500 --> 00:04:26,310
Then you can see that when you actually search for this inside the Stack

66
00:04:26,310 --> 00:04:30,690
Overflow search bar, your results are not very relevant.

67
00:04:31,260 --> 00:04:36,260
But if I put the same query into google.com and at the end I tag on the keyword

68
00:04:37,530 --> 00:04:41,490
Stack Overflow, we get results that are a lot more relevant.

69
00:04:41,610 --> 00:04:43,950
So if we take a look at this post,

70
00:04:44,010 --> 00:04:48,090
you can see that it's starting to talk about this shape method.

71
00:04:48,600 --> 00:04:52,740
And down here, this answer even links to the relevant part of the documentation

72
00:04:53,100 --> 00:04:57,150
which we've already seen previously, this shape method.

73
00:04:57,600 --> 00:05:02,600
The idea is that you might need to Google around to see how to achieve a certain

74
00:05:02,940 --> 00:05:03,960
thing you want to do.

75
00:05:04,410 --> 00:05:07,710
And then once you've seen some of the methods that they mentioned in the

76
00:05:07,710 --> 00:05:08,543
answers,

77
00:05:08,580 --> 00:05:13,470
then go to the documentation and read up on it in order to actually understand

78
00:05:13,470 --> 00:05:18,120
what it does and how to use it. Coming back to our turtle,

79
00:05:18,240 --> 00:05:22,200
what if we wanted to change the color of the turtle? Well,

80
00:05:22,200 --> 00:05:26,550
this should be quite easy. We can see that in the color control section,

81
00:05:26,850 --> 00:05:31,850
we can use this color method to set the pen color and fill color.

82
00:05:32,880 --> 00:05:36,600
So for example, we could say timmy_the_turtle.color,

83
00:05:36,870 --> 00:05:39,180
and we can put in a string here like red.

84
00:05:39,690 --> 00:05:42,000
So now when I run my code,

85
00:05:42,330 --> 00:05:47,330
you can see that my turtle timmy is now red instead of the previous default

86
00:05:48,660 --> 00:05:50,760
color of black. Now,

87
00:05:50,760 --> 00:05:55,140
how do I know which colors I can use other than the ones shown in the example,

88
00:05:55,170 --> 00:05:57,350
red or green? Well,

89
00:05:57,350 --> 00:06:02,350
it says that when we want to change the color using a color string or an RGB

90
00:06:02,690 --> 00:06:07,190
color, then it accepts inputs as in pen color.

91
00:06:07,220 --> 00:06:12,220
So it basically says it's using this method to set the color. And this method

92
00:06:12,560 --> 00:06:16,880
gets the color from a Tk color specifications string.

93
00:06:17,300 --> 00:06:19,700
So let's Google and see what that is.

94
00:06:21,950 --> 00:06:23,300
If we click on the first link,

95
00:06:23,330 --> 00:06:28,330
you can see it takes us to the strings and we've got a whole bunch of names and

96
00:06:28,610 --> 00:06:31,160
their corresponding RGB values.

97
00:06:31,640 --> 00:06:34,610
So what is this Tk exactly?

98
00:06:35,390 --> 00:06:39,170
Tk is short for the module tkinter,

99
00:06:39,200 --> 00:06:40,820
which is the Tk interface.

100
00:06:40,910 --> 00:06:45,200
And this is one of the ways that you can use Python to create a graphical user

101
00:06:45,200 --> 00:06:48,350
interface, which is also known as a GUI.

102
00:06:48,680 --> 00:06:52,520
If you think back to the beginning of computer history,

103
00:06:53,060 --> 00:06:55,340
if you think about the Apple Lisa one,

104
00:06:55,400 --> 00:06:59,270
that was the first computer that had a graphical user interface,

105
00:06:59,270 --> 00:07:01,430
where it had a mouse and you can point and click.

106
00:07:01,820 --> 00:07:04,100
And this was huge back in the day.

107
00:07:04,640 --> 00:07:08,480
But before then we had text interfaces like MS-

108
00:07:08,480 --> 00:07:12,170
DOS or like our console when we're using Python.

109
00:07:12,770 --> 00:07:17,540
The text interfaces accept text commands and the graphical user interfaces can

110
00:07:17,540 --> 00:07:22,540
show images and allows you to click and drag and do all of those things by

111
00:07:23,150 --> 00:07:28,150
looking instead of just typing commands. And tkinter is what the turtle module

112
00:07:29,810 --> 00:07:34,460
actually relies on under the hood to create these graphics,

113
00:07:34,700 --> 00:07:36,860
like our turtle showing up here.

114
00:07:37,790 --> 00:07:40,520
So now that we've seen where these names come from,

115
00:07:40,970 --> 00:07:45,970
a much easier way of using these colors is through a page where they have all

116
00:07:46,550 --> 00:07:50,030
been rendered on screen. So in the course resources,

117
00:07:50,060 --> 00:07:54,680
we link to this page where you can see all the colors next to their names,

118
00:07:54,920 --> 00:07:57,290
and you can pick and choose whichever one you want.

119
00:07:57,980 --> 00:08:02,980
Have a go at changing the color of the turtle by using one of these colors that

120
00:08:03,050 --> 00:08:04,040
you see on screen.

121
00:08:05,390 --> 00:08:09,710
Now that we've seen how we can create a new turtle and change its appearance,

122
00:08:09,980 --> 00:08:14,750
it's time to look at how we can make it do certain things. In the documentation,

123
00:08:14,780 --> 00:08:18,620
you can see there's a whole bunch of things you can do in terms of moving and

124
00:08:18,620 --> 00:08:23,600
drawing. A very simple one would be to move it forward or to move it backwards.

125
00:08:26,570 --> 00:08:30,590
So we can tell it to move forwards by a hundred paces.

126
00:08:31,310 --> 00:08:33,980
And when we rerun the code, this is what we see.

127
00:08:35,059 --> 00:08:39,110
So we can get it to move forwards, move backwards, and also move left

128
00:08:39,110 --> 00:08:43,400
and right. So here, the input that it takes is an angle,

129
00:08:43,490 --> 00:08:48,490
a number between zero and 360 to tell it what angle

130
00:08:48,740 --> 00:08:51,530
to turn right or to turn left. For example,

131
00:08:51,560 --> 00:08:56,100
currently our turtle is facing East. So if we wanted to face

132
00:08:56,310 --> 00:08:56,960
South,

133
00:08:56,960 --> 00:09:01,670
then we have to get it to turn right by 90 degrees. So we can say,

134
00:09:01,910 --> 00:09:05,090
timmy_the_turtle.right and then we put 90 in here.

135
00:09:06,380 --> 00:09:10,400
And that's what it looks like. In the upcoming lessons

136
00:09:10,430 --> 00:09:13,820
we've got a whole bunch of turtle challenges for you

137
00:09:14,300 --> 00:09:18,290
that's going to test not only your knowledge of programming that you've learned so

138
00:09:18,290 --> 00:09:23,150
far, but also how well you can read and understand documentation.

139
00:09:23,630 --> 00:09:28,630
This is a key skill to acquire as a seasoned developer because after all,

140
00:09:29,390 --> 00:09:32,030
we can't always rely on other people telling us what to do.

141
00:09:32,060 --> 00:09:34,070
We have to be able to get it from the source.

142
00:09:34,490 --> 00:09:38,420
So make sure that you've got the documentation pulled up and have a glance

143
00:09:38,450 --> 00:09:39,283
through it.

144
00:09:39,320 --> 00:09:42,950
Then you can head over to the next lesson and start tackling some of these

145
00:09:42,950 --> 00:09:43,783
challenges.

146
00:09:44,390 --> 00:09:48,770
Now remember that some of these challenges can be quite a bit of a struggle,

147
00:09:49,130 --> 00:09:53,180
but keep in mind that the struggle is good. The more that you struggle,

148
00:09:53,180 --> 00:09:55,940
the stronger you get. I'll see you on the next lesson.

