1
00:00:00,180 --> 00:00:02,009
So we figured out how to detect 

2
00:00:02,040 --> 00:00:07,040
collision with the top and bottom walls and make our ball bounce off the walls.

3
00:00:07,470 --> 00:00:11,550
The next thing we need to do is to detect when there's a collision with the

4
00:00:11,550 --> 00:00:14,970
paddle and make the ball bounce off the paddle.

5
00:00:15,630 --> 00:00:18,840
This is probably going to be the hardest part of this program yet

6
00:00:19,080 --> 00:00:21,600
so I wanna talk you through the logic. Now,

7
00:00:21,630 --> 00:00:25,650
how can we know when the ball has hit the paddle? Now,

8
00:00:25,680 --> 00:00:30,680
normally we use the distance method to check what is the distance between the

9
00:00:30,690 --> 00:00:34,680
ball and the paddle. And if it's less than a certain amount,

10
00:00:34,710 --> 00:00:38,670
then we can be relatively sure that they've made contact with each other.

11
00:00:39,360 --> 00:00:42,540
So the code that we normally use would be something like this.

12
00:00:42,960 --> 00:00:47,190
We get our ball and we check its distance from the paddle.

13
00:00:47,820 --> 00:00:52,820
Now remember that our ball has a width of 20 pixels and our paddle has a

14
00:00:54,150 --> 00:00:57,930
width of 20 pixels. Normally we would say, well,

15
00:00:57,960 --> 00:01:02,430
if that distance between the two of them is less than 20, well,

16
00:01:02,430 --> 00:01:04,500
then they've probably made contact, right?

17
00:01:05,040 --> 00:01:09,300
But the problem occurs when the ball hits the paddle, not right in the center,

18
00:01:09,390 --> 00:01:11,370
but at the edge of the paddle,

19
00:01:11,760 --> 00:01:16,500
because this distance measures the center of the ball from the centre of the

20
00:01:16,500 --> 00:01:18,570
paddle as the distance.

21
00:01:18,600 --> 00:01:22,140
So you can see that this distance is way bigger than 20,

22
00:01:22,410 --> 00:01:25,200
so it's not going to register as a collision.

23
00:01:26,280 --> 00:01:28,920
How can we solve this problem? Well,

24
00:01:28,950 --> 00:01:33,150
we could add on an additional condition. We could check well

25
00:01:33,150 --> 00:01:38,150
if the ball has gone past a certain point on the X-axis,

26
00:01:38,700 --> 00:01:43,700
if it's gone far enough over to the right and it's within a 50 pixel distance of

27
00:01:44,880 --> 00:01:45,713
the paddle,

28
00:01:45,750 --> 00:01:50,750
then that also means it's made contact with the paddle. Inside our main.py

29
00:01:52,260 --> 00:01:57,260
I'm going to add a comment to detect collision with right paddle.

30
00:02:00,120 --> 00:02:04,200
So let's experiment with some numbers here and see if we can get the ball to

31
00:02:04,200 --> 00:02:06,720
bounce off the paddle. In this case,

32
00:02:06,750 --> 00:02:11,750
we're going to check if the ball.distance to the r_paddle is less than

33
00:02:15,570 --> 00:02:20,570
50 and the ball.xcor is greater than 340.

34
00:02:24,060 --> 00:02:25,830
If the ball has hit the right paddle,

35
00:02:26,040 --> 00:02:30,840
let's print something to the console to test our code. Let's print made contact.

36
00:02:32,940 --> 00:02:37,260
So if we run this code and I manage to make contact with the ball,

37
00:02:37,530 --> 00:02:41,910
you can see that it says 'Made contact' in our console.

38
00:02:42,390 --> 00:02:44,850
So instead of just printing made contact,

39
00:02:44,970 --> 00:02:49,650
what we want the ball to do is to again, to bounce. Now,

40
00:02:49,680 --> 00:02:52,680
this bounce is a little bit different from the last bounce,

41
00:02:53,040 --> 00:02:58,040
because the previous one was changing its Y coordinate so that it moved in the

42
00:02:58,470 --> 00:03:02,830
opposite along the vertical. But in this case,

