WEBVTT

0
00:00.150 --> 00:03.450
We've coded up the majority of this game now already.

1
00:03.960 --> 00:08.960
All that's left to do is to define the two situations where we might get game

2
00:09.060 --> 00:09.893
over.

3
00:10.110 --> 00:14.280
And that is detecting collision with wall and detecting collision with tail.

4
00:14.730 --> 00:18.360
So let's tackle the easier one first. We want the snake

5
00:18.450 --> 00:20.640
once it gets past a certain point

6
00:20.880 --> 00:25.800
that's very close to the four walls to trigger the game over sequence.

7
00:26.400 --> 00:31.400
So we know that we have a 600 by 600 screen and the X and Y axis go to 300 and

8
00:35.340 --> 00:37.980
-300 at all four sides.

9
00:38.520 --> 00:42.960
So if we create a boundary box that say 280 here,

10
00:42.960 --> 00:45.840
280 here, -280 and -280,

11
00:46.290 --> 00:49.800
then as soon as the snake head touches that position,

12
00:50.160 --> 00:52.290
then once it zoomed past it,

13
00:52.410 --> 00:56.040
then we can say that the snake has pretty much hit the wall.

14
00:56.970 --> 00:59.010
Now this number is not perfect,

15
00:59.040 --> 01:02.370
it's just what I found to work after doing a lot of testing.

16
01:02.760 --> 01:06.060
And this is the case with a lot of games, you have to test it, run it,

17
01:06.270 --> 01:10.950
see if it works the way you want it to. But let's go ahead and add a comment,

18
01:11.010 --> 01:13.830
detect collision with a wall,

19
01:14.430 --> 01:16.140
and let's create our if statement.

20
01:16.440 --> 01:21.440
So what we want to say is if the snake.head has a X coordinate that is

21
01:26.250 --> 01:30.210
greater than 280, so it's gone too far to the right,

22
01:30.690 --> 01:35.690
or if it has a X coordinate that is less than -280,

23
01:37.290 --> 01:39.150
so it's gone too far to the left,

24
01:39.840 --> 01:43.080
or if it has a Y coordinate

25
01:43.290 --> 01:47.040
that's greater than 280, so too far to the top,

26
01:47.460 --> 01:52.460
or if it has a Y coordinate that is less than -280

27
01:53.580 --> 01:58.500
so it's gone too far to the bottom, if any of these four situations occur

28
01:58.560 --> 02:01.560
then it basically means that the snake has hit the wall.

29
02:01.980 --> 02:05.160
So what do we want to happen when the snake hits the wall? Well,

30
02:05.160 --> 02:10.160
we want to change this game_is_on to false because what that's going to allow us

31
02:11.070 --> 02:14.280
to do is to exit the while loop and basically

32
02:14.610 --> 02:17.580
end the snake movement and end the game.

33
02:18.270 --> 02:19.920
Let's go ahead and test this out.

34
02:20.040 --> 02:24.090
So let's let the snake hit the wall and you can see it no longer continues

35
02:24.090 --> 02:28.110
moving forward. But for the user, this might be a little bit confusing.

36
02:28.110 --> 02:32.790
It doesn't seem like it's actually game over. It seems like it's just crushed.

37
02:33.360 --> 02:35.820
So let's go ahead into our scoreboard

38
02:35.850 --> 02:40.380
which does a lot of the writing and let's create a new function

39
02:40.440 --> 02:42.510
which is going to be called game_over.

40
02:43.770 --> 02:48.770
And this game_over function is going to simply just write some texts,

41
02:49.260 --> 02:51.030
not the score, but instead

42
02:51.030 --> 02:56.030
it's just going to write 'GAME OVER' onto the screen using the same alignment and

43
02:56.760 --> 03:01.210
the same font. But instead of it at the position

44
03:01.210 --> 03:03.790
which we initially defined which is right at the top,

45
03:04.210 --> 03:09.130
let's go ahead and get it to go to the center, which is at (0, 0).

46
03:09.700 --> 03:12.040
So now when the snake hits the wall,

47
03:12.370 --> 03:14.770
not only does the snake stop moving,

48
03:15.040 --> 03:19.720
but we're also going to get the scoreboard to trigger the game over a sequence.

49
03:20.560 --> 03:24.730
So that means when it hits the wall, game over shows up in the middle of the

50
03:24.730 --> 03:28.360
screen, and you can still see the score that was written previously

51
03:28.660 --> 03:33.100
because we don't actually hit clear before we write this game over.

52
03:33.700 --> 03:37.720
And this means that the user can see that the game is over, the snake has stopped

53
03:37.750 --> 03:40.570
moving, and they can see their final score.

54
03:42.160 --> 03:46.780
So let's try to score some points. And once I've done that,

55
03:46.810 --> 03:51.550
I'm gonna hit a wall and it's game over and I can see that my score was 2.