WEBVTT

0
00:00.030 --> 00:01.140
In the last lesson,

1
00:01.170 --> 00:06.170
we figured out how we can get our turtle race up and running and get our turtles

2
00:06.930 --> 00:09.270
at the starting positions. Now,

3
00:09.300 --> 00:13.980
the next job we have is to figure out how we can get these turtles to start

4
00:14.010 --> 00:18.150
moving. The movement is going to have to be randomized

5
00:18.540 --> 00:23.540
and each step they take will have to be a random number. That way they don't go

6
00:23.580 --> 00:24.690
straight to the end

7
00:24.960 --> 00:28.620
and we end up with a terrible gaming experience.

8
00:29.190 --> 00:33.360
So what we're going to do is we're going to create a random number,

9
00:33.390 --> 00:36.000
let's say between 0 and 10,

10
00:36.450 --> 00:40.860
and then we're going to tell the turtle to go forward by that random amount.

11
00:40.860 --> 00:43.230
So it could be anywhere between 0 and 10.

12
00:43.950 --> 00:47.490
And then we're going to get this to repeat itself many, many times.

13
00:48.840 --> 00:49.740
As you might've guessed,

14
00:49.770 --> 00:52.740
this is probably going to involve some sort of while loop.

15
00:53.160 --> 00:58.160
So let's say we start out with a variable called is_race_on and it starts out as

16
00:59.460 --> 01:00.293
false.

17
01:00.570 --> 01:05.570
But then once the user has made their bet and we can check to see

18
01:05.880 --> 01:10.170
user_bet exists, then in that case

19
01:10.230 --> 01:13.800
we're going to switch that is_race_on to true.

20
01:16.050 --> 01:20.430
And then we can use our is_race_on inside our while loop.

21
01:20.910 --> 01:25.910
Now this way we prevent our while loop from starting up while the user is still

22
01:25.980 --> 01:28.920
deciding on which turtle they're going to bet on.

23
01:29.160 --> 01:31.020
So it doesn't start prematurely.

24
01:31.710 --> 01:36.480
Now when the user has made their bet and the race is now on, well

25
01:36.480 --> 01:40.080
now we're going to need to get hold of a random number.

26
01:43.130 --> 01:48.130
We're going to use the random module to get hold of a random int between the

27
01:49.430 --> 01:52.220
range of 0 and 10.

28
01:52.610 --> 01:55.250
Now randint is actually inclusive,

29
01:55.250 --> 01:59.060
so the random number could be zero all the way up to 10.

30
01:59.780 --> 02:03.740
So now we're gonna call this the rand_distance,

31
02:04.520 --> 02:09.520
and this is going to be the distance that one of our turtles is going to move

32
02:09.950 --> 02:10.580
forwards by.

33
02:10.580 --> 02:15.580
So we want some sort of turtle.forward and the distance that it goes forward

34
02:18.350 --> 02:20.030
is the random distance.

35
02:20.750 --> 02:23.720
Now we have lots of turtles that we've created,

36
02:24.050 --> 02:26.240
but we kind of haven't really done anything with them.

37
02:26.690 --> 02:31.280
What we want to do instead is to have some sort of list of turtle.

38
02:31.310 --> 02:33.380
So we'll call all_turtles,

39
02:34.070 --> 02:36.350
and this list starts out as empty.

40
02:36.740 --> 02:39.290
But every single time we create a new turtle,

41
02:39.560 --> 02:43.970
then we can append that turtle to this list. So at this stage,

42
02:44.000 --> 02:47.870
it doesn't really make sense to call all the turtles tim anymore, right?

43
02:47.870 --> 02:50.900
Because they're all going to be individual instances.

44
02:51.470 --> 02:56.470
So let's go ahead and refactor and rename this to a new_turtle.

45
02:58.900 --> 03:03.900
And now what's happening in our for loop is we're creating a new turtle and

46
03:04.060 --> 03:09.060
each of those new turtles are going to be appended to our all_turtles list.

47
03:11.320 --> 03:15.730
So now we're going to have a list of multiple turtle instances,

48
03:16.270 --> 03:20.260
and each of those instances is going to have a different state.

49
03:20.530 --> 03:21.490
So for example,

