Pong Game in Java

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

Introduction

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 controled.
  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 

Main Class
 

public class Ball implements Runnable

{

    //Global variable

    int x,y,xDirection,yDirection;

    Rectangle ball;

    Paddle p1=new Paddle(15,140,1);//paddle class objects

    Paddle p2=new Paddle(770,140,2);

    //Score

    int p1Score,p2Score;

    //Ball Image

     Image BallImage;

 

public Ball(int x,int y)

{

    p1Score=p2Score=0;

    this.x=x;

    this.y=y;

    Random r=new Random();//creats Random numbers

    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);

}

//for set the xDirection of the ball

public void setXDirection(int xdir)

{

    xDirection=xdir;

}

//for set the yDirection of the ball

public void setYDirection(int ydir)

{

    yDirection=ydir;

}

public void draw(Graphics g)

{

    java.net.URL urlBall = getClass().getResource("Ball.png"); //get ball image from the project folder

    BallImage = getImage(urlBall);

    g.drawImage(BallImage, this.ball.x, this.ball.y,null);

}

//Collision method

public void collision()

{ //for paddle 1 collision

    if(ball.intersects(p1.paddle))

    {

        setXDirection(+1);

    }

    //for paddle 2 collision

    if(ball.intersects(p2.paddle))

    {

        setXDirection(-1);

    }

}

//Move method

public void move()

{

    collision();

    ball.x+=xDirection;

    ball.y+=yDirection;

    //Bounce the ball when edge is detected

    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);

    }

}

 

//Run method Thread Start from here

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

{

    //Global variable

    int x,y,xDirection,yDirection;

    Rectangle ball;

    Paddle p1=new Paddle(15,140,1);//paddle class objects

    Paddle p2=new Paddle(770,140,2);

    //Score

    int p1Score,p2Score;

    //Ball Image

    Image BallImage;

 

public Ball(int x,int y)

{

    p1Score=p2Score=0;

    this.x=x;

    this.y=y;

    Random r=new Random();//creats Random numbers

    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);

}

//for set the xDirection of the ball

public void setXDirection(int xdir)

{

    xDirection=xdir;

}

//for set the yDirection of the ball

public void setYDirection(int ydir)

{

    yDirection=ydir;

}

public void draw(Graphics g)

{

    java.net.URL urlBall = getClass().getResource("Ball.png"); //get ball image from the project folder

    BallImage = getImage(urlBall);

    g.drawImage(BallImage, this.ball.x, this.ball.y,null);

}

//Collision method

public void collision()

{

    //for paddle 1 collision

    if(ball.intersects(p1.paddle))

    {

        setXDirection(+1);

    }

    //for paddle 2 collision

    if(ball.intersects(p2.paddle))

    {

        setXDirection(-1);

    }

}

//Move method

public void move()

{

    collision();

    ball.x+=xDirection;

    ball.y+=yDirection;

    //Bounce the ball when edge is detected

    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);

    }

}

 

//Run method Thread Start from here

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

{

    //Global Variables

    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);

}

//Keypressed event function

public void keyPressed(KeyEvent e)

{

    switch(id)

    {

        //for Paddle 1

        case 1:

        if(e.getKeyCode()== e.VK_W)

        {

           setYDirection(-1);

        }

        if(e.getKeyCode()== e.VK_S)

        {

             setYDirection(1);

        }

        break;

        //for Paddle 2

        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)

    {

        //for Paddle 1

        case 1:

        if(e.getKeyCode()== e.VK_W)

        {

            setYDirection(0);

        }

        if(e.getKeyCode()== e.VK_S)

        {

            setYDirection(0);

        }

        break;

        //for Paddle 2

        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"); //get image

    PaddleImage1 = getImage(url1);

    java.net.URL url2 = getClass().getResource("PaddleImage2.png");

    PaddleImage2 = getImage(url2);

    switch(id)

    {

        //for Paddle 1

        case 1:

        g.drawImage(PaddleImage1, this.paddle.x, this.paddle.y, null);

        break;

        //for Paddle 2

        case 2:

        g.drawImage(PaddleImage2, this.paddle.x, this.paddle.y, null);

        break;

        default:

        System.out.println("Enter correct id");

    }

}

@Override

//Thread Start from here

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.

Up Next
    Ebook Download
    View all
    Learn
    View all