1 00:00:00,330 --> 00:00:05,330 Now in the last lesson we created these rectangular cars randomly along the Y- 2 00:00:07,350 --> 00:00:08,183 axis. 3 00:00:08,400 --> 00:00:12,210 And then we managed to get them to move across to the left side of the screen. 4 00:00:12,930 --> 00:00:17,930 The next step is to detect when the turtle collides with the car. 5 00:00:18,990 --> 00:00:22,440 So that way, when the turtle hits one of the cars, 6 00:00:22,830 --> 00:00:27,830 we can stop the game and prevent further cars from moving. Inside our while 7 00:00:30,450 --> 00:00:33,690 loop I'm going to detect the collision with the car here. 8 00:00:34,200 --> 00:00:39,200 So I'm going to get hold of all the cars in the car_manager object and I'm 9 00:00:41,100 --> 00:00:46,100 going to use a for loop to loop through each of the cars in that list of cars. 10 00:00:47,400 --> 00:00:52,400 And then we're going to detect whether if the car has a distance to the player 11 00:00:55,620 --> 00:00:59,700 object that is less than 20. 12 00:01:00,870 --> 00:01:05,870 Remember that our cars are 20 pixels in height by 40 pixels in width. 13 00:01:09,030 --> 00:01:13,290 If the player is less than 20 pixels from the center of the car, 14 00:01:13,620 --> 00:01:16,650 then it probably means that it's collided with the car. 15 00:01:17,520 --> 00:01:21,000 So if this distance is less than 20, 16 00:01:21,570 --> 00:01:23,250 then we're going to stop the game. 17 00:01:23,580 --> 00:01:25,740 And the way that we stopped the game is of course, 18 00:01:25,740 --> 00:01:30,360 by turning this game_is_on from true to false. 19 00:01:31,320 --> 00:01:36,320 Let's run our code again and let's see this in action. So we can move our turtle, 20 00:01:36,990 --> 00:01:41,790 we've got randomly generated cars moving across and let's just park our turtle right 21 00:01:41,790 --> 00:01:44,850 here. When that car hit our turtle 22 00:01:45,180 --> 00:01:48,000 it immediately stopped the game. Now, 23 00:01:48,030 --> 00:01:50,460 if we want to take a look at what's happening 24 00:01:50,460 --> 00:01:54,840 so for the screen to stay open instead of closing 25 00:01:54,870 --> 00:01:59,250 once the process is finished, we can tell it to exitonclick. 26 00:01:59,790 --> 00:02:03,000 So now let's run our code again and notice this time 27 00:02:03,330 --> 00:02:05,460 if we collide with one of the cars 28 00:02:07,170 --> 00:02:11,400 you can see that it stops and it waits for further instruction, 29 00:02:11,970 --> 00:02:16,970 which eventually is going to be just the game over text showing up on screen 30 00:02:18,720 --> 00:02:23,670 and we also the final level showing up on screen. That's it. 31 00:02:24,240 --> 00:02:28,530 That's how we detect collision between the car and our player.