1
00:00:00,030 --> 00:00:01,140
In the last lesson,

2
00:00:01,170 --> 00:00:06,170
we figured out how we can get our turtle race up and running and get our turtles

3
00:00:06,930 --> 00:00:09,270
at the starting positions. Now,

4
00:00:09,300 --> 00:00:13,980
the next job we have is to figure out how we can get these turtles to start

5
00:00:14,010 --> 00:00:18,150
moving. The movement is going to have to be randomized

6
00:00:18,540 --> 00:00:23,540
and each step they take will have to be a random number. That way they don't go

7
00:00:23,580 --> 00:00:24,690
straight to the end

8
00:00:24,960 --> 00:00:28,620
and we end up with a terrible gaming experience.

9
00:00:29,190 --> 00:00:33,360
So what we're going to do is we're going to create a random number,

10
00:00:33,390 --> 00:00:36,000
let's say between 0 and 10,

11
00:00:36,450 --> 00:00:40,860
and then we're going to tell the turtle to go forward by that random amount.

12
00:00:40,860 --> 00:00:43,230
So it could be anywhere between 0 and 10.

13
00:00:43,950 --> 00:00:47,490
And then we're going to get this to repeat itself many, many times.

14
00:00:48,840 --> 00:00:49,740
As you might've guessed,

15
00:00:49,770 --> 00:00:52,740
this is probably going to involve some sort of while loop.

16
00:00:53,160 --> 00:00:58,160
So let's say we start out with a variable called is_race_on and it starts out as

17
00:00:59,460 --> 00:01:00,293
false.

18
00:01:00,570 --> 00:01:05,570
But then once the user has made their bet and we can check to see

19
00:01:05,880 --> 00:01:10,170
user_bet exists, then in that case

20
00:01:10,230 --> 00:01:13,800
we're going to switch that is_race_on to true.

21
00:01:16,050 --> 00:01:20,430
And then we can use our is_race_on inside our while loop.

22
00:01:20,910 --> 00:01:25,910
Now this way we prevent our while loop from starting up while the user is still

23
00:01:25,980 --> 00:01:28,920
deciding on which turtle they're going to bet on.

24
00:01:29,160 --> 00:01:31,020
So it doesn't start prematurely.

25
00:01:31,710 --> 00:01:36,480
Now when the user has made their bet and the race is now on, well

26
00:01:36,480 --> 00:01:40,080
now we're going to need to get hold of a random number.

27
00:01:43,130 --> 00:01:48,130
We're going to use the random module to get hold of a random int between the

28
00:01:49,430 --> 00:01:52,220
range of 0 and 10.

29
00:01:52,610 --> 00:01:55,250
Now randint is actually inclusive,

30
00:01:55,250 --> 00:01:59,060
so the random number could be zero all the way up to 10.

31
00:01:59,780 --> 00:02:03,740
So now we're gonna call this the rand_distance,

32
00:02:04,520 --> 00:02:09,520
and this is going to be the distance that one of our turtles is going to move

33
00:02:09,949 --> 00:02:10,580
forwards by.

34
00:02:10,580 --> 00:02:15,580
So we want some sort of turtle.forward and the distance that it goes forward

35
00:02:18,350 --> 00:02:20,030
is the random distance.

36
00:02:20,750 --> 00:02:23,720
Now we have lots of turtles that we've created,

37
00:02:24,050 --> 00:02:26,240
but we kind of haven't really done anything with them.

38
00:02:26,690 --> 00:02:31,280
What we want to do instead is to have some sort of list of turtle.

39
00:02:31,310 --> 00:02:33,380
So we'll call all_turtles,

40
00:02:34,070 --> 00:02:36,350
and this list starts out as empty.

41
00:02:36,740 --> 00:02:39,290
But every single time we create a new turtle,

42
00:02:39,560 --> 00:02:43,970
then we can append that turtle to this list. So at this stage,

43
00:02:44,000 --> 00:02:47,870
it doesn't really make sense to call all the turtles tim anymore, right?

44
00:02:47,870 --> 00:02:50,900
Because they're all going to be individual instances.

45
00:02:51,470 --> 00:02:56,470
So let's go ahead and refactor and rename this to a new_turtle.

46
00:02:58,900 --> 00:03:03,900
And now what's happening in our for loop is we're creating a new turtle and

47
00:03:04,060 --> 00:03:09,060
each of those new turtles are going to be appended to our all_turtles list.

48
00:03:11,320 --> 00:03:15,730
So now we're going to have a list of multiple turtle instances,

49
00:03:16,270 --> 00:03:20,260
and each of those instances is going to have a different state.

50
00:03:20,530 --> 00:03:21,490
So for example,

