1
00:00:00,060 --> 00:00:04,470
All right guys. In this lesson, we're going to be building out our turtle race.

2
00:00:04,830 --> 00:00:06,060
And through building this race,

3
00:00:06,090 --> 00:00:11,090
we're going to get more familiar with these concepts of state and instances.

4
00:00:11,820 --> 00:00:15,720
And that's because we need to create multiple turtles to join our race.

5
00:00:16,110 --> 00:00:18,390
This is what we're aiming for. In the beginning,

6
00:00:18,450 --> 00:00:23,450
there's going to be a little popup that will ask us to bet who will win the

7
00:00:23,610 --> 00:00:26,640
race. And it asked me to choose a color.

8
00:00:27,000 --> 00:00:29,370
Now the turtles are in the color of the rainbow,

9
00:00:29,370 --> 00:00:33,060
so I'm just going to pick the red one. Once I hit okay,

10
00:00:33,060 --> 00:00:37,830
we have all of our turtles lined up in the starting position and they will start

11
00:00:37,860 --> 00:00:42,450
making random steps towards the right edge of the screen. Now,

12
00:00:42,450 --> 00:00:46,620
the first hurdle that reaches past the right edge of the screen is going to be

13
00:00:46,620 --> 00:00:47,453
the winner.

14
00:00:47,670 --> 00:00:52,670
And at the moment it looks like the green turtle is probably going to get there

15
00:00:52,770 --> 00:00:57,750
first. There we go. So as soon as a turtle crosses the finish line,

16
00:00:58,920 --> 00:01:03,390
then it will print out a line telling us whether if we won or whether

17
00:01:03,390 --> 00:01:06,990
if we lost our bets and which turtle won the game.

18
00:01:07,200 --> 00:01:08,430
So this is the goal.

19
00:01:08,970 --> 00:01:13,800
Now I'm going to get started by deleting all of the code inside our previous

20
00:01:13,800 --> 00:01:17,220
Etch-A-Sketch project other than these few lines.

21
00:01:17,280 --> 00:01:19,890
So I've got Tim my turtle, screen

22
00:01:19,890 --> 00:01:24,890
which is a screen object that is going to hold the screen until I click on it so

23
00:01:26,130 --> 00:01:30,150
that I can see what's going on. Now, because in this game

24
00:01:30,180 --> 00:01:35,180
the dimensions of the screen or of the window is really crucial,

25
00:01:35,940 --> 00:01:40,020
I'm not just going to get the default screen size show up. Instead

26
00:01:40,020 --> 00:01:44,430
I'm actually going to use one of the methods that is in the screen object

27
00:01:44,460 --> 00:01:48,060
which is called setup. And setup, as you can see,

28
00:01:48,060 --> 00:01:52,290
allows me to set up the width and the height of this window

29
00:01:52,290 --> 00:01:53,310
that's going to show up.

30
00:01:54,510 --> 00:01:59,510
So I'm going to set the width of my screen to 500 and the height to 400.

31
00:02:00,360 --> 00:02:05,000
So I should end up with a 500 pixel wide, 400 pixel high screen.

32
00:02:05,480 --> 00:02:09,710
And once I hit run, you can see this is what it gets resized to.

33
00:02:10,370 --> 00:02:13,700
Now, if you came across this code and it was somebody else's code,

34
00:02:14,030 --> 00:02:17,960
then these numbers and their positions don't actually make a lot of sense,

35
00:02:17,990 --> 00:02:21,230
which one's the height, which one's the width. So in these situations,

36
00:02:21,260 --> 00:02:25,880
I recommend using keyword arguments rather than positional arguments. Oh,

37
00:02:25,910 --> 00:02:29,000
let's write out the parameters explicitly. Our width

38
00:02:29,030 --> 00:02:32,540
to be 500 and our height to be 400. This way

39
00:02:32,540 --> 00:02:34,880
when somebody else comes along and reads your code,

40
00:02:34,910 --> 00:02:38,990
it's a lot easier to understand rather than just 500 and 400,

41
00:02:38,990 --> 00:02:40,880
cause it could be both ways.

42
00:02:41,630 --> 00:02:44,120
So now that we've got our screen set up,

43
00:02:44,180 --> 00:02:49,180
the next thing I want to do is to bring up that popup and ask the user to make a

44
00:02:49,280 --> 00:02:53,510
bet. Now, if we take a look at the Python documentation,

45
00:02:53,960 --> 00:02:56,750
um, where we've been spending a lot of time recently,

46
00:02:57,020 --> 00:03:00,910
you can see that there is a method on the screen

47
00:03:00,970 --> 00:03:03,670
which is called textinput.

48
00:03:04,060 --> 00:03:09,060
And this is going to show up a popup window and it allow the user to look at the

