9 – USING THE SCOREBOARD CLASS

 

 

IMPROVING THE BALL CLASS

 

We will start by adding two simple methods to the ball class.  This will help us to restart the game after a goal has occurred. 

 

ADDING moveToMiddle() TO BALL CLASS

 

First, we add the moveToMiddle() method that will simply move the ball to the middle.  This will be used after a goal is scored and at the very start of the game.

 

The code is simple.

 

   public void moveToMiddle()

   {

         this.x = (Gdx.graphics.getWidth()-width)/2;

         this.y = (Gdx.graphics.getHeight()-height)/2;

   }

 

ADDING randomVelocities() TO BALL CLASS

 

Next, we need to add a method that will simply generate random velocities.  This will be used at the very start of the game and after a goal to send the ball in a random direction.

 

This can be done in many ways.  We want to avoid a situation where velocityX is given a value of zero as then the ball would be stuck moving vertically and the game would be stuck.

 

Below is one possible implementation.  We have four arguments that provide the minimum and maximum velocities in both x and y directions.  These parameters would allow for us to produce faster velocities later in the game if we wanted to.

 

   public void randomVelocities(float minVx, float minVy, float maxVx, float maxVy)

   {

         if(Math.random() < 0.5)  //go right

         {

            this.velocityX = (int)(Math.random()*(maxVx-minVx)) + minVx;

         }

         else  //go left

         {

            this.velocityX = -1*((int)(Math.random()*(maxVx-minVx)) + minVx);

         }

        

         if(Math.random() < 0.5) //go up

         {    

            this.velocityY = (int)(Math.random()*(maxVy-minVy)) + minVy;

         }

         else  //go down

         {    

            this.velocityY = -(int)(Math.random()*(maxVy-minVy)) + minVy;

         }  

    }

 

ADDITIONS TO PONGGAME CLASS

 

We will now start working in the PointGame class to add a Scoreboard object.  At the same time, in order to display the score, we will add both a Font object and a SpriteRenderer object.

 

NEW DATAFIELDS

 

Add the following data fields to the PongGame class:

 

   private Scoreboard scoreboard;

   private SpriteBatch spriteRenderer;

   private BitmapFont font;

 

INITIALIZING OUR NEW DATAFIELDS

 

Initialize the new data fields inside the create method.

 

      FileHandle myFile3 = Gdx.files.internal("PressStart2p.fnt");

      font = new BitmapFont(myFile3);

     

      spriteRenderer = new SpriteBatch();

      scoreboard = new Scoreboard(15);  //max score of 15

 

Of course, you can use any font file you want.  And of course, you need to have the font files inside the assets folder.

 

RENDERING THE SCORES

 

The following method takes care of displaying the scores. We call it from the bottom of the render method.

 

      private void renderScores()

      {

            spriteRenderer.begin();

            String player1Text = "" + scoreboard.getScore1();

            String player2Text = "" + scoreboard.getScore2();

 

            TextBounds b1 = font.getBounds(player1Text);

 

            // Note: font.draw() draws y from top, not bottom

           

            float centreX = Gdx.graphics.getWidth() / 2;

           

            float xLeft = centreX - b1.width - 20;         //x of left score

            float y = Gdx.graphics.getHeight() - 10;

            font.draw(spriteRenderer, player1Text, xLeft, y);

           

            float xRight = centreX + 20 + 10;

            font.draw(spriteRenderer, player2Text, xRight, y);

           

            spriteRenderer.end();

     }

 

Add the method above in the PongGame class.  Then call this method from the bottom of the render method.

 

TEST YOUR GAME

 

You should see zeros at the top of the game.

 

LONGER GAMES

 

We now want to make the games last past the first goal.  We do so by altering the update method of the Ball class.

 

First off, the update method now also needs to get the Scoreboard object as parameter.

 

We then need to alter the if statements that detect the goals.

 

         //SCORE ON LEFT NET?

         if (getLeft() < 0)  //if score on left side

         {

             sb.incrementScore2();

             moveToMiddle();

             if(sb.gameOver() == false)

             {   

                randomVelocities(2,4,8,10);

             }

             else

             {

                velocityX=0;

                velocityY=0;

             }   

            

         }  

         //SCORE ON RIGHT NET?

         if (getRight() > Gdx.graphics.getWidth())

         {

             sb.incrementScore1();

             moveToMiddle();

             if(sb.gameOver() == false)

             {   

                randomVelocities(2,4,8,10);

             }

             else

             {

                velocityX=0;

                velocityY=0;

             }   

         }  

 

                                                }