WEBVTT

0
00:00.270 --> 00:03.120
Now we're pretty close to the end of the program.

1
00:03.240 --> 00:08.130
We've managed to do pretty much everything other than detect when the paddle

2
00:08.130 --> 00:12.630
actually misses a ball. And if the right paddle misses the ball,

3
00:12.780 --> 00:15.150
then the left player gets a point.

4
00:15.930 --> 00:20.340
And this should trigger a restart of the game where the ball goes back to the

5
00:20.340 --> 00:23.760
center and starts off in the opposite direction.

6
00:24.600 --> 00:26.010
In order to do this,

7
00:26.040 --> 00:30.240
to detect when a paddle has actually missed the ball, effectively,

8
00:30.240 --> 00:35.130
what we're checking is that the ball has gone past a certain point on the

9
00:35.130 --> 00:37.590
screen. So we know that we're already

10
00:37.590 --> 00:42.240
checking when the ball goes past this line and it's within 50 pixels of the

11
00:42.240 --> 00:42.960
paddle,

12
00:42.960 --> 00:46.740
then it should bounce back because that means the ball was caught by the paddle.

13
00:47.130 --> 00:51.150
But if it goes further than that, if it goes beyond that on the screen

14
00:51.450 --> 00:53.820
like over here or over here,

15
00:54.090 --> 00:57.450
then that means it's a miss and we should reset the ball.

16
00:58.200 --> 01:01.140
Have a think about how you might create this behavior,

17
01:01.650 --> 01:05.310
and as a challenge to yourself, see if you can complete the code.

18
01:09.920 --> 01:12.200
So I'm going to add another comment here

19
01:12.230 --> 01:17.230
which is going to be detect when right paddle misses.

20
01:19.490 --> 01:21.920
The right paddle has basically missed the ball

21
01:22.040 --> 01:27.040
if the ball.xcor is beyond 380.

22
01:28.430 --> 01:31.550
We know that the width of the screen is 800

23
01:31.640 --> 01:34.850
and we know that the paddle is at 350.

24
01:35.120 --> 01:40.040
So the paddle basically goes from 340 to 360,

25
01:40.370 --> 01:42.470
and if it's already gone past 380,

26
01:42.470 --> 01:47.120
then the paddle has definitely missed the ball. So in this situation,

27
01:47.150 --> 01:50.570
we're going to get the ball to reset its position.

28
01:51.170 --> 01:55.160
And this is going to be a method in the ball class.

29
01:55.730 --> 01:58.580
So let's create it and I'm just going to paste it in

30
01:58.580 --> 02:03.580
so I don't make any errors in terms of the naming. Resetting the position of the

31
02:04.640 --> 02:09.640
ball is going to involve getting the ball to go to the original position.

32
02:10.310 --> 02:15.230
So we can tell self to go to (0, 0).

33
02:16.010 --> 02:17.780
Now, if we run our code,

34
02:18.020 --> 02:22.490
you can see when the ball has gone past a certain point and it's not caught by

35
02:22.490 --> 02:25.490
the paddle, it goes back into the center.

36
02:26.810 --> 02:30.380
But we want the ball to go in the opposite direction

37
02:30.500 --> 02:35.300
once it's been missed by the right-sided player. It's time for the left-sided

38
02:35.300 --> 02:36.650
player to get a turn.

39
02:37.430 --> 02:41.420
So in addition to getting the ball to reset its position,

40
02:41.750 --> 02:45.830
we're going to get the ball to reverse its X-axis.

41
02:46.280 --> 02:49.610
So we're going to call self.bounce_x.

42
02:50.480 --> 02:53.630
Now notice how when the ball gets missed,

43
02:54.050 --> 02:56.420
then its direction gets reversed,

44
02:56.510 --> 03:00.610
it starts off in the center and it goes off in the opposite direction.

45
03:01.360 --> 03:05.100
Let's go ahead and define the left-sided paddle miss.

46
03:07.530 --> 03:12.530
And we can do that by simply saying ball.xcor is less than -380.

47
03:15.600 --> 03:19.920
And in this case, we're again going to get the ball to reset the position.

48
03:20.490 --> 03:20.670
Now,

49
03:20.670 --> 03:24.900
the reason why I've got these in separate if statements is because later on,

50
03:24.930 --> 03:29.610
we're going to need to detect when the right-sided player or the left-sided

51
03:29.610 --> 03:33.420
player actually gains a point. So when the right paddle misses,

52
03:33.720 --> 03:37.650
the left player gets a point, when the left paddle misses the right player

53
03:37.650 --> 03:38.490
gets a point.

54
03:39.660 --> 03:44.070
Now when the ball goes past the right edge and it's not caught by the paddle,

55
03:44.430 --> 03:47.430
then it starts off in the center and it moves to the left.

56
03:47.850 --> 03:51.480
When it's not caught by the left side of the paddle,

57
03:51.660 --> 03:54.210
it starts off in the center and it goes to the right.

58
03:55.080 --> 03:59.190
All that's left to do is to figure out how to do the scoring.

59
03:59.550 --> 04:02.130
And that is what we're going to do on the next lesson.