🐍 Two Player Snake Game – Case Study
📌 Project Overview
Name: Two Player Snake Game (Inspired by Snake.io) Technologies: Java (Core, AWT/Swing), MySQL, Griddle Developer: Haris Khan (GitHub: developebyharis/snake-game)
This project is a real-time, competitive snake game for two players. Unlike traditional implementations, this game integrates with a MySQL database (via Griddle) to store and retrieve persistent data such as scores, matches, and player history.
🎮 Key Features
-
Two-player mode: Player 1 (W/A/S/D) vs Player 2 (arrow keys).
-
Database-driven scoreboard:
- Stores high scores, win/loss records, and match history in MySQL.
- Displays leaderboard dynamically using Griddle integration.
-
Collision detection for snake-to-snake, walls, and self-collision.
-
Intelligent food placement: Prevents spawning inside snakes or outside the grid.
-
Persistent session tracking: Results remain saved even after closing the game.
⚙️ Technical Architecture
1. Frontend (Java AWT & Swing)
-
Handles game rendering, animations, and input capture.
-
Uses a game loop for smooth real-time movement.
-
UI displays:
- Game board
- Current scores
- Live leaderboard (synced with DB)
2. Backend (MySQL + Griddle)
-
MySQL database schema stores:
players (id, name, wins, losses, high_score)matches (id, player1, player2, winner, date_played)
-
Griddle is used as the ORM/data access layer to connect Java with MySQL.
-
CRUD operations:
- Save new scores after each match.
- Fetch leaderboard for display.
- Update player statistics after each round.
3. Game Logic
-
Snakes move in sync with player input.
-
Food spawns at random positions not occupied by snakes.
-
On collision:
- Winner is determined.
- Game state is saved to MySQL.
- Leaderboard is refreshed in real-time.
🚀 Setup & Execution
-
Clone the repository:
git clone https://github.com/developebyharis/snake-game.git -
The schema is build on MySql CLI.
-
Configure DB credentials in
/build.gradle. -
Compile & run the project:
javac SnakeGame.java java SnakeGame -
Play and watch results sync with the MySQL-powered leaderboard.
💡 Design Decisions & Insights
| Decision | Reason |
|---|---|
| Java AWT & Swing | Lightweight, portable GUI framework ideal for quick rendering. |
| MySQL + Griddle | Adds persistence and realism by tracking game history & leaderboard. |
| Two-player mode | Makes the game competitive and engaging. |
🌟 Project Impact
- Strengthened Java GUI & OOP knowledge.
- Hands-on with MySQL integration in a game setting.
- Learned to design database schemas for persistence.
- Showcases full-stack thinking: frontend (UI/game) + backend (DB).