Today in this article I will show you how to make a Pong game in Java. I have made three classes in this game.

Pong game in Java


This is a two-player game in which players need to touch the ball with the paddle if the ball will not touch the paddle then the other player will get numbers and vice versa.

  1. The Main class from which all things are controlled.
  2. Ball class that draws the ball and also check its collision with the paddles.
  3. Paddle class that draws the paddles and also move the paddles up and down.

Pong game in Java

Main Class
  1. public class Ball implements Runnable {
  2. //Global variable
  3. int x, y, xDirection, yDirection;
  4. Rectangle ball;
  5. Paddle p1 = new Paddle(15, 140, 1); //paddle class objects
  6. Paddle p2 = new Paddle(770, 140, 2);
  7. //Score
  8. int p1Score, p2Score;
  9. //Ball Image
  10. Image BallImage;
  11. public Ball(int x, int y)
  12. {
  13. p1Score = p2Score = 0;
  14. this.x = x;
  15. this.y = y;
  16. Random r = new Random(); //creats Random numbers
  17. int rDir = r.nextInt(1);
  18. if (rDir == 0)
  19. {
  20. rDir--;
  21. }
  22. setXDirection(rDir);
  23. int yrDir = r.nextInt(1);
  24. if (yrDir == 0)
  25. {
  26. yrDir--;
  27. }
  28. setYDirection(yrDir);
  29. ball = new Rectangle(this.x, this.y, 15, 15);
  30. }
  31. //for set the xDirection of the ball
  32. public void setXDirection(int xdir)
  33. {
  34. xDirection = xdir;
  35. }
  36. //for set the yDirection of the ball
  37. public void setYDirection(int ydir)
  38. {
  39. yDirection = ydir;
  40. }
  41. public void draw(Graphics g)
  42. {
  43. java.net.URL urlBall = getClass().getResource("Ball.png"); //get ball image from the project folder
  44. BallImage = getImage(urlBall);
  45. g.drawImage(BallImage, this.ball.x, this.ball.y, null);
  46. }
  47. //Collision method
  48. public void collision()
  49. { //for paddle 1 collision
  50. if (ball.intersects(p1.paddle))
  51. {
  52. setXDirection(+1);
  53. }
  54. //for paddle 2 collision
  55. if (ball.intersects(p2.paddle))
  56. {
  57. setXDirection(-1);
  58. }
  59. }
  60. //Move method
  61. public void move()
  62. {
  63. collision();
  64. ball.x += xDirection;
  65. ball.y += yDirection;
  66. //Bounce the ball when edge is detected
  67. if (ball.x <= 0)
  68. {
  69. setXDirection(+1);
  70. p2Score++;
  71. }
  72. if (ball.x >= 785)
  73. {
  74. setXDirection(-1);
  75. p1Score++;
  76. }
  77. if (ball.y <= 0)
  78. {
  79. setYDirection(+1);
  80. }
  81. if (ball.y >= 485)
  82. {
  83. setYDirection(-1);
  84. }
  85. }
  86. //Run method Thread Start from here
  87. public void run()
  88. {
  89. try {
  90. while (true)
  91. {
  92. move();
  93. Thread.sleep(2);
  94. }
  95. } catch (Exception e)
  96. {
  97. System.err.println(e.getMessage());
  98. }
  99. }
  100. }
Ball Class
  1. public class Ball implements Runnable
  2. {
  3. //Global variable
  4. int x, y, xDirection, yDirection;
  5. Rectangle ball;
  6. Paddle p1 = new Paddle(15, 140, 1); //paddle class objects
  7. Paddle p2 = new Paddle(770, 140, 2);
  8. //Score
  9. int p1Score, p2Score
  10. //Ball Image
  11. Image BallImage;
  12. public Ball(int x, int y)
  13. {
  14. p1Score = p2Score = 0;
  15. this.x = x;
  16. this.y = y;
  17. Random r = new Random(); //creats Random numbers
  18. int rDir = r.nextInt(1);
  19. if (rDir == 0)
  20. {
  21. rDir--;
  22. }
  23. setXDirection(rDir);
  24. int yrDir = r.nextInt(1);
  25. if (yrDir == 0)
  26. {
  27. yrDir--;
  28. }
  29. setYDirection(yrDir);
  30. ball = new Rectangle(this.x, this.y, 15, 15);
  31. }
  32. //for set the xDirection of the ball
  33. public void setXDirection(int xdir)
  34. {
  35. xDirection = xdir;
  36. }
  37. //for set the yDirection of the ball
  38. public void setYDirection(int ydir)
  39. {
  40. yDirection = ydir;
  41. }
  42. public void draw(Graphics g)
  43. {
  44. java.net.URL urlBall = getClass().getResource("Ball.png"); //get ball image from the project folder
  45. BallImage = getImage(urlBall);
  46. g.drawImage(BallImage, this.ball.x, this.ball.y, null);
  47. }
  48. //Collision method
  49. public void collision()
  50. {
  51. //for paddle 1 collision
  52. if (ball.intersects(p1.paddle))
  53. {
  54. setXDirection(+1);
  55. }
  56. //for paddle 2 collision
  57. if (ball.intersects(p2.paddle))
  58. {
  59. setXDirection(-1);
  60. }
  61. }
  62. //Move method
  63. public void move()
  64. {
  65. collision();
  66. ball.x += xDirection;
  67. ball.y += yDirection;
  68. //Bounce the ball when edge is detected
  69. if (ball.x <= 0)
  70. {
  71. setXDirection(+1);
  72. p2Score++;
  73. }
  74. if (ball.x >= 785)
  75. {
  76. setXDirection(-1);
  77. p1Score++;
  78. }
  79. if (ball.y <= 0)
  80. {
  81. setYDirection(+1);
  82. }
  83. if (ball.y >= 485)
  84. {
  85. setYDirection(-1);
  86. }
  87. }
  88. //Run method Thread Start from here
  89. public void run()
  90. {
  91. try {
  92. while (true)
  93. {
  94. move();
  95. Thread.sleep(2)
  96. }
  97. } catch (Exception e)
  98. {
  99. System.err.println(e.getMessage());
  100. }
  101. }
  102. }
