1
00:00:00,330 --> 00:00:03,570
In the last lesson, we managed to figure out how to get the turtle

2
00:00:03,780 --> 00:00:08,340
to move with a keypress and that's with every press of the Up key

3
00:00:08,610 --> 00:00:11,040
the turtle moves further towards North.

4
00:00:11,730 --> 00:00:15,300
Now the next task is to create and move the cars.

5
00:00:15,930 --> 00:00:20,930
The cars need to move from right to left and they need to be randomly generated

6
00:00:21,060 --> 00:00:23,460
along the Y-axis here.

7
00:00:23,970 --> 00:00:28,970
So they need to be generated right to the right edge of the screen and they can

8
00:00:29,460 --> 00:00:31,920
appear anywhere along this line,

9
00:00:32,220 --> 00:00:36,840
and then they're going to move automatically towards the left at a fixed pace.

10
00:00:37,530 --> 00:00:38,160
Each of these cars,

11
00:00:38,160 --> 00:00:43,160
you can see, are going to be turtles and they're going to be 20 by 40

12
00:00:43,740 --> 00:00:46,770
pixels. So we mentioned that by default

13
00:00:46,860 --> 00:00:50,490
every turtle that we create is 20 by 20 pixels.

14
00:00:50,940 --> 00:00:54,480
So we'll need to stretch the turtle to make it into this shape.

15
00:00:55,500 --> 00:01:00,150
Now that we are concerned with the cars let's create our code inside the car

16
00:01:00,150 --> 00:01:00,983
manager.

17
00:01:01,680 --> 00:01:06,680
The first thing we'll need to do is to import the turtle class from the turtle

18
00:01:06,900 --> 00:01:11,900
module and our car manager is going to have a init where we create all of our

19
00:01:14,790 --> 00:01:18,780
cars. So I'm going to create a list called all_cars,

20
00:01:19,320 --> 00:01:23,280
and it's going to start out being an empty list. Now,

21
00:01:23,310 --> 00:01:24,420
at some point,

22
00:01:24,810 --> 00:01:29,810
our main.py is going to call a function from the car_manager called create_

23
00:01:30,570 --> 00:01:31,403
cars.

24
00:01:31,740 --> 00:01:36,740
And this method is going to create a random car somewhere along the Y-axis with

25
00:01:38,670 --> 00:01:42,780
a given dimension. Each of these cars,

26
00:01:42,900 --> 00:01:47,900
so let's call it a new_car, is going to be a turtle object and it's going to be

27
00:01:48,630 --> 00:01:52,680
created in a square shape. Now, in addition,

28
00:01:52,710 --> 00:01:56,370
we're going to have to change the dimensions of this turtle

29
00:01:56,730 --> 00:01:59,730
and we're going to use that method that we used before

30
00:02:00,000 --> 00:02:02,100
which is this one, shapesize.

31
00:02:02,670 --> 00:02:05,940
Now, what shapesize allows us to do, if you remember,

32
00:02:06,030 --> 00:02:11,030
is it allows us to stretch our turtle along the width and along the length.

33
00:02:11,700 --> 00:02:15,150
So we want the width to be double the original size,

34
00:02:15,180 --> 00:02:20,180
so two times 20 pixels is 40 pixels and the length we don't want to be stretched

35
00:02:21,090 --> 00:02:21,510
at all.

36
00:02:21,510 --> 00:02:26,510
So we're going to say it's one times the original length. In addition our new

37
00:02:27,210 --> 00:02:31,050
car is not going to draw, so we're going to get it to penup.

38
00:02:31,710 --> 00:02:36,710
And also our new car is going to have a random color. So we can set the color and

39
00:02:38,400 --> 00:02:43,400
then we can import the random module so that we can use one of these colors in

40
00:02:44,520 --> 00:02:45,353
this list.

41
00:02:45,960 --> 00:02:50,960
And we can say random.choice and then pass in our list of colors from this

42
00:02:52,620 --> 00:02:55,680
COLORS constant. Finally,

43
00:02:55,710 --> 00:02:59,980
we're going to define where it's going to go on the screen. To do that

44
00:03:00,040 --> 00:03:03,820
we need to define a random_y position.

