Game of Pong - V8.0

In this version of the game, we add the JOptionPane methods that allow us to read data in from the keyboard and to send data out via dialog boxes.

Because we can now use user input, we use this in two places:

  • setting up the player (name, number of games)

  • we give the user the option to quit the game after every game is finished.

Open your latest version of PongGame version 7. If you wish, you can use this solution here.

Save this as PongGameV8_0. Now, make changes to this work.

PongGame class - changes

Firstly note that we will only use the main tab (in this case PongGameV8_0) to read in data or report data. PongGameV8_0 then decides how data comes in/goes out (e.g. console or JOptionPane). Taking this approach, Ball and Paddle should never 'talk to the user'.

So, the only changes in this version update is in the PongGame class.

In the main tab, we firstly import the swing package as the first line of code.

import javax.swing.*;

Changes in setup() method

Change the setup method so that it is:

void setup()

   //setting up the paddle and the ball with hard-coded sizes.
   //This could be updated so that the values are read from the user at the start of the tournament.
   paddle = new Paddle(20,100);
   ball = new Ball(20.0);

   //create a player object with the data entered by the user 
   maxNumberOfGames = Integer.parseInt(JOptionPane.showInputDialog("Welcome to the Pong Tournament\n\n Please enter the number of games you would like to play: ","3"));
   player = new Player(JOptionPane.showInputDialog("Enter the player name (max 6 chars: "), maxNumberOfGames);

Ensure that you understand the code you have written in. For example:

  • The first use of the showInputDialog method returns a string which is then converted to an integer using Integer.parseInt

  • The second use of showInputDialog calls the method and uses the value returned directly in the Player constructor. So if "Siobhan" were entered in the dialog box, this would be the same as using

player = new Player("Siobhan", maxNumberOfGames);

Now run this code: You will get the following dialog boxes:

Entering number of games

Then you will get the following:

Entering player name

Changes in the draw() class - game functionality

We will add extra functionality to the game...when a player has finished a game s/he is asked do they wish to continue (only if there are still games left):

Amend your code so that it is:

  //If the player has no lives left in the current game
      //If the player has more games left in the tournament, 
      //display their score and ask them if they want to continue with the tournament.
      if (numberOfGamesPlayed < maxNumberOfGames){
         int reply = JOptionPane.showConfirmDialog(null,
            "Game Over! You scored " + score + ".\nWould you like to play the next game in your tournament?",
            "Play next game?",
         if (reply == JOptionPane.YES_OPTION){
            //player chooses to play the next game in their tournament.
            //player chooses to leave the tournament early.
         //the player has no more games left in the tournament 

Ensure that you understand the code.

Now play a tournament. When you finish any game you are given the option to quit the tournament:

Do you wish to continue? option

Changes to the tournamentOver() method - reporting results

Currently, we output to the console. We are going to change this so that our output is via a dialog box.

Change the tournamentOver() method code to be:

//method displays the player information, high scores and statistics, before exiting the program.
void tournamentOver()
   JOptionPane.showMessageDialog(null, player.getPlayerName() + ", your tournament is over! \n\n"  
                                 + "Number of games played: " + numberOfGamesPlayed
                                 + "\n\n"                     + player.toString()
                                 + "\n\nHighest Score: "      + player.highestScore()
                                 +   "\nLowest Score:  "      + player.lowestScore()
                                 +   "\nAverage Score: "      + player.averageScore());

Note that you are not changing any of the data being displayed, just their method of display.

Now when you play the tournament, you should get results in the format below:

Results of a tournament

Save your work.

Make sure you understand the code before moving onto PongGameV9_0.


If your code is not working, the solution can be found here.