Paddle Class
  1. public class Paddle implements Runnable
  2. {
  3. //Global Variables
  4. int x, y, yDirection, id;
  5. Rectangle paddle;
  6. Image PaddleImage1;
  7. Image PaddleImage2;
  8. public Paddle(int x, int y, int id)
  9. {
  10. this.x = x;
  11. this.y = y;
  12. this.id = id;
  13. paddle = new Rectangle(x, y, 10, 80);
  14. }
  15. //Keypressed event function
  16. public void keyPressed(KeyEvent e)
  17. {
  18. switch (id)
  19. {
  20. //for Paddle 1
  21. case 1:
  22. if (e.getKeyCode() == e.VK_W)
  23. {
  24. setYDirection(-1);
  25. }
  26. if (e.getKeyCode() == e.VK_S)
  27. {
  28. setYDirection(1);
  29. }
  30. break;
  31. //for Paddle 2
  32. case 2:
  33. if (e.getKeyCode() == e.VK_UP)
  34. {
  35. setYDirection(-1);
  36. }
  37. if (e.getKeyCode() == e.VK_DOWN)
  38. {
  39. setYDirection(+1);
  40. }
  41. break;
  42. default:
  43. System.out.println("Enter correct id");
  44. break;
  45. }
  46. }
  47. public void keyReleased(KeyEvent e)
  48. {
  49. switch (id)
  50. {
  51. //for Paddle 1
  52. case 1:
  53. if (e.getKeyCode() == e.VK_W)
  54. {
  55. setYDirection(0);
  56. }
  57. if (e.getKeyCode() == e.VK_S)
  58. {
  59. setYDirection(0);
  60. }
  61. break;
  62. //for Paddle 2
  63. case 2:
  64. if (e.getKeyCode() == e.VK_UP)
  65. {
  66. setYDirection(0);
  67. }
  68. if (e.getKeyCode() == e.VK_DOWN)
  69. {
  70. setYDirection(0);
  71. }
  72. break;
  73. default:
  74. System.out.println("Enter correct id");
  75. break;
  76. }
  77. }
  78. public void setYDirection(int ydir)
  79. {
  80. yDirection = ydir;
  81. }
  82. public void move()
  83. {
  84. paddle.y += yDirection;
  85. if (paddle.y <= 30)
  86. {
  87. paddle.y = 30;
  88. }
  89. if (paddle.y >= 405)
  90. {
  91. paddle.y = 405;
  92. }
  93. }
  94. public void draw(Graphics g)
  95. {
  96. java.net.URL url1 = getClass().getResource("PaddleImage1.png"); //get image
  97. PaddleImage1 = getImage(url1);
  98. java.net.URL url2 = getClass().getResource("PaddleImage2.png");
  99. PaddleImage2 = getImage(url2);
  100. switch (id)
  101. {
  102. //for Paddle 1
  103. case 1:
  104. g.drawImage(PaddleImage1, this.paddle.x, this.paddle.y, null);
  105. break;
  106. //for Paddle 2
  107. case 2:
  108. g.drawImage(PaddleImage2, this.paddle.x, this.paddle.y, null);
  109. break;
  110. default:
  111. System.out.println("Enter correct id");
  112. }
  113. }
  114. @Override
  115. //Thread Start from here
  116. public void run()
  117. {
  118. try
  119. {
  120. while (true)
  121. {
  122. move();
  123. Thread.sleep(4);
  124. }
  125. } catch (Exception e)
  126. {
  127. System.err.println(e.getMessage());
  128. }
  129. }
  130. }
I have also attached the source code and JAR file with it. So you can download it and run it on your PC.