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.
- The Main class from which all things are controlled.
- Ball class that draws the ball and also check its collision with the paddles.
- Paddle class that draws the paddles and also move the paddles up and down.
Main Class
- public class Ball implements Runnable {
-
- int x, y, xDirection, yDirection;
- Rectangle ball;
- Paddle p1 = new Paddle(15, 140, 1);
- Paddle p2 = new Paddle(770, 140, 2);
-
- int p1Score, p2Score;
-
- Image BallImage;
- public Ball(int x, int y)
- {
- p1Score = p2Score = 0;
- this.x = x;
- this.y = y;
- Random r = new Random();
- int rDir = r.nextInt(1);
- if (rDir == 0)
- {
- rDir--;
- }
- setXDirection(rDir);
- int yrDir = r.nextInt(1);
- if (yrDir == 0)
- {
- yrDir--;
- }
- setYDirection(yrDir);
- ball = new Rectangle(this.x, this.y, 15, 15);
- }
-
- public void setXDirection(int xdir)
- {
- xDirection = xdir;
- }
-
- public void setYDirection(int ydir)
- {
- yDirection = ydir;
- }
- public void draw(Graphics g)
- {
- java.net.URL urlBall = getClass().getResource("Ball.png");
- BallImage = getImage(urlBall);
- g.drawImage(BallImage, this.ball.x, this.ball.y, null);
- }
-
- public void collision()
- {
- if (ball.intersects(p1.paddle))
- {
- setXDirection(+1);
- }
-
- if (ball.intersects(p2.paddle))
- {
- setXDirection(-1);
- }
- }
-
- public void move()
- {
- collision();
- ball.x += xDirection;
- ball.y += yDirection;
-
- if (ball.x <= 0)
- {
- setXDirection(+1);
- p2Score++;
- }
- if (ball.x >= 785)
- {
- setXDirection(-1);
- p1Score++;
- }
- if (ball.y <= 0)
- {
- setYDirection(+1);
- }
- if (ball.y >= 485)
- {
- setYDirection(-1);
- }
- }
-
- public void run()
- {
- try {
- while (true)
- {
- move();
- Thread.sleep(2);
- }
- } catch (Exception e)
- {
- System.err.println(e.getMessage());
- }
- }
- }
Ball Class
- public class Ball implements Runnable
- {
-
- int x, y, xDirection, yDirection;
- Rectangle ball;
- Paddle p1 = new Paddle(15, 140, 1);
- Paddle p2 = new Paddle(770, 140, 2);
-
- int p1Score, p2Score
-
- Image BallImage;
- public Ball(int x, int y)
- {
- p1Score = p2Score = 0;
- this.x = x;
- this.y = y;
- Random r = new Random();
- int rDir = r.nextInt(1);
- if (rDir == 0)
- {
- rDir--;
- }
- setXDirection(rDir);
- int yrDir = r.nextInt(1);
- if (yrDir == 0)
- {
- yrDir--;
- }
- setYDirection(yrDir);
- ball = new Rectangle(this.x, this.y, 15, 15);
- }
-
- public void setXDirection(int xdir)
- {
- xDirection = xdir;
- }
-
- public void setYDirection(int ydir)
- {
- yDirection = ydir;
- }
- public void draw(Graphics g)
- {
- java.net.URL urlBall = getClass().getResource("Ball.png");
- BallImage = getImage(urlBall);
- g.drawImage(BallImage, this.ball.x, this.ball.y, null);
- }
-
- public void collision()
- {
-
- if (ball.intersects(p1.paddle))
- {
- setXDirection(+1);
- }
-
- if (ball.intersects(p2.paddle))
- {
- setXDirection(-1);
- }
- }
-
- public void move()
- {
- collision();
- ball.x += xDirection;
- ball.y += yDirection;
-
- if (ball.x <= 0)
- {
- setXDirection(+1);
- p2Score++;
- }
- if (ball.x >= 785)
- {
- setXDirection(-1);
- p1Score++;
- }
- if (ball.y <= 0)
- {
- setYDirection(+1);
- }
- if (ball.y >= 485)
- {
- setYDirection(-1);
- }
- }
-
- public void run()
- {
- try {
- while (true)
- {
- move();
- Thread.sleep(2)
- }
- } catch (Exception e)
- {
- System.err.println(e.getMessage());
- }
- }
- }
Paddle Class
- public class Paddle implements Runnable
- {
-
- int x, y, yDirection, id;
- Rectangle paddle;
- Image PaddleImage1;
- Image PaddleImage2;
- public Paddle(int x, int y, int id)
- {
- this.x = x;
- this.y = y;
- this.id = id;
- paddle = new Rectangle(x, y, 10, 80);
- }
-
- public void keyPressed(KeyEvent e)
- {
- switch (id)
- {
-
- case 1:
- if (e.getKeyCode() == e.VK_W)
- {
- setYDirection(-1);
- }
- if (e.getKeyCode() == e.VK_S)
- {
- setYDirection(1);
- }
- break;
-
- case 2:
- if (e.getKeyCode() == e.VK_UP)
- {
- setYDirection(-1);
- }
- if (e.getKeyCode() == e.VK_DOWN)
- {
- setYDirection(+1);
- }
- break;
- default:
- System.out.println("Enter correct id");
- break;
- }
- }
- public void keyReleased(KeyEvent e)
- {
- switch (id)
- {
-
- case 1:
- if (e.getKeyCode() == e.VK_W)
- {
- setYDirection(0);
- }
- if (e.getKeyCode() == e.VK_S)
- {
- setYDirection(0);
- }
- break;
-
- case 2:
- if (e.getKeyCode() == e.VK_UP)
- {
- setYDirection(0);
- }
- if (e.getKeyCode() == e.VK_DOWN)
- {
- setYDirection(0);
- }
- break;
- default:
- System.out.println("Enter correct id");
- break;
- }
- }
- public void setYDirection(int ydir)
- {
- yDirection = ydir;
- }
- public void move()
- {
- paddle.y += yDirection;
- if (paddle.y <= 30)
- {
- paddle.y = 30;
- }
- if (paddle.y >= 405)
- {
- paddle.y = 405;
- }
- }
- public void draw(Graphics g)
- {
- java.net.URL url1 = getClass().getResource("PaddleImage1.png");
- PaddleImage1 = getImage(url1);
- java.net.URL url2 = getClass().getResource("PaddleImage2.png");
- PaddleImage2 = getImage(url2);
- switch (id)
- {
-
- case 1:
- g.drawImage(PaddleImage1, this.paddle.x, this.paddle.y, null);
- break;
-
- case 2:
- g.drawImage(PaddleImage2, this.paddle.x, this.paddle.y, null);
- break;
- default:
- System.out.println("Enter correct id");
- }
- }
- @Override
-
- public void run()
- {
- try
- {
- while (true)
- {
- move();
- Thread.sleep(4);
- }
- } catch (Exception e)
- {
- System.err.println(e.getMessage());
- }
- }
- }
I have also attached the source code and JAR file with it. So you can download it and run it on your PC.