50
03:21.520 --> 03:25.780
they already start out with a different Y-position and a different color.

51
03:26.320 --> 03:27.340
But later on,

52
03:27.400 --> 03:31.600
we're also going to get them to move forwards by a different amount.

53
03:32.170 --> 03:35.560
So we can loop through our list of turtles,

54
03:35.590 --> 03:38.080
so for turtle in all_turtles,

55
03:40.980 --> 03:45.980
let's go ahead and create a random distance for each of them and then move that

56
03:46.740 --> 03:49.320
turtle forward by the random distance.

57
03:49.860 --> 03:52.050
So now if we run our code

58
03:53.670 --> 03:55.200
and see it in action,

59
03:55.620 --> 03:59.580
you can see that once all of the turtles have gone into their starting

60
03:59.580 --> 04:04.560
positions, they stop creeping along each time by a different amount.

61
04:08.190 --> 04:12.990
Now, however, our turtles are actually going to keep going until forever.

62
04:13.110 --> 04:16.140
It's basically a loop that's never going to stop.

63
04:16.620 --> 04:20.700
So let's think about what the stopping conditions should be. Well,

64
04:20.730 --> 04:25.730
once any of these turtles have actually reached the end of the screen

65
04:26.730 --> 04:31.170
which is the X-value of +250,

66
04:31.170 --> 04:35.820
once they've reached this line, then that's kind of the end of the race,

67
04:35.850 --> 04:36.683
right?

68
04:36.990 --> 04:41.490
But a turtle object is a 40 by 40 object.

69
04:41.910 --> 04:45.390
So if we're only detecting for the 250,

70
04:45.660 --> 04:48.390
then our turtle would have already overshot.

71
04:48.480 --> 04:53.480
So instead, we want to detect when the turtle has reached the 230 mark. Back in our

72
04:55.230 --> 04:56.700
code in our for-loop,

73
04:57.060 --> 05:01.620
we can check to see if the turtle that we're currently looping through,

74
05:02.190 --> 05:07.190
if it's X-coordinate is greater than 

75
05:09.210 --> 05:13.710
230. Well, in this case, it's basically won.

76
05:14.190 --> 05:15.510
Now at this stage,

77
05:15.600 --> 05:19.890
if we print this current turtle's color,

78
05:20.340 --> 05:23.370
then you'll actually see something a little bit strange.

79
05:25.530 --> 05:27.360
You can see that firstly,

80
05:27.360 --> 05:31.890
it prints multiple values because the while loop is still continuing and the

81
05:31.890 --> 05:34.020
other turtles continue to go forward.

82
05:34.470 --> 05:37.770
But the first value is going to be the turtle that won.

83
05:38.340 --> 05:41.370
And you can see it prints yellow and yellow.

84
05:41.880 --> 05:45.840
So what's going on here? Well, if we look at this color method,

85
05:45.930 --> 05:50.790
you can see that you can either use it to set the pen color and fill color,

86
05:51.090 --> 05:54.720
or it will return the pen color and fill color.

87
05:55.170 --> 05:56.940
So when it prints out two colors,

88
05:57.650 --> 06:01.430
the first one is going to be the pen color and the second is going to be the

89
06:01.430 --> 06:05.720
fill color. So if we only want to know the pen color,

90
06:06.050 --> 06:10.910
then we can just get hold of the turtle.pencolor.

91
06:10.970 --> 06:15.290
This is going to be the color of the body of the turtle. So in our code,

92
06:15.320 --> 06:20.320
we can say that the winning color is equal to the current turtle.pencolor.

93
06:25.100 --> 06:30.100
And then we can now check to see if the winning color is equal to the user's

94
06:31.100 --> 06:35.420
bet, because if this is the case, well then the user has won, right?

95
06:35.450 --> 06:38.090
So we can tell them you've won.

96
06:38.330 --> 06:41.300
And then we can tell them the color of the winning turtle.

97
06:44.200 --> 06:45.070
Now, however,

98
06:45.070 --> 06:49.480
if this is false or rather in the else statement,

99
06:49.870 --> 06:53.140
well, in that case, we're going to have to print the same thing

100
06:53.470 --> 06:56.770
but now we're going to tell them that they've lost.