45
00:03:04,360 --> 00:03:08,110
So this again is going to be created from the random module and we're going to

46
00:03:08,110 --> 00:03:13,110
use the randint and then we're going to define somewhere along this Y-axis,

47
00:03:14,050 --> 00:03:18,220
which is the vertical, somewhere for this call to be generated at.

48
00:03:18,880 --> 00:03:22,630
So we know that our screen is 600 by 600, so

49
00:03:23,110 --> 00:03:27,730
that means the Y-axis goes from +300 to -300.

50
00:03:28,090 --> 00:03:31,900
But we don't want the car to be generated all the way to the edge because

51
00:03:31,900 --> 00:03:35,800
remember the turtle needs a bit of starting space and also a bit of ending

52
00:03:35,800 --> 00:03:38,830
space. So you can play around with this number,

53
00:03:38,860 --> 00:03:43,360
but I've ended up going with -250 to +250.

54
00:03:44,140 --> 00:03:49,140
This means that we get a good range in the middle of the screen and that the

55
00:03:49,300 --> 00:03:53,830
turtle has a bit of empty space at the beginning and also at the end when it

56
00:03:53,830 --> 00:03:58,090
needs to move across. So now that we've defined our random_

57
00:03:58,090 --> 00:04:03,090
y, we can tell our new_car to go to the x position

58
00:04:03,670 --> 00:04:07,030
which is going to be the very edge of the right screen,

59
00:04:07,300 --> 00:04:10,540
so that's going to be +300, right to the edge.

60
00:04:10,960 --> 00:04:15,960
And then the y position is going to be the random_y that we just created.

61
00:04:17,589 --> 00:04:20,170
And finally, once we've created our new_car,

62
00:04:20,380 --> 00:04:25,380
we're going to append it to our list of all_cars which of course,

63
00:04:26,650 --> 00:04:31,090
because it's defined in a class, it has to have the self in front of it.

64
00:04:32,110 --> 00:04:35,830
So self.all_cars.append,

65
00:04:36,160 --> 00:04:39,730
and then we're going to append this new car that we've just created.

66
00:04:40,780 --> 00:04:43,120
Now, coming back to our main.py,

67
00:04:43,450 --> 00:04:48,010
let's create our car_manager from the CarManager class,

68
00:04:48,490 --> 00:04:51,910
and then we're going to use it inside our game loop here

69
00:04:52,030 --> 00:04:55,150
so that on every refresh of the screen,

70
00:04:55,450 --> 00:04:57,070
every 0.1 seconds,

71
00:04:57,370 --> 00:05:01,330
we're going to get the car_manager to create a new car.

72
00:05:03,010 --> 00:05:08,010
And strictly this probably shouldn't be plural because each time we call this

73
00:05:08,470 --> 00:05:11,500
method, it only creates one new car.

74
00:05:11,980 --> 00:05:15,670
So let's change that method so that it only creates one car,

75
00:05:15,880 --> 00:05:18,910
but it creates one car every 0.1 seconds.

76
00:05:19,930 --> 00:05:23,260
Now at this point in time if we run our code,

77
00:05:23,560 --> 00:05:28,560
you'll actually see nothing happen because the cars are being created at this

78
00:05:29,500 --> 00:05:30,640
right edge here

79
00:05:30,820 --> 00:05:34,630
and it's not yet visible on the screen. To make it visible

80
00:05:34,630 --> 00:05:37,660
we have to move our cars across the screen.

81
00:05:38,440 --> 00:05:43,440
Let's create another method which we'll call move_cars because this method is

82
00:05:43,960 --> 00:05:46,420
going to go through our list of cars,

83
00:05:46,690 --> 00:05:50,230
so for car in self.all_cars,

84
00:05:50,770 --> 00:05:55,150
and for each of those cars, it's going to move it towards the left

85
00:05:55,330 --> 00:05:58,370
by the move distance that we've defined up here.

86
00:05:59,270 --> 00:06:02,240
So to do that, all we need is to get the car

87
00:06:02,480 --> 00:06:07,460
to move backwards by the starting move distance.

88
00:06:08,210 --> 00:06:12,740
Now it's going to loop through our list of all_cars that were created