43
00:03:02,860 --> 00:03:07,420
we actually want it to move along the opposite direction in the horizontal

44
00:03:07,420 --> 00:03:09,940
because the paddle is here and the ball is coming this way,

45
00:03:10,120 --> 00:03:14,590
we want it to go back this way. So instead of just calling this bounce,

46
00:03:14,620 --> 00:03:19,360
I'm going to right-click on it and then refactor and rename to bounce_

47
00:03:19,420 --> 00:03:23,860
y because this is bouncing in the Y-axis. Now,

48
00:03:24,220 --> 00:03:28,120
once we hit refactor, it'll change it here and it will change it here.

49
00:03:29,770 --> 00:03:33,460
Now on top of that, I'm going to define my bounce_x,

50
00:03:33,730 --> 00:03:37,600
so it bouncing in the X-axis, and this is very similar.

51
00:03:37,630 --> 00:03:39,910
We're going to change the x_move,

52
00:03:39,940 --> 00:03:44,940
so the amount that the ball moves each time when the move method is called and

53
00:03:45,340 --> 00:03:48,100
I'm going to multiply it by -1,

54
00:03:48,430 --> 00:03:51,160
so reversing the X direction.

55
00:03:51,850 --> 00:03:54,760
And then when our collision is detected,

56
00:03:54,790 --> 00:03:58,660
we're going to get the ball to bounce in the X direction.

57
00:03:59,440 --> 00:04:01,360
So now if we run our code,

58
00:04:01,390 --> 00:04:04,930
you can see that even though this only works on the right paddle,

59
00:04:05,200 --> 00:04:07,030
when the ball hits the right paddle

60
00:04:07,270 --> 00:04:11,500
it moves back and it bounces back towards the left.

61
00:04:11,860 --> 00:04:16,570
It changes the direction that it was moving in the X-axis.

62
00:04:17,740 --> 00:04:22,740
So now lets add the collision for both paddles because they both need to bounce

63
00:04:25,690 --> 00:04:26,920
in the X-axis

64
00:04:27,280 --> 00:04:32,280
and so we're going to tag onto this a 'or' statement and we're going to say

65
00:04:32,410 --> 00:04:37,410
or if the ball.distance to the left paddle is also less than 50

66
00:04:41,860 --> 00:04:43,300
and on top of that

67
00:04:43,450 --> 00:04:48,450
the ball.xcor is less than -340.

68
00:04:53,080 --> 00:04:58,080
So this basically checks to see if the ball has gone far enough to the left as

69
00:04:58,360 --> 00:05:03,360
to be past the paddle and its within a distance of 50 pixels from the left

70
00:05:05,230 --> 00:05:08,080
paddle. So if we run the code right now,

71
00:05:08,110 --> 00:05:12,370
you can see that not only does our ball bounce when it hits the right.

72
00:05:14,230 --> 00:05:16,630
So now when we run the program again,

73
00:05:16,660 --> 00:05:20,500
you can see that not only does the ball bounce on the right paddle,

74
00:05:20,740 --> 00:05:24,940
it also bounces when it hits the left paddle.

75
00:05:26,170 --> 00:05:30,370
Now there's a little bit more tweaking that we can do with the coordinates because

76
00:05:30,370 --> 00:05:33,160
you can see that when the ball hits the paddle,

77
00:05:33,220 --> 00:05:38,220
it goes a little bit too far before it actually reverses direction.

78
00:05:39,160 --> 00:05:44,160
So we can change this to a slightly smaller number if we want it to bounce

79
00:05:45,580 --> 00:05:48,370
before it's within the actual paddle.

80
00:05:48,700 --> 00:05:53,700
So we can change this to 320 and this one also to 320 and notice what

81
00:05:55,000 --> 00:05:59,000
happens now. Before it hits the actual paddle,

82
00:05:59,180 --> 00:06:04,180
it now actually bounces and it doesn't look like as if the ball's fallen into the

83
00:06:05,120 --> 00:06:08,960
paddle before it turns back. And there you have it.

84
00:06:09,080 --> 00:06:12,530
Now we just have to figure out what should happen when the paddle misses the

85
00:06:12,530 --> 00:06:16,220
ball. For all of that and more, I'll see you in the next lesson.

