skip to content

Pong Game Java

[completed]Javagithub ↗

Project Overview

PongGameFR is a classic Pong implementation written entirely in Java using Swing and AWT for graphics rendering. The project recreates the iconic two-player paddle-and-ball gameplay with a clean architecture separating game entities, rendering logic, and user input handling.

This was my freshman year final project, inspired by Bro Code's tutorials. It has a menu system, a controls screen, and dynamic gameplay features including increasing ball speed on paddle collisions.

Technology Stack

| Component | Technology | | ---------- | ---------- | | Language | Java | | UI | Swing + AWT |

No build tooling beyond javac — the whole game compiles and runs with the JDK.

Architecture

The codebase has four layers:

  • Game state machine. Three states: MENU, CONTROLS, PLAY. Each renders differently and listens to different inputs.
  • Entities. Paddle, Ball, with position + velocity vectors and rectangular collision bounds.
  • Renderer. A single GamePanel extends JPanel overrides paintComponent to draw the current state. Double-buffered automatically by Swing.
  • Input. KeyListener attached to the JFrame; player 1 uses W/S, player 2 uses Up/Down.

Gameplay Loop

The game runs at a fixed-step ~60 FPS. Each tick:

  1. Read input state (held keys).
  2. Update paddle positions (clamped to screen bounds).
  3. Update ball position.
  4. Check collisions — wall (top/bottom), paddle (left/right), score (off either side).
  5. On paddle collision, the ball's velocity multiplies by ~1.05x — the rally gets faster the longer you can keep it going.
  6. Repaint.

What I Learned

  • Java Swing is dated but it works. Double-buffered paintComponent is enough for a 60 FPS game on commodity hardware.
  • A finite state machine for menus + gameplay is the right structure even at this scale — trying to do it with flags gets messy fast.
  • The ball-speed-up-on-collision mechanic makes Pong genuinely tense at high rally counts. It's the only thing the game needs beyond the basic loop.
  • Don't put game logic inside paintComponent. Swing calls it on its own schedule; you'll have non-deterministic physics. Use a javax.swing.Timer (or in modern Java, a ScheduledExecutorService) for the game loop.