WEBVTT

0
00:00.660 --> 00:03.000
All right guys, we're at the final stretch.

1
00:03.210 --> 00:08.190
All we have left to do now is to figure out how we can detect when the snake

2
00:08.400 --> 00:12.810
collides with its own tail. As a snake gets longer and longer,

3
00:12.900 --> 00:17.900
it's more likely that at some point the head might hit some part of the tail. And

4
00:18.690 --> 00:21.150
in the snake game, this means it's game over.

5
00:21.480 --> 00:25.800
So we have to figure out how we can detect this and then trigger the game over

6
00:25.800 --> 00:26.633
sequence.

7
00:28.050 --> 00:33.050
One of the things that we've left until now is a way of extending the snake

8
00:34.560 --> 00:37.110
every time it gets some food,

9
00:37.530 --> 00:42.120
because at the moment what's happening is our snake is staying the same size

10
00:42.150 --> 00:46.710
with only three segments. But now we need to change that.

11
00:47.100 --> 00:52.080
We need to add a segment to the snake every single time it hits the food so that

12
00:52.080 --> 00:56.070
it grows in length and we can start detecting collision with the tail.

13
00:57.420 --> 01:00.750
The place we're going to do that is inside our snake class.

14
01:01.290 --> 01:05.610
So we're going to create a function that's going to be called extend.

15
01:06.540 --> 01:11.520
And this extend function is going to add a new segment to the snake.

16
01:12.660 --> 01:14.910
So we're probably going to need another function,

17
01:14.910 --> 01:17.700
which is going to be called add_segment,

18
01:18.300 --> 01:23.300
and this is going to require the position to add the segment to essentially.

19
01:25.350 --> 01:29.340
Notice how this is the part of the code where we actually add the segment.

20
01:29.610 --> 01:33.780
So lets take it out of the for loop and put it into the add_segment.

21
01:34.650 --> 01:39.450
And so we can now call this function, self.add_segment

22
01:39.570 --> 01:42.420
and then pass in this position that we're looping through.

23
01:42.990 --> 01:46.740
These two functions together are going to create our snake.

24
01:47.190 --> 01:48.210
But in addition,

25
01:48.240 --> 01:52.920
we're going to use this add_segment when we want to extend the snake.

26
01:53.430 --> 01:57.690
But what position do we want that last piece to go to? Well,

27
01:57.720 --> 02:02.720
we're going to get hold of the list of segments and we're going to get hold of

28
02:02.880 --> 02:07.080
the last one. Remember that with lists in Python,

29
02:07.200 --> 02:07.950
you can write

30
02:07.950 --> 02:12.660
a negative number so that you start counting from the end of the list.

31
02:13.020 --> 02:16.680
For example, if our list was 1, 2, and 3,

32
02:17.040 --> 02:20.610
then this list at minus one position would be

33
02:20.840 --> 02:21.673
<v 1>3.</v>

34
02:22.880 --> 02:23.420
<v 0>Effectively</v>

35
02:23.420 --> 02:27.770
we're getting hold of the last segment in our list and then we're going to get

36
02:27.770 --> 02:30.620
hold of it's position. And remember,

37
02:30.620 --> 02:35.620
this is a method that comes from the turtle class and we can call it by writing

38
02:36.350 --> 02:40.250
position and then parentheses and we'll get the position of that segment.

39
02:40.760 --> 02:42.470
And then we're going to add this new segment

40
02:42.680 --> 02:45.410
to the same position as the last segment.

41
02:46.640 --> 02:49.670
So now if inside our main.py

42
02:50.210 --> 02:53.660
every time our snake collides with the food,

43
02:53.960 --> 02:56.270
then not only are we going to refresh the food,

44
02:56.540 --> 02:59.830
but we're also going to get the snake to extend itself.

45
03:00.370 --> 03:05.370
So lets go ahead and run this game again. And notice how this time,

46
03:05.860 --> 03:07.870
once I hit the food,

47
03:08.200 --> 03:12.190
the snake actually extends itself and it gets longer each time.

48
03:13.030 --> 03:18.030
So now we're able to think about how to detect tail collision because as the

49
03:18.820 --> 03:23.380
snake gets longer, this tail collision becomes more of a problem.

50
03:23.740 --> 03:26.980
We're more likely to get tangled with our own tail.

51
03:28.870 --> 03:32.320
Let's go ahead and add a comment here which will say 'Detect

52
03:32.380 --> 03:37.380
collision with tail.' And the way that we want to check this is we wanna say if

53
03:39.760 --> 03:44.620
the head collides with any segment

54
03:46.330 --> 03:48.040
in the tail,

55
03:48.490 --> 03:53.490
then this means that we're going to trigger the game over sequence.

56
03:56.530 --> 04:01.420
So how can we detect if the head collides with any segment in the tail?

57
04:02.290 --> 04:02.650
Well,

58
04:02.650 --> 04:07.650
we could loop through our list of segments in the snake.

59
04:08.020 --> 04:13.020
So we could say for segment in snake.segments,

60
04:14.350 --> 04:18.340
so out of all the snakes segments, let's go through each one of them.

61
04:18.850 --> 04:20.800
And then we can say that well,

62
04:21.160 --> 04:26.160
if the snake.head has a distance of,

63
04:26.860 --> 04:28.090
let's say, I don't know,

64
04:28.120 --> 04:33.120
less than 10 from any of those segments that we're currently looping through, well

65
04:35.710 --> 04:38.890
that probably is a collision, right? So in that case,

66
04:38.920 --> 04:43.300
we're going to set the game_is_on to false and we're going to get the

67
04:43.300 --> 04:46.720
scoreboard to trigger the game over sequence.

68
04:48.160 --> 04:51.970
Now there's just one problem with this. If we go ahead and run the code

69
04:52.030 --> 04:55.570
as it is, you can see that immediately we get game over.

70
04:56.140 --> 04:59.410
And the reason is because out of all of the snake segments,

71
04:59.530 --> 05:02.230
the head is the first segment.

72
05:02.620 --> 05:05.050
So when we loop through each of the segments,

73
05:05.230 --> 05:07.750
the first segment is going to be the snake head.

74
05:08.080 --> 05:11.710
And so we're detecting if the snake head has a distance to

75
05:11.710 --> 05:15.280
the snake head of less than 10, which of course it is going to.

76
05:15.760 --> 05:19.180
So we need some sort of way of bypassing the snakehead.

77
05:19.630 --> 05:24.630
So we could say if the current segment that we're looping through is equal to, so

78
05:25.390 --> 05:30.370
double equal, to the snake.head, well, in this case,

79
05:30.400 --> 05:35.170
we're going to pass. But if this is not the case, so in the elif

80
05:35.200 --> 05:36.460
after the if statement,

81
05:36.820 --> 05:41.820
then we can check to see if the snake head has a distance to the segment of less

82
05:41.980 --> 05:45.100
than 10. Now, if we rerun this code,

83
05:46.330 --> 05:49.960
you'll see that it works exactly as we expect it to.

84
05:52.000 --> 05:55.810
We can move our snake around and as soon as the head hits the body,

85
05:56.080 --> 05:57.830
then it's game over for us.

86
05:59.180 --> 06:03.590
We've pretty much now completed the snake game. But in the next lesson,

87
06:03.680 --> 06:07.310
I want to talk about a special functionality in Python

88
06:07.340 --> 06:08.690
which is called slicing.

89
06:09.200 --> 06:12.770
And this is something that you will often see in code in the wild,

90
06:13.100 --> 06:16.640
and we'll use that to improve our code just a little bit more. So

91
06:17.330 --> 06:19.310
for all of that and more, I'll see you there.