49
00:03:09,940 --> 00:03:13,960
prompt and the title and to enter a piece of text.

50
00:03:14,410 --> 00:03:18,100
And if you want the user to enter a number, then you would use numinput.

51
00:03:18,550 --> 00:03:22,570
So let's go ahead and add that here. So it will be a method from screen

52
00:03:22,600 --> 00:03:26,800
which is called textinput and then it's got two parameters.

53
00:03:26,860 --> 00:03:31,000
One is the title which in my case

54
00:03:31,000 --> 00:03:33,220
I'm just going to say 'Make your bet'.

55
00:03:33,850 --> 00:03:38,710
And then the second parameter is going to be the prompt. And in my case,

56
00:03:38,740 --> 00:03:40,750
I will probably say, um,

57
00:03:40,870 --> 00:03:45,610
which turtle win the race and tell them to enter a color.

58
00:03:46,120 --> 00:03:48,490
Now, this works really similarly to our input

59
00:03:48,490 --> 00:03:53,350
which we've been really used to because it will return the string and we can

60
00:03:53,350 --> 00:03:56,170
catch that string inside a variable.

61
00:03:56,200 --> 00:04:01,200
So we'll call it user_bet and we'll set it to equal the output from this

62
00:04:02,620 --> 00:04:03,453
method.

63
00:04:06,870 --> 00:04:09,150
Now let's give this a shot.

64
00:04:09,570 --> 00:04:14,130
Let's run the code and then let's enter a color, hit okay.

65
00:04:14,190 --> 00:04:16,350
And if we take a look in our console,

66
00:04:16,589 --> 00:04:20,550
you can see that that bet string is printed in here.

67
00:04:21,540 --> 00:04:25,680
So now the next thing we're going to do is we need to do something with our

68
00:04:25,680 --> 00:04:28,200
turtle. I'm going to move it down a little bit.

69
00:04:28,590 --> 00:04:33,590
And what I want my turtle to do is I want it to go to the start of the line.

70
00:04:35,130 --> 00:04:39,810
So the very left edge of my screen. Now I could,

71
00:04:39,810 --> 00:04:43,890
of course, just say tim.backward by, um,

72
00:04:43,920 --> 00:04:45,330
however many paces,

73
00:04:45,750 --> 00:04:50,520
but thinking forward and knowing that I'm going to have six or seven of these

74
00:04:50,520 --> 00:04:51,240
turtles,

75
00:04:51,240 --> 00:04:54,720
I can't just tell them all to go backwards because they're going to go to the

76
00:04:54,720 --> 00:04:59,660
same position. Instead, we're going to use a method that the turtle has

77
00:04:59,710 --> 00:05:01,110
which is called goto.

78
00:05:01,650 --> 00:05:06,210
And this allows us to define an X value and a Y value.

79
00:05:06,600 --> 00:05:10,650
But how do we know what X and Y values to give it? Well,

80
00:05:10,650 --> 00:05:14,640
we first have to understand how the Python turtle coordinate system works.

81
00:05:14,970 --> 00:05:19,970
So if you imagine your program window as a graph where the center of it is at

82
00:05:20,460 --> 00:05:22,740
the coordinate (0, 0). Well

83
00:05:22,740 --> 00:05:27,330
then if you have a window that has a height of 400,

84
00:05:27,690 --> 00:05:32,690
then that graph will have a Y-axis that extends from the center at zero all the

85
00:05:34,590 --> 00:05:37,740
way up to the top edge and then from the middle

86
00:05:37,740 --> 00:05:42,630
it goes down to the very bottom edge, which is going to be -200.

87
00:05:42,840 --> 00:05:47,840
So 200 plus 200 makes up 400 and the same happens with the X-axis.

88
00:05:49,500 --> 00:05:52,530
So if the width of the screen is 500, then

89
00:05:52,750 --> 00:05:57,750
the X-axis goes from the center 0 to positive 250,

90
00:05:58,310 --> 00:06:03,050
so half of 500. And then from zero to -250.

91
00:06:03,470 --> 00:06:07,310
So here's a question. Let's say we wanted to move our turtle here.

92
00:06:07,430 --> 00:06:10,370
What do you think would be the X and Y coordinate of that point?

93
00:06:13,210 --> 00:06:14,350
Okay. So let's say

94
00:06:14,460 --> 00:06:19,460
that this is roughly halfway on the X-axis and about halfway on the Y-axis.

95
00:06:21,250 --> 00:06:26,250
Then half of 250 is 125 and half of 200 is 100.

96
00:06:28,300 --> 00:06:33,300
So at this point would have the coordinate of 125 by 100.

97
00:06:35,500 --> 00:06:40,210
If we wanted our turtle to go all the way to the left side of the screen

98
00:06:40,420 --> 00:06:44,020
say maybe starting over here or starting over here, well,

