So we figured out how to detect
collision with the top and bottom walls and make our ball bounce off the walls.
The next thing we need to do is to detect when there's a collision with the
paddle and make the ball bounce off the paddle.
This is probably going to be the hardest part of this program yet
so I wanna talk you through the logic. Now,
how can we know when the ball has hit the paddle? Now,
normally we use the distance method to check what is the distance between the
ball and the paddle. And if it's less than a certain amount,
then we can be relatively sure that they've made contact with each other.
So the code that we normally use would be something like this.
We get our ball and we check its distance from the paddle.
Now remember that our ball has a width of 20 pixels and our paddle has a
width of 20 pixels. Normally we would say, well,
if that distance between the two of them is less than 20, well,
then they've probably made contact, right?
But the problem occurs when the ball hits the paddle, not right in the center,
but at the edge of the paddle,
because this distance measures the center of the ball from the centre of the
paddle as the distance.
So you can see that this distance is way bigger than 20,
so it's not going to register as a collision.
How can we solve this problem? Well,
we could add on an additional condition. We could check well
if the ball has gone past a certain point on the X-axis,
if it's gone far enough over to the right and it's within a 50 pixel distance of
the paddle,
then that also means it's made contact with the paddle. Inside our main.py
I'm going to add a comment to detect collision with right paddle.
So let's experiment with some numbers here and see if we can get the ball to
bounce off the paddle. In this case,
we're going to check if the ball.distance to the r_paddle is less than
50 and the ball.xcor is greater than 340.
If the ball has hit the right paddle,
let's print something to the console to test our code. Let's print made contact.
So if we run this code and I manage to make contact with the ball,
you can see that it says 'Made contact' in our console.
So instead of just printing made contact,
what we want the ball to do is to again, to bounce. Now,
this bounce is a little bit different from the last bounce,
because the previous one was changing its Y coordinate so that it moved in the
opposite along the vertical. But in this case,
we actually want it to move along the opposite direction in the horizontal
because the paddle is here and the ball is coming this way,
we want it to go back this way. So instead of just calling this bounce,
I'm going to right-click on it and then refactor and rename to bounce_
y because this is bouncing in the Y-axis. Now,
once we hit refactor, it'll change it here and it will change it here.
Now on top of that, I'm going to define my bounce_x,
so it bouncing in the X-axis, and this is very similar.
We're going to change the x_move,
so the amount that the ball moves each time when the move method is called and
I'm going to multiply it by -1,
so reversing the X direction.
And then when our collision is detected,
we're going to get the ball to bounce in the X direction.
So now if we run our code,
you can see that even though this only works on the right paddle,
when the ball hits the right paddle
it moves back and it bounces back towards the left.
It changes the direction that it was moving in the X-axis.
So now lets add the collision for both paddles because they both need to bounce
in the X-axis
and so we're going to tag onto this a 'or' statement and we're going to say
or if the ball.distance to the left paddle is also less than 50
and on top of that
the ball.xcor is less than -340.
So this basically checks to see if the ball has gone far enough to the left as
to be past the paddle and its within a distance of 50 pixels from the left
paddle. So if we run the code right now,
you can see that not only does our ball bounce when it hits the right.
So now when we run the program again,
you can see that not only does the ball bounce on the right paddle,
it also bounces when it hits the left paddle.
Now there's a little bit more tweaking that we can do with the coordinates because
you can see that when the ball hits the paddle,
it goes a little bit too far before it actually reverses direction.
So we can change this to a slightly smaller number if we want it to bounce
before it's within the actual paddle.
So we can change this to 320 and this one also to 320 and notice what
happens now. Before it hits the actual paddle,
it now actually bounces and it doesn't look like as if the ball's fallen into the
paddle before it turns back. And there you have it.
Now we just have to figure out what should happen when the paddle misses the
ball. For all of that and more, I'll see you in the next lesson.