WEBVTT

0
00:00.330 --> 00:05.330
Now in the last lesson we created these rectangular cars randomly along the Y-

1
00:07.350 --> 00:08.183
axis.

2
00:08.400 --> 00:12.210
And then we managed to get them to move across to the left side of the screen.

3
00:12.930 --> 00:17.930
The next step is to detect when the turtle collides with the car.

4
00:18.990 --> 00:22.440
So that way, when the turtle hits one of the cars,

5
00:22.830 --> 00:27.830
we can stop the game and prevent further cars from moving. Inside our while

6
00:30.450 --> 00:33.690
loop I'm going to detect the collision with the car here.

7
00:34.200 --> 00:39.200
So I'm going to get hold of all the cars in the car_manager object and I'm

8
00:41.100 --> 00:46.100
going to use a for loop to loop through each of the cars in that list of cars.

9
00:47.400 --> 00:52.400
And then we're going to detect whether if the car has a distance to the player

10
00:55.620 --> 00:59.700
object that is less than 20.

11
01:00.870 --> 01:05.870
Remember that our cars are 20 pixels in height by 40 pixels in width.

12
01:09.030 --> 01:13.290
If the player is less than 20 pixels from the center of the car,

13
01:13.620 --> 01:16.650
then it probably means that it's collided with the car.

14
01:17.520 --> 01:21.000
So if this distance is less than 20,

15
01:21.570 --> 01:23.250
then we're going to stop the game.

16
01:23.580 --> 01:25.740
And the way that we stopped the game is of course,

17
01:25.740 --> 01:30.360
by turning this game_is_on from true to false.

18
01:31.320 --> 01:36.320
Let's run our code again and let's see this in action. So we can move our turtle,

19
01:36.990 --> 01:41.790
we've got randomly generated cars moving across and let's just park our turtle right

20
01:41.790 --> 01:44.850
here. When that car hit our turtle

21
01:45.180 --> 01:48.000
it immediately stopped the game. Now,

22
01:48.030 --> 01:50.460
if we want to take a look at what's happening

23
01:50.460 --> 01:54.840
so for the screen to stay open instead of closing

24
01:54.870 --> 01:59.250
once the process is finished, we can tell it to exitonclick.

25
01:59.790 --> 02:03.000
So now let's run our code again and notice this time

26
02:03.330 --> 02:05.460
if we collide with one of the cars

27
02:07.170 --> 02:11.400
you can see that it stops and it waits for further instruction,

28
02:11.970 --> 02:16.970
which eventually is going to be just the game over text showing up on screen

29
02:18.720 --> 02:23.670
and we also the final level showing up on screen. That's it.

30
02:24.240 --> 02:28.530
That's how we detect collision between the car and our player.