4 – USING THE BALL CLASS

 

 

INTRO

 

In this section, we will use the ball class in order to make a semi functioning game.  We will be working in the PongGame class.

 

Thankfully, this is short.

 

DATA FIELD

 

We add a new data field for the ball.

 

   private Ball b;

 

CREATE METHOD

 

In the create method, we create the ball. We will use dummy values for now.  So x and y can be say 250.  Really we’d want them to be in the middle of the room. The values for velocityX and velocityY will be 0.5f and 3.  Finally, we’ll use a size of 5 by 5.

 

      b = new Ball(250,250,0.5f,3,5,5);

 

It is your job to center the ball in the room.

 

RENDER METHOD

 

There are two steps to do inside the render method.  Near the top, we must update the ball’s location (very easy) and, near the bottom, we must draw the ball (again, very easy).

 

To update the ball’s location, we simply call the update method inside the ball class. 

 

Call the update method on the ball.

 

To render the ball, simply draw the ball in a very similar way as you did with the paddles.

 

Write the code to render the ball.

 

TEST IT

 

You now have most of a Pong game.  Of course, we aren’t tracking scores yet or resetting the game after a goal.  That will come next.