99
00:06:44,020 --> 00:06:45,520
then we would have to use that

100
00:06:45,550 --> 00:06:50,550
goto method and then specify a X-value and a Y-value. Following the logic of the

101
00:06:54,700 --> 00:06:59,560
total coordinate system then in order to move our turtle to the very left edge,

102
00:06:59,800 --> 00:07:03,520
then we should supply an X-value of minus 250

103
00:07:03,610 --> 00:07:05,770
which is half of the width of 500.

104
00:07:06,400 --> 00:07:11,400
Now the Y-value will determine where on the Y-axis our turtle moves to.

105
00:07:12,850 --> 00:07:14,500
So if we had it at zero

106
00:07:14,500 --> 00:07:18,730
then it would basically just go straight backwards and it would not go up or

107
00:07:18,730 --> 00:07:21,970
down. But let's say that we put it at, um,

108
00:07:22,060 --> 00:07:26,260
-100. So now if I run this code,

109
00:07:26,920 --> 00:07:29,170
you can see that it goes in the right direction,

110
00:07:29,380 --> 00:07:31,180
but it's actually gone off the screen.

111
00:07:31,510 --> 00:07:36,510
So what's happened here is that -250 is on the very,

112
00:07:36,730 --> 00:07:38,530
very edge of the window.

113
00:07:38,890 --> 00:07:42,910
And once our turtle moves over there and the arrow is at the back,

114
00:07:43,150 --> 00:07:45,160
then you can't actually see it at all.

115
00:07:45,400 --> 00:07:50,320
So let's try changing that X-value and shifting our turtle a little bit more to

116
00:07:50,320 --> 00:07:55,270
the right. Then you can see it moves to pretty much the start of the window.

117
00:07:55,840 --> 00:08:00,840
So I recommend using this goto method and trying out some different numbers

118
00:08:02,170 --> 00:08:04,570
just to see where it ends up on the screen.

119
00:08:05,080 --> 00:08:07,300
And once you put in some different numbers in here,

120
00:08:07,510 --> 00:08:11,350
you'll start to get the hang of how this coordinate system actually works.

121
00:08:11,740 --> 00:08:15,280
But just remember that the X-axis is along the horizontal.

122
00:08:15,580 --> 00:08:18,790
It goes from zero to positive and zero to negative.

123
00:08:19,120 --> 00:08:21,790
And then the Y-axis is along the vertical,

124
00:08:21,820 --> 00:08:24,940
going from zero to positive and zero to negative.

125
00:08:25,930 --> 00:08:30,930
So we want to get rid of this line and we don't actually want our turtle to draw

126
00:08:31,660 --> 00:08:36,549
at all. So to do that, we're going to do tim.penup,

127
00:08:37,000 --> 00:08:40,299
and we're actually never going to put the pen down because we're going to be

128
00:08:40,299 --> 00:08:42,130
moving the turtle itself.

129
00:08:42,640 --> 00:08:46,360
And the other thing is that it would be really nice if instead of an arrow,

130
00:08:46,360 --> 00:08:48,250
we actually got a turtle.

131
00:08:48,640 --> 00:08:53,640
So we can of course say tim.shape and set it to a turtle shape,

132
00:08:54,960 --> 00:08:59,370
but here's an even easier way. When we create a new turtle,

133
00:08:59,700 --> 00:09:01,770
look at the prompt that it's giving us.

134
00:09:02,040 --> 00:09:07,040
It's actually giving us a way to initialize a new turtle object already with a

135
00:09:08,730 --> 00:09:09,720
shape set up.

136
00:09:10,230 --> 00:09:14,130
Now this shape is set to have a default value

137
00:09:14,160 --> 00:09:15,600
which is basically the arrow.

138
00:09:16,020 --> 00:09:21,020
But we can also specify the shape and give it the turtle shape to begin with.

139
00:09:23,580 --> 00:09:27,570
Now, when I run the code you can see that firstly,

140
00:09:27,990 --> 00:09:32,990
I get a turtle shape and then the pen is up and I'm not drawing

141
00:09:33,120 --> 00:09:37,980
and then it moves to this place on the graph; -230, -100.

142
00:09:38,880 --> 00:09:43,880
So now the next thing we want to do is to be able to create lots of turtles,

143
00:09:44,430 --> 00:09:45,263
right?

144
00:09:45,420 --> 00:09:50,420
What if we had all of the colors in the rainbow and we create a turtle for each

145
00:09:51,420 --> 00:09:52,253
color?

146
00:09:52,290 --> 00:09:56,580
So there's six colors in this list and these of course,

147
00:09:56,670 --> 00:10:00,600
correspond to the colors which turtle will recognize.

148
00:10:01,050 --> 00:10:02,760
So here's a challenge for you.

