1
00:00:00,150 --> 00:00:03,450
We've coded up the majority of this game now already.

2
00:00:03,960 --> 00:00:08,960
All that's left to do is to define the two situations where we might get game

3
00:00:09,060 --> 00:00:09,893
over.

4
00:00:10,110 --> 00:00:14,280
And that is detecting collision with wall and detecting collision with tail.

5
00:00:14,730 --> 00:00:18,360
So let's tackle the easier one first. We want the snake

6
00:00:18,450 --> 00:00:20,640
once it gets past a certain point

7
00:00:20,880 --> 00:00:25,800
that's very close to the four walls to trigger the game over sequence.

8
00:00:26,400 --> 00:00:31,400
So we know that we have a 600 by 600 screen and the X and Y axis go to 300 and

9
00:00:35,340 --> 00:00:37,980
-300 at all four sides.

10
00:00:38,520 --> 00:00:42,960
So if we create a boundary box that say 280 here,

11
00:00:42,960 --> 00:00:45,840
280 here, -280 and -280,

12
00:00:46,290 --> 00:00:49,800
then as soon as the snake head touches that position,

13
00:00:50,160 --> 00:00:52,290
then once it zoomed past it,

14
00:00:52,410 --> 00:00:56,040
then we can say that the snake has pretty much hit the wall.

15
00:00:56,970 --> 00:00:59,010
Now this number is not perfect,

16
00:00:59,040 --> 00:01:02,370
it's just what I found to work after doing a lot of testing.

17
00:01:02,760 --> 00:01:06,060
And this is the case with a lot of games, you have to test it, run it,

18
00:01:06,270 --> 00:01:10,950
see if it works the way you want it to. But let's go ahead and add a comment,

19
00:01:11,010 --> 00:01:13,830
detect collision with a wall,

20
00:01:14,430 --> 00:01:16,140
and let's create our if statement.

21
00:01:16,440 --> 00:01:21,440
So what we want to say is if the snake.head has a X coordinate that is

22
00:01:26,250 --> 00:01:30,210
greater than 280, so it's gone too far to the right,

23
00:01:30,690 --> 00:01:35,690
or if it has a X coordinate that is less than -280,

24
00:01:37,290 --> 00:01:39,150
so it's gone too far to the left,

25
00:01:39,840 --> 00:01:43,080
or if it has a Y coordinate

26
00:01:43,290 --> 00:01:47,040
that's greater than 280, so too far to the top,

27
00:01:47,460 --> 00:01:52,460
or if it has a Y coordinate that is less than -280

28
00:01:53,580 --> 00:01:58,500
so it's gone too far to the bottom, if any of these four situations occur

29
00:01:58,560 --> 00:02:01,560
then it basically means that the snake has hit the wall.

30
00:02:01,980 --> 00:02:05,160
So what do we want to happen when the snake hits the wall? Well,

31
00:02:05,160 --> 00:02:10,160
we want to change this game_is_on to false because what that's going to allow us

32
00:02:11,070 --> 00:02:14,280
to do is to exit the while loop and basically

33
00:02:14,610 --> 00:02:17,580
end the snake movement and end the game.

34
00:02:18,270 --> 00:02:19,920
Let's go ahead and test this out.

35
00:02:20,040 --> 00:02:24,090
So let's let the snake hit the wall and you can see it no longer continues

36
00:02:24,090 --> 00:02:28,110
moving forward. But for the user, this might be a little bit confusing.

37
00:02:28,110 --> 00:02:32,790
It doesn't seem like it's actually game over. It seems like it's just crushed.

38
00:02:33,360 --> 00:02:35,820
So let's go ahead into our scoreboard

39
00:02:35,850 --> 00:02:40,380
which does a lot of the writing and let's create a new function

40
00:02:40,440 --> 00:02:42,510
which is going to be called game_over.

41
00:02:43,770 --> 00:02:48,770
And this game_over function is going to simply just write some texts,

42
00:02:49,260 --> 00:02:51,030
not the score, but instead

43
00:02:51,030 --> 00:02:56,030
it's just going to write 'GAME OVER' onto the screen using the same alignment and

44
00:02:56,760 --> 00:03:01,210
the same font. But instead of it at the position

45
00:03:01,210 --> 00:03:03,790
which we initially defined which is right at the top,

46
00:03:04,210 --> 00:03:09,130
let's go ahead and get it to go to the center, which is at (0, 0).

47
00:03:09,700 --> 00:03:12,040
So now when the snake hits the wall,

48
00:03:12,370 --> 00:03:14,770
not only does the snake stop moving,

49
00:03:15,040 --> 00:03:19,720
but we're also going to get the scoreboard to trigger the game over a sequence.

50
00:03:20,560 --> 00:03:24,730
So that means when it hits the wall, game over shows up in the middle of the

51
00:03:24,730 --> 00:03:28,360
screen, and you can still see the score that was written previously

52
00:03:28,660 --> 00:03:33,100
because we don't actually hit clear before we write this game over.

53
00:03:33,700 --> 00:03:37,720
And this means that the user can see that the game is over, the snake has stopped

54
00:03:37,750 --> 00:03:40,570
moving, and they can see their final score.

55
00:03:42,160 --> 00:03:46,780
So let's try to score some points. And once I've done that,

56
00:03:46,810 --> 00:03:51,550
I'm gonna hit a wall and it's game over and I can see that my score was 2.

