2 – USING THE PADDLE CLASS

 

 

INTRO

 

In this section, we will code in the PongGame class to add paddles to the game.  The paddles will respond to keyboard input.

 

BLANK PONGGAME CLASS

 

We start with the following blank class.  We add a few comments inside the render method to organize ourselves.

 

public class PongGame extends ApplicationAdapter

{

   private ShapeRenderer renderer;

 

   @Override

   public void create ()

   {

 

   }

 

   @Override

   public void render()

   {

        //Update

       

        //Clear the screen

       

        //Render graphics

   }

}

 

CLEARING THE SCREEN

 

The following statements will clear the screen.  We will place them right under the corresponding comment.

 

   Gdx.gl.glClearColor(0, 0, 0, 1);

   Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

 

DATA FIELDS

 

The template above already has the ShapeRenderer datafield.  We now need to consider what other data fields are needed for the game.  Essentially, we need to ask ourselves what objects are in the game.

 

The game contains three objects: a ball and two paddles. 

 

For now, our focus will be on the paddles. We will worry about the ball later.

 

The game has two paddles so we add two Paddle objects as data fields.  Note that the ShapeRendererdata field was already in our template from above.

 

   private ShapeRenderer renderer;

   private Paddle p1;

   private Paddle p2;

 

THE CREATE METHOD

 

In the create method, we have to take care of initializing all the data fields.  The ShapeRenderer is easy.

 

   renderer = new ShapeRenderer();

 

We now need to create the first Paddle p1.  The Paddle constructor requires for us to provide 4 parameters:  x, y, width and height.  We’ll do this over multiple lines in order to be organized and clear.

 

We create two local variables that temporarily store the width and height of 10 and 90.  The x value will be 50. 

 

We want the y value to be at the center.  Because the x and y coordinates of the paddle are actually at the bottom left, the middle can be found by taking the room height, subtracting the height of the paddle and then dividing by two.

 

Now that we have values for each parameter, we can go ahead and construct the object.

 

      float paddleWidth = 10;

      float paddleHeight = 90;    

      float p1x = 50;

      float p1y = (Gdx.graphics.getHeight()-paddleHeight)/2;

      p1 = new Paddle(p1x,p1y,paddleWidth,paddleHeight);

 

We do the same for Paddle p2.  Because the paddle is the same size as p1, we only need to figure out the x and y values.  While y is the same, we have some work to do for x.

 

We want p2 to be the same distance from the right hand side as p1 is from the left side.  So we want it to be 50 pixels from the right. However, the x coordinate refers to the left side of the paddle.  So x will the the room’s width minus the 50 pixels minus the paddle’s width.

 

      float p2x = Gdx.graphics.getWidth()-paddleWidth-50;

      float p2y = (Gdx.graphics.getHeight()-paddleHeight)/2;

      p2 = new Paddle(p2x,p2y,paddleWidth,paddleHeight);

 

THE RENDER METHOD

 

There are two sections in the render method that we have to fill.  In the update section, we have to update the location of the paddles based on keyboard input.  In the render section, we have to draw the paddles.

 

We will actually start with the rendering section.  Its pretty easy.  For each paddle, we simply draw a rectangle using the paddle’s datafields.

 

        //Render paddles

      renderer.begin(ShapeType.Filled);

      renderer.setColor(Color.BLUE);

      renderer.rect(p1.getX(), p1.getY(), p1.getWidth(), p1.getHeight());

      renderer.rect(p2.getX(), p2.getY(), p2.getWidth(), p2.getHeight());

      renderer.end();

 

Now we will look at the update section.  It is straight forward.  For p1, we check if either w or s is hit and move in the corresponding direction. For p2, we check the up and down arrows.

 

For now, we move up or down by 5 pixels.  You may decide to change that up later.

 

        //Update - Check Keyboard input

        if(Gdx.input.isKeyPressed(Input.Keys.UP))

        {

              float newY = p2.getY() + 5;

              p2.setY(newY);

        }

        if(Gdx.input.isKeyPressed(Input.Keys.DOWN))

        {

              float newY = p2.getY() - 5;

              p2.setY(newY);

        }    

        if(Gdx.input.isKeyPressed(Input.Keys.W))

        {

              float newY = p1.getY() + 5;

              p1.setY(newY);

        }    

        if(Gdx.input.isKeyPressed(Input.Keys.S))

        {

              float newY = p1.getY() - 5;

              p1.setY(newY);

       }   

 

TEST IT

 

Run DesktopLauncher.  You should now be able to move your paddles.