WEBVTT

0
00:00.180 --> 00:02.010
So we figured out how to detect 

1
00:02.040 --> 00:07.040
collision with the top and bottom walls and make our ball bounce off the walls.

2
00:07.470 --> 00:11.550
The next thing we need to do is to detect when there's a collision with the

3
00:11.550 --> 00:14.970
paddle and make the ball bounce off the paddle.

4
00:15.630 --> 00:18.840
This is probably going to be the hardest part of this program yet

5
00:19.080 --> 00:21.600
so I wanna talk you through the logic. Now,

6
00:21.630 --> 00:25.650
how can we know when the ball has hit the paddle? Now,

7
00:25.680 --> 00:30.680
normally we use the distance method to check what is the distance between the

8
00:30.690 --> 00:34.680
ball and the paddle. And if it's less than a certain amount,

9
00:34.710 --> 00:38.670
then we can be relatively sure that they've made contact with each other.

10
00:39.360 --> 00:42.540
So the code that we normally use would be something like this.

11
00:42.960 --> 00:47.190
We get our ball and we check its distance from the paddle.

12
00:47.820 --> 00:52.820
Now remember that our ball has a width of 20 pixels and our paddle has a

13
00:54.150 --> 00:57.930
width of 20 pixels. Normally we would say, well,

14
00:57.960 --> 01:02.430
if that distance between the two of them is less than 20, well,

15
01:02.430 --> 01:04.500
then they've probably made contact, right?

16
01:05.040 --> 01:09.300
But the problem occurs when the ball hits the paddle, not right in the center,

17
01:09.390 --> 01:11.370
but at the edge of the paddle,

18
01:11.760 --> 01:16.500
because this distance measures the center of the ball from the centre of the

19
01:16.500 --> 01:18.570
paddle as the distance.

20
01:18.600 --> 01:22.140
So you can see that this distance is way bigger than 20,

21
01:22.410 --> 01:25.200
so it's not going to register as a collision.

22
01:26.280 --> 01:28.920
How can we solve this problem? Well,

23
01:28.950 --> 01:33.150
we could add on an additional condition. We could check well

24
01:33.150 --> 01:38.150
if the ball has gone past a certain point on the X-axis,

25
01:38.700 --> 01:43.700
if it's gone far enough over to the right and it's within a 50 pixel distance of

26
01:44.880 --> 01:45.713
the paddle,

27
01:45.750 --> 01:50.750
then that also means it's made contact with the paddle. Inside our main.py

28
01:52.260 --> 01:57.260
I'm going to add a comment to detect collision with right paddle.

29
02:00.120 --> 02:04.200
So let's experiment with some numbers here and see if we can get the ball to

30
02:04.200 --> 02:06.720
bounce off the paddle. In this case,

31
02:06.750 --> 02:11.750
we're going to check if the ball.distance to the r_paddle is less than

32
02:15.570 --> 02:20.570
50 and the ball.xcor is greater than 340.

33
02:24.060 --> 02:25.830
If the ball has hit the right paddle,

34
02:26.040 --> 02:30.840
let's print something to the console to test our code. Let's print made contact.

35
02:32.940 --> 02:37.260
So if we run this code and I manage to make contact with the ball,

36
02:37.530 --> 02:41.910
you can see that it says 'Made contact' in our console.

37
02:42.390 --> 02:44.850
So instead of just printing made contact,

38
02:44.970 --> 02:49.650
what we want the ball to do is to again, to bounce. Now,

39
02:49.680 --> 02:52.680
this bounce is a little bit different from the last bounce,

40
02:53.040 --> 02:58.040
because the previous one was changing its Y coordinate so that it moved in the

41
02:58.470 --> 03:02.830
opposite along the vertical. But in this case,

42
03:02.860 --> 03:07.420
we actually want it to move along the opposite direction in the horizontal

43
03:07.420 --> 03:09.940
because the paddle is here and the ball is coming this way,

44
03:10.120 --> 03:14.590
we want it to go back this way. So instead of just calling this bounce,

45
03:14.620 --> 03:19.360
I'm going to right-click on it and then refactor and rename to bounce_

46
03:19.420 --> 03:23.860
y because this is bouncing in the Y-axis. Now,

47
03:24.220 --> 03:28.120
once we hit refactor, it'll change it here and it will change it here.

48
03:29.770 --> 03:33.460
Now on top of that, I'm going to define my bounce_x,

49
03:33.730 --> 03:37.600
so it bouncing in the X-axis, and this is very similar.

50
03:37.630 --> 03:39.910
We're going to change the x_move,

51
03:39.940 --> 03:44.940
so the amount that the ball moves each time when the move method is called and

52
03:45.340 --> 03:48.100
I'm going to multiply it by -1,

53
03:48.430 --> 03:51.160
so reversing the X direction.

54
03:51.850 --> 03:54.760
And then when our collision is detected,

55
03:54.790 --> 03:58.660
we're going to get the ball to bounce in the X direction.

56
03:59.440 --> 04:01.360
So now if we run our code,

57
04:01.390 --> 04:04.930
you can see that even though this only works on the right paddle,

58
04:05.200 --> 04:07.030
when the ball hits the right paddle

59
04:07.270 --> 04:11.500
it moves back and it bounces back towards the left.

60
04:11.860 --> 04:16.570
It changes the direction that it was moving in the X-axis.

61
04:17.740 --> 04:22.740
So now lets add the collision for both paddles because they both need to bounce

62
04:25.690 --> 04:26.920
in the X-axis

63
04:27.280 --> 04:32.280
and so we're going to tag onto this a 'or' statement and we're going to say

64
04:32.410 --> 04:37.410
or if the ball.distance to the left paddle is also less than 50

65
04:41.860 --> 04:43.300
and on top of that

66
04:43.450 --> 04:48.450
the ball.xcor is less than -340.

67
04:53.080 --> 04:58.080
So this basically checks to see if the ball has gone far enough to the left as

68
04:58.360 --> 05:03.360
to be past the paddle and its within a distance of 50 pixels from the left

69
05:05.230 --> 05:08.080
paddle. So if we run the code right now,

70
05:08.110 --> 05:12.370
you can see that not only does our ball bounce when it hits the right.

71
05:14.230 --> 05:16.630
So now when we run the program again,

72
05:16.660 --> 05:20.500
you can see that not only does the ball bounce on the right paddle,

73
05:20.740 --> 05:24.940
it also bounces when it hits the left paddle.

74
05:26.170 --> 05:30.370
Now there's a little bit more tweaking that we can do with the coordinates because

75
05:30.370 --> 05:33.160
you can see that when the ball hits the paddle,

76
05:33.220 --> 05:38.220
it goes a little bit too far before it actually reverses direction.

77
05:39.160 --> 05:44.160
So we can change this to a slightly smaller number if we want it to bounce

78
05:45.580 --> 05:48.370
before it's within the actual paddle.

79
05:48.700 --> 05:53.700
So we can change this to 320 and this one also to 320 and notice what

80
05:55.000 --> 05:59.000
happens now. Before it hits the actual paddle,

81
05:59.180 --> 06:04.180
it now actually bounces and it doesn't look like as if the ball's fallen into the

82
06:05.120 --> 06:08.960
paddle before it turns back. And there you have it.

83
06:09.080 --> 06:12.530
Now we just have to figure out what should happen when the paddle misses the

84
06:12.530 --> 06:16.220
ball. For all of that and more, I'll see you in the next lesson.