Now we're pretty close to the end of the program.
We've managed to do pretty much everything other than detect when the paddle
actually misses a ball. And if the right paddle misses the ball,
then the left player gets a point.
And this should trigger a restart of the game where the ball goes back to the
center and starts off in the opposite direction.
In order to do this,
to detect when a paddle has actually missed the ball, effectively,
what we're checking is that the ball has gone past a certain point on the
screen. So we know that we're already
checking when the ball goes past this line and it's within 50 pixels of the
paddle,
then it should bounce back because that means the ball was caught by the paddle.
But if it goes further than that, if it goes beyond that on the screen
like over here or over here,
then that means it's a miss and we should reset the ball.
Have a think about how you might create this behavior,
and as a challenge to yourself, see if you can complete the code.
So I'm going to add another comment here
which is going to be detect when right paddle misses.
The right paddle has basically missed the ball
if the ball.xcor is beyond 380.
We know that the width of the screen is 800
and we know that the paddle is at 350.
So the paddle basically goes from 340 to 360,
and if it's already gone past 380,
then the paddle has definitely missed the ball. So in this situation,
we're going to get the ball to reset its position.
And this is going to be a method in the ball class.
So let's create it and I'm just going to paste it in
so I don't make any errors in terms of the naming. Resetting the position of the
ball is going to involve getting the ball to go to the original position.
So we can tell self to go to (0, 0).
Now, if we run our code,
you can see when the ball has gone past a certain point and it's not caught by
the paddle, it goes back into the center.
But we want the ball to go in the opposite direction
once it's been missed by the right-sided player. It's time for the left-sided
player to get a turn.
So in addition to getting the ball to reset its position,
we're going to get the ball to reverse its X-axis.
So we're going to call self.bounce_x.
Now notice how when the ball gets missed,
then its direction gets reversed,
it starts off in the center and it goes off in the opposite direction.
Let's go ahead and define the left-sided paddle miss.
And we can do that by simply saying ball.xcor is less than -380.
And in this case, we're again going to get the ball to reset the position.
Now,
the reason why I've got these in separate if statements is because later on,
we're going to need to detect when the right-sided player or the left-sided
player actually gains a point. So when the right paddle misses,
the left player gets a point, when the left paddle misses the right player
gets a point.
Now when the ball goes past the right edge and it's not caught by the paddle,
then it starts off in the center and it moves to the left.
When it's not caught by the left side of the paddle,
it starts off in the center and it goes to the right.
All that's left to do is to figure out how to do the scoring.
And that is what we're going to do on the next lesson.