Back to Portfolio

Classic Snake Game (Java Edition)

Java / Desktop GUI2024

A strategic reimplementation of the classic Snake game using Java 21 and Swing, featuring timed poisonous and special apples, custom audio, and a modular architecture.

Classic Snake Game (Java Edition)

The Problem

Building a responsive and thread-safe game loop in a standard Java desktop environment without external game engines is a challenge. Additionally, standard retro games often lack strategic depth beyond basic movement.

Problem illustration

The Solution

Developed using Java 21 and Swing, this version introduces unique mechanics like 'Poisonous Apples' (reducing length/score) and 'Special Apples' (ending the game) on independent 7s and 10s timers. The architecture separates input handling, game logic, and audio rendering for a smooth user experience.

Input Management

Implemented a custom Controller using KeyListener to manage snake direction while preventing illegal 180-degree turns.

Game Loop & Logic

Utilized Swing Timers for a consistent 140ms game tick, handling real-time collision detection with walls, self-body, and timed items.

Audio Engine

Centralized audio management using javax.sound.sampled for looping background music and synchronized sound effects for game events.

Architecture & Features

A modular single-package structure using JFrame as the main container with specialized panels for the menu and gameplay.

  • View: StartPanel (Menu) & Game (Board Rendering)
  • Logic: Controller (Input handling) & Features (State/Score management)
  • Audio: MusicPlayer (Centralized WAV playback)
  • Entry Point: SnakePlayer (JFrame and system setup)

Technologies Used

Java 21Java SwingJava AWTjavax.sound.sampledIntelliJ IDEAWAV Audio Processing
Classic Snake Game (Java Edition) | Abdulrahman Hamdi