1
00:00:00,660 --> 00:00:03,000
All right guys, we're at the final stretch.

2
00:00:03,210 --> 00:00:08,189
All we have left to do now is to figure out how we can detect when the snake

3
00:00:08,400 --> 00:00:12,810
collides with its own tail. As a snake gets longer and longer,

4
00:00:12,900 --> 00:00:17,900
it's more likely that at some point the head might hit some part of the tail. And

5
00:00:18,690 --> 00:00:21,150
in the snake game, this means it's game over.

6
00:00:21,480 --> 00:00:25,800
So we have to figure out how we can detect this and then trigger the game over

7
00:00:25,800 --> 00:00:26,633
sequence.

8
00:00:28,050 --> 00:00:33,050
One of the things that we've left until now is a way of extending the snake

9
00:00:34,560 --> 00:00:37,110
every time it gets some food,

10
00:00:37,530 --> 00:00:42,120
because at the moment what's happening is our snake is staying the same size

11
00:00:42,150 --> 00:00:46,710
with only three segments. But now we need to change that.

12
00:00:47,100 --> 00:00:52,080
We need to add a segment to the snake every single time it hits the food so that

13
00:00:52,080 --> 00:00:56,070
it grows in length and we can start detecting collision with the tail.

14
00:00:57,420 --> 00:01:00,750
The place we're going to do that is inside our snake class.

15
00:01:01,290 --> 00:01:05,610
So we're going to create a function that's going to be called extend.

16
00:01:06,540 --> 00:01:11,520
And this extend function is going to add a new segment to the snake.

17
00:01:12,660 --> 00:01:14,910
So we're probably going to need another function,

18
00:01:14,910 --> 00:01:17,700
which is going to be called add_segment,

19
00:01:18,300 --> 00:01:23,300
and this is going to require the position to add the segment to essentially.

20
00:01:25,350 --> 00:01:29,340
Notice how this is the part of the code where we actually add the segment.

21
00:01:29,610 --> 00:01:33,780
So lets take it out of the for loop and put it into the add_segment.

22
00:01:34,650 --> 00:01:39,450
And so we can now call this function, self.add_segment

23
00:01:39,570 --> 00:01:42,420
and then pass in this position that we're looping through.

24
00:01:42,990 --> 00:01:46,740
These two functions together are going to create our snake.

25
00:01:47,190 --> 00:01:48,210
But in addition,

26
00:01:48,240 --> 00:01:52,920
we're going to use this add_segment when we want to extend the snake.

27
00:01:53,430 --> 00:01:57,690
But what position do we want that last piece to go to? Well,

28
00:01:57,720 --> 00:02:02,720
we're going to get hold of the list of segments and we're going to get hold of

29
00:02:02,880 --> 00:02:07,080
the last one. Remember that with lists in Python,

30
00:02:07,200 --> 00:02:07,950
you can write

31
00:02:07,950 --> 00:02:12,660
a negative number so that you start counting from the end of the list.

32
00:02:13,020 --> 00:02:16,680
For example, if our list was 1, 2, and 3,

33
00:02:17,040 --> 00:02:20,610
then this list at minus one position would be

34
00:02:20,840 --> 00:02:21,673
3.

35
00:02:22,880 --> 00:02:23,420
Effectively

36
00:02:23,420 --> 00:02:27,770
we're getting hold of the last segment in our list and then we're going to get

37
00:02:27,770 --> 00:02:30,620
hold of it's position. And remember,

38
00:02:30,620 --> 00:02:35,620
this is a method that comes from the turtle class and we can call it by writing

39
00:02:36,350 --> 00:02:40,250
position and then parentheses and we'll get the position of that segment.

40
00:02:40,760 --> 00:02:42,470
And then we're going to add this new segment

41
00:02:42,680 --> 00:02:45,410
to the same position as the last segment.

42
00:02:46,640 --> 00:02:49,670
So now if inside our main.py

43
00:02:50,210 --> 00:02:53,660
every time our snake collides with the food,

44
00:02:53,960 --> 00:02:56,270
then not only are we going to refresh the food,

45
00:02:56,540 --> 00:02:59,830
but we're also going to get the snake to extend itself.

46
00:03:00,370 --> 00:03:05,370
So lets go ahead and run this game again. And notice how this time,