51
00:03:21,520 --> 00:03:25,780
they already start out with a different Y-position and a different color.

52
00:03:26,320 --> 00:03:27,340
But later on,

53
00:03:27,400 --> 00:03:31,600
we're also going to get them to move forwards by a different amount.

54
00:03:32,170 --> 00:03:35,560
So we can loop through our list of turtles,

55
00:03:35,590 --> 00:03:38,080
so for turtle in all_turtles,

56
00:03:40,980 --> 00:03:45,980
let's go ahead and create a random distance for each of them and then move that

57
00:03:46,740 --> 00:03:49,320
turtle forward by the random distance.

58
00:03:49,860 --> 00:03:52,050
So now if we run our code

59
00:03:53,670 --> 00:03:55,200
and see it in action,

60
00:03:55,620 --> 00:03:59,580
you can see that once all of the turtles have gone into their starting

61
00:03:59,580 --> 00:04:04,560
positions, they stop creeping along each time by a different amount.

62
00:04:08,190 --> 00:04:12,990
Now, however, our turtles are actually going to keep going until forever.

63
00:04:13,110 --> 00:04:16,140
It's basically a loop that's never going to stop.

64
00:04:16,620 --> 00:04:20,700
So let's think about what the stopping conditions should be. Well,

65
00:04:20,730 --> 00:04:25,730
once any of these turtles have actually reached the end of the screen

66
00:04:26,730 --> 00:04:31,170
which is the X-value of +250,

67
00:04:31,170 --> 00:04:35,820
once they've reached this line, then that's kind of the end of the race,

68
00:04:35,850 --> 00:04:36,683
right?

69
00:04:36,990 --> 00:04:41,490
But a turtle object is a 40 by 40 object.

70
00:04:41,910 --> 00:04:45,390
So if we're only detecting for the 250,

71
00:04:45,660 --> 00:04:48,390
then our turtle would have already overshot.

72
00:04:48,480 --> 00:04:53,480
So instead, we want to detect when the turtle has reached the 230 mark. Back in our

73
00:04:55,230 --> 00:04:56,700
code in our for-loop,

74
00:04:57,060 --> 00:05:01,620
we can check to see if the turtle that we're currently looping through,

75
00:05:02,190 --> 00:05:07,190
if it's X-coordinate is greater than 

76
00:05:09,210 --> 00:05:13,710
230. Well, in this case, it's basically won.

77
00:05:14,190 --> 00:05:15,510
Now at this stage,

78
00:05:15,600 --> 00:05:19,890
if we print this current turtle's color,

79
00:05:20,340 --> 00:05:23,370
then you'll actually see something a little bit strange.

80
00:05:25,530 --> 00:05:27,360
You can see that firstly,

81
00:05:27,360 --> 00:05:31,890
it prints multiple values because the while loop is still continuing and the

82
00:05:31,890 --> 00:05:34,020
other turtles continue to go forward.

83
00:05:34,470 --> 00:05:37,770
But the first value is going to be the turtle that won.

84
00:05:38,340 --> 00:05:41,370
And you can see it prints yellow and yellow.

85
00:05:41,880 --> 00:05:45,840
So what's going on here? Well, if we look at this color method,

86
00:05:45,930 --> 00:05:50,790
you can see that you can either use it to set the pen color and fill color,

87
00:05:51,090 --> 00:05:54,720
or it will return the pen color and fill color.

88
00:05:55,170 --> 00:05:56,940
So when it prints out two colors,

89
00:05:57,650 --> 00:06:01,430
the first one is going to be the pen color and the second is going to be the

90
00:06:01,430 --> 00:06:05,720
fill color. So if we only want to know the pen color,

91
00:06:06,050 --> 00:06:10,910
then we can just get hold of the turtle.pencolor.

92
00:06:10,970 --> 00:06:15,290
This is going to be the color of the body of the turtle. So in our code,

93
00:06:15,320 --> 00:06:20,320
we can say that the winning color is equal to the current turtle.pencolor.

94
00:06:25,100 --> 00:06:30,100
And then we can now check to see if the winning color is equal to the user's

95
00:06:31,100 --> 00:06:35,420
bet, because if this is the case, well then the user has won, right?

96
00:06:35,450 --> 00:06:38,090
So we can tell them you've won.

97
00:06:38,330 --> 00:06:41,300
And then we can tell them the color of the winning turtle.

98
00:06:44,200 --> 00:06:45,070
Now, however,

99
00:06:45,070 --> 00:06:49,480
if this is false or rather in the else statement,

100
00:06:49,870 --> 00:06:53,140
well, in that case, we're going to have to print the same thing