149
00:10:03,330 --> 00:10:06,360
I want you to create six turtles,

150
00:10:06,840 --> 00:10:10,050
one for each of the colors in this list of colors.

151
00:10:10,320 --> 00:10:15,320
You're aiming for the turtles to all go to the starting line in a distribution

152
00:10:15,960 --> 00:10:19,350
that looks something like this. It doesn't have to be precise,

153
00:10:19,380 --> 00:10:21,150
you don't have to get it perfectly right.

154
00:10:21,480 --> 00:10:25,980
But just make sure that they're sort of evenly spaced out and they are at the

155
00:10:25,980 --> 00:10:30,570
starting line. So this is what you're aiming for; six turtles, six colors,

156
00:10:30,600 --> 00:10:34,650
all starting at the starting point along a different point on the Y axis.

157
00:10:35,340 --> 00:10:36,780
Pause the video and give that a go.

158
00:10:39,060 --> 00:10:41,610
So we know that in order to create lots of turtles,

159
00:10:41,670 --> 00:10:43,920
we're going to need some sort of a loop.

160
00:10:44,370 --> 00:10:49,370
So let's go ahead and use a for loop to say turtle_index

161
00:10:50,130 --> 00:10:55,130
and we're going to create a range to specify how many turtles we need.

162
00:10:56,730 --> 00:11:00,900
Our range function will go from 0 to 6.

163
00:11:01,380 --> 00:11:05,880
And remember that the range function actually doesn't include the number six,

164
00:11:05,910 --> 00:11:10,890
so it'll create a range from 0 to 5. And once we've done that,

165
00:11:10,890 --> 00:11:15,890
then we can indent this block of code so that we create six turtles.

166
00:11:17,160 --> 00:11:19,650
But at the moment, they're all going to the same position.

167
00:11:20,790 --> 00:11:23,220
So how can we change this? Well,

168
00:11:23,580 --> 00:11:27,900
we want the X-position to always be the same for all the turtles,

169
00:11:27,960 --> 00:11:31,260
because the X-position is along the horizontal axis

170
00:11:31,410 --> 00:11:34,680
and we want all the turtles to start at the starting point.

171
00:11:35,010 --> 00:11:39,780
Nobody gets a headstart. But the Y-position is the thing that we want to change.

172
00:11:40,290 --> 00:11:45,290
A really simple way of doing this is to simply create a list of Y-positions

173
00:11:46,650 --> 00:11:50,400
and you can work out some sort of reasonable position that you want to take.

174
00:11:50,670 --> 00:11:55,660
So let's say we start out at -70 and then we just increased by 30 each

175
00:11:55,660 --> 00:11:56,380
time.

176
00:11:56,380 --> 00:12:01,380
So then that becomes -40 and then -10 and then 20,

177
00:12:01,720 --> 00:12:04,000
50 and 80.

178
00:12:04,060 --> 00:12:08,410
So it's roughly distributed somewhere along the middle. Now,

179
00:12:08,410 --> 00:12:11,380
instead of using this Y-position

180
00:12:11,380 --> 00:12:13,630
which is hardcoded -100,

181
00:12:14,020 --> 00:12:19,020
we're going to take the Y-positions and then pass in our turtle_index like

182
00:12:19,080 --> 00:12:21,900
this. So now when we rerun this code,

183
00:12:21,960 --> 00:12:24,270
you can see that for each of the turtles

184
00:12:24,300 --> 00:12:27,060
they're all going to get an individual Y-position

185
00:12:27,450 --> 00:12:30,660
and they're all separated by 30 in distance.

186
00:12:31,080 --> 00:12:34,650
Now we've got our turtles neatly lined up at the starting point.

187
00:12:34,950 --> 00:12:37,530
The next thing is to give them a different color.

188
00:12:37,650 --> 00:12:42,030
So we're going to use the list of colors and then picking out the color using

189
00:12:42,090 --> 00:12:43,440
the turtle_index.

190
00:12:43,770 --> 00:12:48,770
So now we should get some multicolored turtles and it will be much easier to bet

191
00:12:49,830 --> 00:12:53,280
on a turtle. So now that we have our turtle race

192
00:12:53,340 --> 00:12:55,020
all set up and ready to go,

193
00:12:55,350 --> 00:12:58,890
the next step is to actually get the turtles to start moving.

194
00:12:59,310 --> 00:13:00,750
But before we can do that,

195
00:13:00,840 --> 00:13:05,840
I really want you to get a good grasp of how the coordinate system works in

196
00:13:05,940 --> 00:13:08,220
turtle. So in the next lesson,

197
00:13:08,310 --> 00:13:11,850
I've got a quick quiz for you just to make sure that you really understand

198
00:13:11,880 --> 00:13:12,930
what's going on here.

