Introduction
Pong is one of the earliest arcade games and a popular starting project for game developers. In this tutorial, we will walk through how to create a simple Pong game using the Java programming language. The objective is to learn the basic principles of game programming, such as handling user input, drawing graphics, and detecting collisions.
Objective
Our goal is to create a two-player Pong game in Java where each player controls a paddle, and the game has a ball bouncing between the paddles. The player who fails to hit the ball with their paddle loses a point, and the first to reach a predefined score wins the game.
Java Code for Pong Game
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PongGame extends JPanel implements ActionListener, KeyListener {
private int ballX = 300, ballY = 200, ballDX = 2, ballDY = 2, ballSize = 15;
private int player1Y = 150, player2Y = 150, paddleWidth = 10, paddleHeight = 60;
private Timer timer;
private int player1Score = 0, player2Score = 0;
private final int WIDTH = 600, HEIGHT = 400;
public PongGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
addKeyListener(this);
setFocusable(true);
timer = new Timer(10, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(player1Y, 20, paddleWidth, paddleHeight); // Player 1 paddle
g.fillRect(player2Y, HEIGHT – paddleHeight – 20, paddleWidth, paddleHeight); // Player 2 paddle
g.fillOval(ballX, ballY, ballSize, ballSize); // Ball
g.setFont(new Font(“Arial”, Font.PLAIN, 30));
g.drawString(“Player 1: ” + player1Score, 20, 30);
g.drawString(“Player 2: ” + player2Score, WIDTH – 150, 30);
}
@Override
public void actionPerformed(ActionEvent e) {
ballX += ballDX;
ballY += ballDY;
if (ballY <= 0 || ballY >= HEIGHT – ballSize) {
ballDY = -ballDY; // Ball hits top or bottom
}
if (ballX <= player1Y + paddleWidth && ballY >= 20 && ballY <= 20 + paddleHeight) { ballDX = -ballDX; // Ball hits player 1 paddle } if (ballX >= player2Y – ballSize && ballY >= HEIGHT – paddleHeight – 20 && ballY <= HEIGHT – 20) {
ballDX = -ballDX; // Ball hits player 2 paddle
}
if (ballX <= 0) { player2Score++; // Player 2 scores resetBall(); } if (ballX >= WIDTH – ballSize) {
player1Score++; // Player 1 scores
resetBall();
}
repaint();
}
private void resetBall() {
ballX = WIDTH / 2 – ballSize / 2;
ballY = HEIGHT / 2 – ballSize / 2;
ballDX = -ballDX;
ballDY = 2;
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_W && player1Y > 0) {
player1Y -= 15; // Move player 1 paddle up
}
if (keyCode == KeyEvent.VK_S && player1Y < HEIGHT – paddleHeight) { player1Y += 15; // Move player 1 paddle down } if (keyCode == KeyEvent.VK_UP && player2Y > 0) {
player2Y -= 15; // Move player 2 paddle up
}
if (keyCode == KeyEvent.VK_DOWN && player2Y < HEIGHT – paddleHeight) {
player2Y += 15; // Move player 2 paddle down
}
}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame(“Pong Game”);
PongGame pongGame = new PongGame();
frame.add(pongGame);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation of Program Structure
The program consists of a PongGame class that extends JPanel and implements ActionListener and KeyListener interfaces:
- ballX and ballY store the position of the ball.
- player1Y and player2Y track the position of the paddles.
- paintComponent method renders the ball, paddles, and score.
- actionPerformed method updates the ball position and checks for collisions with paddles and screen boundaries.
- keyPressed method handles player input to move paddles.
- The game runs inside a Timer to update the game state and refresh the display every 10 milliseconds.
How to Run the Program
Follow these steps to run the Pong game:
- Install Java Development Kit (JDK) if you haven’t already.
- Save the code in a file named PongGame.java.
- Open the command line (or terminal) and navigate to the directory where the file is saved.
- Compile the code using the command:
javac PongGame.java
- Run the program with:
java PongGame
- The game window will open, and you can play using the W/S keys for Player 1 and the UP/DOWN arrow keys for Player 2.