47
00:03:05,860 --> 00:03:07,870
once I hit the food,

48
00:03:08,200 --> 00:03:12,190
the snake actually extends itself and it gets longer each time.

49
00:03:13,030 --> 00:03:18,030
So now we're able to think about how to detect tail collision because as the

50
00:03:18,820 --> 00:03:23,380
snake gets longer, this tail collision becomes more of a problem.

51
00:03:23,740 --> 00:03:26,980
We're more likely to get tangled with our own tail.

52
00:03:28,870 --> 00:03:32,320
Let's go ahead and add a comment here which will say 'Detect

53
00:03:32,380 --> 00:03:37,380
collision with tail.' And the way that we want to check this is we wanna say if

54
00:03:39,760 --> 00:03:44,620
the head collides with any segment

55
00:03:46,330 --> 00:03:48,040
in the tail,

56
00:03:48,490 --> 00:03:53,490
then this means that we're going to trigger the game over sequence.

57
00:03:56,530 --> 00:04:01,420
So how can we detect if the head collides with any segment in the tail?

58
00:04:02,290 --> 00:04:02,650
Well,

59
00:04:02,650 --> 00:04:07,650
we could loop through our list of segments in the snake.

60
00:04:08,020 --> 00:04:13,020
So we could say for segment in snake.segments,

61
00:04:14,350 --> 00:04:18,339
so out of all the snakes segments, let's go through each one of them.

62
00:04:18,850 --> 00:04:20,800
And then we can say that well,

63
00:04:21,160 --> 00:04:26,160
if the snake.head has a distance of,

64
00:04:26,860 --> 00:04:28,090
let's say, I don't know,

65
00:04:28,120 --> 00:04:33,120
less than 10 from any of those segments that we're currently looping through, well

66
00:04:35,710 --> 00:04:38,890
that probably is a collision, right? So in that case,

67
00:04:38,920 --> 00:04:43,300
we're going to set the game_is_on to false and we're going to get the

68
00:04:43,300 --> 00:04:46,720
scoreboard to trigger the game over sequence.

69
00:04:48,160 --> 00:04:51,970
Now there's just one problem with this. If we go ahead and run the code

70
00:04:52,030 --> 00:04:55,570
as it is, you can see that immediately we get game over.

71
00:04:56,140 --> 00:04:59,410
And the reason is because out of all of the snake segments,

72
00:04:59,530 --> 00:05:02,230
the head is the first segment.

73
00:05:02,620 --> 00:05:05,050
So when we loop through each of the segments,

74
00:05:05,230 --> 00:05:07,750
the first segment is going to be the snake head.

75
00:05:08,080 --> 00:05:11,710
And so we're detecting if the snake head has a distance to

76
00:05:11,710 --> 00:05:15,280
the snake head of less than 10, which of course it is going to.

77
00:05:15,760 --> 00:05:19,180
So we need some sort of way of bypassing the snakehead.

78
00:05:19,630 --> 00:05:24,630
So we could say if the current segment that we're looping through is equal to, so

79
00:05:25,390 --> 00:05:30,370
double equal, to the snake.head, well, in this case,

80
00:05:30,400 --> 00:05:35,170
we're going to pass. But if this is not the case, so in the elif

81
00:05:35,200 --> 00:05:36,460
after the if statement,

82
00:05:36,820 --> 00:05:41,820
then we can check to see if the snake head has a distance to the segment of less

83
00:05:41,980 --> 00:05:45,100
than 10. Now, if we rerun this code,

84
00:05:46,330 --> 00:05:49,960
you'll see that it works exactly as we expect it to.

85
00:05:52,000 --> 00:05:55,810
We can move our snake around and as soon as the head hits the body,

86
00:05:56,080 --> 00:05:57,830
then it's game over for us.

87
00:05:59,180 --> 00:06:03,590
We've pretty much now completed the snake game. But in the next lesson,

88
00:06:03,680 --> 00:06:07,310
I want to talk about a special functionality in Python

89
00:06:07,340 --> 00:06:08,690
which is called slicing.

90
00:06:09,200 --> 00:06:12,770
And this is something that you will often see in code in the wild,

91
00:06:13,100 --> 00:06:16,640
and we'll use that to improve our code just a little bit more. So

92
00:06:17,330 --> 00:06:19,310
for all of that and more, I'll see you there.

