Part 6

Adding boundaries to the Racquet


Now that our “Racquet” can move, we need to get it to collide with the ball. Add the code as shown inside the “Racquet” file to get started.

In order to collide with something we need to know its bounds, the coordinates that contain the object. If something is also in these coordinates, we’ve had a collision! In the “getBounds” function, we return a “Rectangle” which is an area of coordinates.

We also create a function called “getTopY” to return the top “y” coordinate of the “Racquet.” The “Ball” will need to use this information later.

Adding boundaries to the Ball


Next, navigate back to the “Ball” file. Just like with the “Racquet” we need to be able to get the bounds of the ball. Complete the code as shown to do this.

For the sake of simplicity, we use a “Rectangle” to calculate the bounds of the ball. We do this because it is easier overall, for both the programmer and the program itself.

This will result in a few cases where the ball detects a collision where it shouldn’t (mainly in the corners), but from a gameplay standpoint it isn’t very noticeable.


Creating the Ball collision function


Now that we have the bounds/rectangle for both the “Ball” and “Rectangle”, we can detect if they have collided. Add the code as shown to do this.

The “Rectangle” class has a function called “intersects” that detects if it has collided with a different “Rectangle.” We use this function here to detect if the “Ball” has collided with the “Rectangle.”

Creating the Ball collision function


Now that we have the bounds/rectangle for both the “Ball” and “Rectangle”, we can detect if they have collided. Add the code as shown to do this.

The “Rectangle” class has a function called “intersects” that detects if it has collided with a different “Rectangle.” We use this function here to detect if the “Ball” has collided with the “Rectangle.”

Making the Ball bounce off the Racquet


Now that we can detect when the “Ball” and “Racquet” collide, we need to do something when that happens. Still inside the “Ball” file, modify the “move” function so it looks like what is shown here.

First, since we are colliding with the “Racquet” now, we need to change what happens when the “Ball” hits the bottom of the screen. Modify line 37, so we tell the game that we lost, we’ll make this “gameOver” function in a bit.

Next we add an if statement to check if we have made a collision. If we have, the first thing we do is get the ball to start moving up by making its “ya” negative.

Then we set the “y” of the “Ball” to be right on top of the “Racquet.” We do this in case the “Ball” collides with the bottom of the “Racquet”.

Lastly, after we hit the “Ball,” we increase the difficulty of the game by increasing the speed by 1.

Adding the gameOver function


Next, let’s code the gameOver function we were missing. To do this, go back into the “Main” file and add the code as shown here.

This will pop up a small “Game Over” window when you lose the game. It will also end the game after with “System.exit.”

Testing the game


After this test out the code to see if everything is working. If done correctly you should be able to play the game! If things aren’t working, double check your code for any errors.

The core game is now complete! There is just one more small thing we’re going to add to finish things off.