WEBVTT

0
00:00.330 --> 00:03.570
In the last lesson, we managed to figure out how to get the turtle

1
00:03.780 --> 00:08.340
to move with a keypress and that's with every press of the Up key

2
00:08.610 --> 00:11.040
the turtle moves further towards North.

3
00:11.730 --> 00:15.300
Now the next task is to create and move the cars.

4
00:15.930 --> 00:20.930
The cars need to move from right to left and they need to be randomly generated

5
00:21.060 --> 00:23.460
along the Y-axis here.

6
00:23.970 --> 00:28.970
So they need to be generated right to the right edge of the screen and they can

7
00:29.460 --> 00:31.920
appear anywhere along this line,

8
00:32.220 --> 00:36.840
and then they're going to move automatically towards the left at a fixed pace.

9
00:37.530 --> 00:38.160
Each of these cars,

10
00:38.160 --> 00:43.160
you can see, are going to be turtles and they're going to be 20 by 40

11
00:43.740 --> 00:46.770
pixels. So we mentioned that by default

12
00:46.860 --> 00:50.490
every turtle that we create is 20 by 20 pixels.

13
00:50.940 --> 00:54.480
So we'll need to stretch the turtle to make it into this shape.

14
00:55.500 --> 01:00.150
Now that we are concerned with the cars let's create our code inside the car

15
01:00.150 --> 01:00.983
manager.

16
01:01.680 --> 01:06.680
The first thing we'll need to do is to import the turtle class from the turtle

17
01:06.900 --> 01:11.900
module and our car manager is going to have a init where we create all of our

18
01:14.790 --> 01:18.780
cars. So I'm going to create a list called all_cars,

19
01:19.320 --> 01:23.280
and it's going to start out being an empty list. Now,

20
01:23.310 --> 01:24.420
at some point,

21
01:24.810 --> 01:29.810
our main.py is going to call a function from the car_manager called create_

22
01:30.570 --> 01:31.403
cars.

23
01:31.740 --> 01:36.740
And this method is going to create a random car somewhere along the Y-axis with

24
01:38.670 --> 01:42.780
a given dimension. Each of these cars,

25
01:42.900 --> 01:47.900
so let's call it a new_car, is going to be a turtle object and it's going to be

26
01:48.630 --> 01:52.680
created in a square shape. Now, in addition,

27
01:52.710 --> 01:56.370
we're going to have to change the dimensions of this turtle

28
01:56.730 --> 01:59.730
and we're going to use that method that we used before

29
02:00.000 --> 02:02.100
which is this one, shapesize.

30
02:02.670 --> 02:05.940
Now, what shapesize allows us to do, if you remember,

31
02:06.030 --> 02:11.030
is it allows us to stretch our turtle along the width and along the length.

32
02:11.700 --> 02:15.150
So we want the width to be double the original size,

33
02:15.180 --> 02:20.180
so two times 20 pixels is 40 pixels and the length we don't want to be stretched

34
02:21.090 --> 02:21.510
at all.

35
02:21.510 --> 02:26.510
So we're going to say it's one times the original length. In addition our new

36
02:27.210 --> 02:31.050
car is not going to draw, so we're going to get it to penup.

37
02:31.710 --> 02:36.710
And also our new car is going to have a random color. So we can set the color and

38
02:38.400 --> 02:43.400
then we can import the random module so that we can use one of these colors in

39
02:44.520 --> 02:45.353
this list.

40
02:45.960 --> 02:50.960
And we can say random.choice and then pass in our list of colors from this

41
02:52.620 --> 02:55.680
COLORS constant. Finally,

42
02:55.710 --> 02:59.980
we're going to define where it's going to go on the screen. To do that

43
03:00.040 --> 03:03.820
we need to define a random_y position.

44
03:04.360 --> 03:08.110
So this again is going to be created from the random module and we're going to

45
03:08.110 --> 03:13.110
use the randint and then we're going to define somewhere along this Y-axis,

46
03:14.050 --> 03:18.220
which is the vertical, somewhere for this call to be generated at.

47
03:18.880 --> 03:22.630
So we know that our screen is 600 by 600, so

48
03:23.110 --> 03:27.730
that means the Y-axis goes from +300 to -300.

49
03:28.090 --> 03:31.900
But we don't want the car to be generated all the way to the edge because

50
03:31.900 --> 03:35.800
remember the turtle needs a bit of starting space and also a bit of ending

51
03:35.800 --> 03:38.830
space. So you can play around with this number,

52
03:38.860 --> 03:43.360
but I've ended up going with -250 to +250.

53
03:44.140 --> 03:49.140
This means that we get a good range in the middle of the screen and that the

54
03:49.300 --> 03:53.830
turtle has a bit of empty space at the beginning and also at the end when it

55
03:53.830 --> 03:58.090
needs to move across. So now that we've defined our random_

56
03:58.090 --> 04:03.090
y, we can tell our new_car to go to the x position

57
04:03.670 --> 04:07.030
which is going to be the very edge of the right screen,

58
04:07.300 --> 04:10.540
so that's going to be +300, right to the edge.

59
04:10.960 --> 04:15.960
And then the y position is going to be the random_y that we just created.

60
04:17.590 --> 04:20.170
And finally, once we've created our new_car,

61
04:20.380 --> 04:25.380
we're going to append it to our list of all_cars which of course,

62
04:26.650 --> 04:31.090
because it's defined in a class, it has to have the self in front of it.

63
04:32.110 --> 04:35.830
So self.all_cars.append,

64
04:36.160 --> 04:39.730
and then we're going to append this new car that we've just created.