101
06:59.020 --> 06:59.410
<v 1>Yeah.</v>

102
06:59.410 --> 07:03.790
<v 0>Finally, when the first turtle reaches the X-coordinate,</v>

103
07:03.790 --> 07:06.160
when this first color gets printed,

104
07:06.280 --> 07:09.160
we want to stop the while loop. This way

105
07:09.160 --> 07:12.490
the remaining turtles don't continue to nudge forward

106
07:12.790 --> 07:17.170
and we get end up with lots of winners like this. So inside this

107
07:17.200 --> 07:21.520
if statement, when the turtle's X-coordinate has surpassed 230,

108
07:21.820 --> 07:25.390
we're going to change the is_race_on to false.

109
07:26.020 --> 07:30.190
Now we're ready to test out our code. And again,

110
07:30.190 --> 07:34.390
I'm going to bet on the red turtle and I'm going to speed up the playback speed

111
07:34.420 --> 07:36.640
of this race when I'm editing this video.

112
07:37.240 --> 07:42.240
And now the game has ended and you can see that the blue turtle is the one that

113
07:42.760 --> 07:44.920
first got its head across the finish line.

114
07:45.460 --> 07:50.260
And it tells me just as much. I've lost and the blue turtle is the winner.

115
07:51.070 --> 07:56.070
So we've now used Python turtle to build a very simple turtle racing game.

116
07:56.680 --> 08:01.680
And we're starting to use these X and Y positions in the turtle coordinate

117
08:02.350 --> 08:06.520
system. Now, these coordinates take a little bit of getting used to,

118
08:06.850 --> 08:11.850
especially thinking about things such as if a turtle is 40 pixels wide,

119
08:12.160 --> 08:14.770
then in order to get the turtle to the starting line

120
08:14.980 --> 08:19.300
we have to subtract half of the width of the turtle, which is 20.

121
08:19.540 --> 08:23.350
So then 250 minus 20 becomes 230.

122
08:23.680 --> 08:26.740
And it's things like this that takes a little bit of getting used to.

123
08:27.220 --> 08:31.480
So what I really recommend is to have a play around with these values,

124
08:31.750 --> 08:36.430
make it smaller, or make it larger, change the size of the screen.

125
08:36.730 --> 08:41.050
And just until you're really familiar with how the coordinate system works

126
08:41.080 --> 08:44.620
before you proceed to the next lesson, because as you keep going,

127
08:44.620 --> 08:48.160
I'm going to presume that you've already had a play around with it and you know

128
08:48.160 --> 08:50.560
exactly how it works. Now,

129
08:50.590 --> 08:55.150
the other thing that we saw a lot of in this project is this concept of having

130
08:55.380 --> 08:59.700
multiple objects created from the same class. So in this case,

131
08:59.700 --> 09:04.590
we're using the turtle class to construct a turtle object called Timmy,

132
09:04.860 --> 09:09.860
but we've also got a separate object called Tommy. And Timmy and Tommy are both

133
09:10.020 --> 09:14.900
Tuttle objects, but they're different instances. So they can act independently,

134
09:14.900 --> 09:18.110
they can have different appearances, they can have different colors,

135
09:18.110 --> 09:21.380
different attributes, and also a different speed of movement.

136
09:21.830 --> 09:26.830
So we've seen in our turtle race that each of these instances can act of their

137
09:27.140 --> 09:29.690
own accord and have different state.

138
09:29.930 --> 09:33.860
And that's shown in the different speed of movement and this capability of

139
09:33.860 --> 09:37.580
creating multiple objects which can act and behave independently

140
09:37.880 --> 09:42.590
is really the secret to why Object Oriented Programming can be so

141
09:42.590 --> 09:43.423
powerful.

142
09:43.550 --> 09:48.050
We can create so many of these objects and get them to do our bidding in various

143
09:48.050 --> 09:50.480
parts of our program. And in the coming lessons

144
09:50.510 --> 09:52.970
as we build more and more complex projects,

145
09:53.270 --> 09:58.130
you'll see this concept of having multiple instances with different state being

146
09:58.130 --> 10:02.060
shown again and again. And we're going to come to see and enjoy the benefits.