89
00:06:13,190 --> 00:06:14,750
and then for each of the cars,

90
00:06:14,780 --> 00:06:18,260
it's going to move it backwards by five paces.

91
00:06:18,890 --> 00:06:23,890
So now in addition to getting the car manager to create a new car on every

92
00:06:24,470 --> 00:06:26,030
refresh of the game loop,

93
00:06:26,360 --> 00:06:31,160
we're also going to get it to move all of the cars by five paces.

94
00:06:31,700 --> 00:06:32,540
Let's hit run

95
00:06:32,540 --> 00:06:37,160
now and you should see that we've got our cars being generated,

96
00:06:37,580 --> 00:06:42,580
but notice how I've made a slight error in the stretch. Instead of stretching it

97
00:06:43,160 --> 00:06:46,670
along the length, I've stretched it along the width

98
00:06:47,060 --> 00:06:50,810
and this is why we've got this random array of shapes moving.

99
00:06:51,320 --> 00:06:56,320
So let's go back to our car manager and change the width to stretch by one and

100
00:06:56,870 --> 00:07:01,160
stretch to the length by two. So now if we rerun our code,

101
00:07:01,490 --> 00:07:04,070
you can see the cars are now the right shape,

102
00:07:04,700 --> 00:07:08,600
a rectangle that looks like it's moving to the left.

103
00:07:09,680 --> 00:07:11,690
Now notice how, in my case,

104
00:07:11,750 --> 00:07:15,260
the cars are being generated far too frequently.

105
00:07:15,260 --> 00:07:20,210
We've got way too many calls and it's impossible for our turtle to cross the

106
00:07:20,210 --> 00:07:21,043
road.

107
00:07:21,260 --> 00:07:26,260
So we need to figure out a way of how we can reduce the number of cars that are being

108
00:07:26,720 --> 00:07:29,780
made. Instead of creating a car

109
00:07:29,960 --> 00:07:34,700
every 0.1 seconds at every refresh of the game,

110
00:07:35,090 --> 00:07:37,280
we need to slow this down a little bit.

111
00:07:37,970 --> 00:07:42,650
And one way we can do this is by using a random number.

112
00:07:43,310 --> 00:07:47,210
Let's say that we create a random_chance

113
00:07:47,780 --> 00:07:51,080
which is going to be a one in six chance,

114
00:07:51,110 --> 00:07:55,250
so it's almost like throwing a dice. If you get a one on the dice,

115
00:07:55,580 --> 00:07:56,780
then we create a car.

116
00:07:57,290 --> 00:08:00,740
We can do this by getting hold of our random module,

117
00:08:00,950 --> 00:08:04,490
creating a random int between one and six

118
00:08:05,120 --> 00:08:10,120
and then we can say that if the random_chance is equal to one,

119
00:08:12,020 --> 00:08:15,920
then, and only then do we actually create a new car.

120
00:08:16,430 --> 00:08:21,430
So this basically ensures that every six times the while loop runs a new car will

121
00:08:23,930 --> 00:08:24,980
be generated.

122
00:08:25,430 --> 00:08:29,900
Now it's not going to be precisely every six times because there's some degree

123
00:08:29,900 --> 00:08:33,500
of chance involved. But notice when we run our program

124
00:08:33,500 --> 00:08:38,500
now, see how the cars are now being created much less frequently.

125
00:08:39,620 --> 00:08:43,130
And we've actually got some space for our turtle to cross.

126
00:08:43,880 --> 00:08:48,880
So that is how we create and move our cars across the screen and make sure that

127
00:08:49,580 --> 00:08:53,450
they don't occur so frequently that our turtle has no chance of crossing the

128
00:08:53,450 --> 00:08:57,150
road. I hope this walkthrough was helpful.

129
00:08:57,360 --> 00:09:01,320
Don't worry if your code doesn't look the same as mine, it doesn't have to.

130
00:09:01,770 --> 00:09:06,270
What matters is if you're able to achieve the desired functionality of the game.

131
00:09:06,660 --> 00:09:09,240
There are a million different ways that you can solve this problem.

132
00:09:09,450 --> 00:09:13,110
And as long as it works, whatever way you choose will be the best way.