65
04:40.780 --> 04:43.120
Now, coming back to our main.py,

66
04:43.450 --> 04:48.010
let's create our car_manager from the CarManager class,

67
04:48.490 --> 04:51.910
and then we're going to use it inside our game loop here

68
04:52.030 --> 04:55.150
so that on every refresh of the screen,

69
04:55.450 --> 04:57.070
every 0.1 seconds,

70
04:57.370 --> 05:01.330
we're going to get the car_manager to create a new car.

71
05:03.010 --> 05:08.010
And strictly this probably shouldn't be plural because each time we call this

72
05:08.470 --> 05:11.500
method, it only creates one new car.

73
05:11.980 --> 05:15.670
So let's change that method so that it only creates one car,

74
05:15.880 --> 05:18.910
but it creates one car every 0.1 seconds.

75
05:19.930 --> 05:23.260
Now at this point in time if we run our code,

76
05:23.560 --> 05:28.560
you'll actually see nothing happen because the cars are being created at this

77
05:29.500 --> 05:30.640
right edge here

78
05:30.820 --> 05:34.630
and it's not yet visible on the screen. To make it visible

79
05:34.630 --> 05:37.660
we have to move our cars across the screen.

80
05:38.440 --> 05:43.440
Let's create another method which we'll call move_cars because this method is

81
05:43.960 --> 05:46.420
going to go through our list of cars,

82
05:46.690 --> 05:50.230
so for car in self.all_cars,

83
05:50.770 --> 05:55.150
and for each of those cars, it's going to move it towards the left

84
05:55.330 --> 05:58.370
by the move distance that we've defined up here.

85
05:59.270 --> 06:02.240
So to do that, all we need is to get the car

86
06:02.480 --> 06:07.460
to move backwards by the starting move distance.

87
06:08.210 --> 06:12.740
Now it's going to loop through our list of all_cars that were created

88
06:13.190 --> 06:14.750
and then for each of the cars,

89
06:14.780 --> 06:18.260
it's going to move it backwards by five paces.

90
06:18.890 --> 06:23.890
So now in addition to getting the car manager to create a new car on every

91
06:24.470 --> 06:26.030
refresh of the game loop,

92
06:26.360 --> 06:31.160
we're also going to get it to move all of the cars by five paces.

93
06:31.700 --> 06:32.540
Let's hit run

94
06:32.540 --> 06:37.160
now and you should see that we've got our cars being generated,

95
06:37.580 --> 06:42.580
but notice how I've made a slight error in the stretch. Instead of stretching it

96
06:43.160 --> 06:46.670
along the length, I've stretched it along the width

97
06:47.060 --> 06:50.810
and this is why we've got this random array of shapes moving.

98
06:51.320 --> 06:56.320
So let's go back to our car manager and change the width to stretch by one and

99
06:56.870 --> 07:01.160
stretch to the length by two. So now if we rerun our code,

100
07:01.490 --> 07:04.070
you can see the cars are now the right shape,

101
07:04.700 --> 07:08.600
a rectangle that looks like it's moving to the left.

102
07:09.680 --> 07:11.690
Now notice how, in my case,

103
07:11.750 --> 07:15.260
the cars are being generated far too frequently.

104
07:15.260 --> 07:20.210
We've got way too many calls and it's impossible for our turtle to cross the

105
07:20.210 --> 07:21.043
road.

106
07:21.260 --> 07:26.260
So we need to figure out a way of how we can reduce the number of cars that are being

107
07:26.720 --> 07:29.780
made. Instead of creating a car

108
07:29.960 --> 07:34.700
every 0.1 seconds at every refresh of the game,

109
07:35.090 --> 07:37.280
we need to slow this down a little bit.

110
07:37.970 --> 07:42.650
And one way we can do this is by using a random number.

111
07:43.310 --> 07:47.210
Let's say that we create a random_chance

112
07:47.780 --> 07:51.080
which is going to be a one in six chance,

113
07:51.110 --> 07:55.250
so it's almost like throwing a dice. If you get a one on the dice,

114
07:55.580 --> 07:56.780
then we create a car.

115
07:57.290 --> 08:00.740
We can do this by getting hold of our random module,

116
08:00.950 --> 08:04.490
creating a random int between one and six

117
08:05.120 --> 08:10.120
and then we can say that if the random_chance is equal to one,

118
08:12.020 --> 08:15.920
then, and only then do we actually create a new car.

119
08:16.430 --> 08:21.430
So this basically ensures that every six times the while loop runs a new car will

120
08:23.930 --> 08:24.980
be generated.

121
08:25.430 --> 08:29.900
Now it's not going to be precisely every six times because there's some degree

122
08:29.900 --> 08:33.500
of chance involved. But notice when we run our program

123
08:33.500 --> 08:38.500
now, see how the cars are now being created much less frequently.

124
08:39.620 --> 08:43.130
And we've actually got some space for our turtle to cross.

125
08:43.880 --> 08:48.880
So that is how we create and move our cars across the screen and make sure that

126
08:49.580 --> 08:53.450
they don't occur so frequently that our turtle has no chance of crossing the

127
08:53.450 --> 08:57.150
road. I hope this walkthrough was helpful.

128
08:57.360 --> 09:01.320
Don't worry if your code doesn't look the same as mine, it doesn't have to.

129
09:01.770 --> 09:06.270
What matters is if you're able to achieve the desired functionality of the game.

130
09:06.660 --> 09:09.240
There are a million different ways that you can solve this problem.

131
09:09.450 --> 09:13.110
And as long as it works, whatever way you choose will be the best way.