101
00:06:53,470 --> 00:06:56,770
but now we're going to tell them that they've lost.

102
00:06:59,020 --> 00:06:59,410
Yeah.

103
00:06:59,410 --> 00:07:03,790
Finally, when the first turtle reaches the X-coordinate,

104
00:07:03,790 --> 00:07:06,160
when this first color gets printed,

105
00:07:06,280 --> 00:07:09,160
we want to stop the while loop. This way

106
00:07:09,160 --> 00:07:12,490
the remaining turtles don't continue to nudge forward

107
00:07:12,790 --> 00:07:17,170
and we get end up with lots of winners like this. So inside this

108
00:07:17,200 --> 00:07:21,520
if statement, when the turtle's X-coordinate has surpassed 230,

109
00:07:21,820 --> 00:07:25,390
we're going to change the is_race_on to false.

110
00:07:26,020 --> 00:07:30,190
Now we're ready to test out our code. And again,

111
00:07:30,190 --> 00:07:34,390
I'm going to bet on the red turtle and I'm going to speed up the playback speed

112
00:07:34,420 --> 00:07:36,640
of this race when I'm editing this video.

113
00:07:37,240 --> 00:07:42,240
And now the game has ended and you can see that the blue turtle is the one that

114
00:07:42,760 --> 00:07:44,920
first got its head across the finish line.

115
00:07:45,460 --> 00:07:50,260
And it tells me just as much. I've lost and the blue turtle is the winner.

116
00:07:51,070 --> 00:07:56,070
So we've now used Python turtle to build a very simple turtle racing game.

117
00:07:56,680 --> 00:08:01,680
And we're starting to use these X and Y positions in the turtle coordinate

118
00:08:02,350 --> 00:08:06,520
system. Now, these coordinates take a little bit of getting used to,

119
00:08:06,850 --> 00:08:11,850
especially thinking about things such as if a turtle is 40 pixels wide,

120
00:08:12,160 --> 00:08:14,770
then in order to get the turtle to the starting line

121
00:08:14,980 --> 00:08:19,300
we have to subtract half of the width of the turtle, which is 20.

122
00:08:19,540 --> 00:08:23,350
So then 250 minus 20 becomes 230.

123
00:08:23,680 --> 00:08:26,740
And it's things like this that takes a little bit of getting used to.

124
00:08:27,220 --> 00:08:31,480
So what I really recommend is to have a play around with these values,

125
00:08:31,750 --> 00:08:36,429
make it smaller, or make it larger, change the size of the screen.

126
00:08:36,730 --> 00:08:41,049
And just until you're really familiar with how the coordinate system works

127
00:08:41,080 --> 00:08:44,620
before you proceed to the next lesson, because as you keep going,

128
00:08:44,620 --> 00:08:48,160
I'm going to presume that you've already had a play around with it and you know

129
00:08:48,160 --> 00:08:50,560
exactly how it works. Now,

130
00:08:50,590 --> 00:08:55,150
the other thing that we saw a lot of in this project is this concept of having

131
00:08:55,380 --> 00:08:59,700
multiple objects created from the same class. So in this case,

132
00:08:59,700 --> 00:09:04,590
we're using the turtle class to construct a turtle object called Timmy,

133
00:09:04,860 --> 00:09:09,860
but we've also got a separate object called Tommy. And Timmy and Tommy are both

134
00:09:10,020 --> 00:09:14,900
Tuttle objects, but they're different instances. So they can act independently,

135
00:09:14,900 --> 00:09:18,110
they can have different appearances, they can have different colors,

136
00:09:18,110 --> 00:09:21,380
different attributes, and also a different speed of movement.

137
00:09:21,830 --> 00:09:26,830
So we've seen in our turtle race that each of these instances can act of their

138
00:09:27,140 --> 00:09:29,690
own accord and have different state.

139
00:09:29,930 --> 00:09:33,860
And that's shown in the different speed of movement and this capability of

140
00:09:33,860 --> 00:09:37,580
creating multiple objects which can act and behave independently

141
00:09:37,880 --> 00:09:42,590
is really the secret to why Object Oriented Programming can be so

142
00:09:42,590 --> 00:09:43,423
powerful.

143
00:09:43,550 --> 00:09:48,050
We can create so many of these objects and get them to do our bidding in various

144
00:09:48,050 --> 00:09:50,480
parts of our program. And in the coming lessons

145
00:09:50,510 --> 00:09:52,970
as we build more and more complex projects,

146
00:09:53,270 --> 00:09:58,130
you'll see this concept of having multiple instances with different state being

147
00:09:58,130 --> 00:10:02,060
shown again and again. And we're going to come to see and enjoy the benefits.

