Snake Game

Snake Game Screenshot

This Snake Game is a classic arcade-style game implemented in Java using the Swing library. Players control a snake that moves around the screen, eating food to grow longer while avoiding collisions with walls and itself. The game features multiple states including a customizable welcome screen, active gameplay, and a game over screen with high score tracking.

Project Overview

This Snake Game is a classic arcade-style game implemented in Java using the Swing library. Players control a snake that moves around the screen, eating food to grow longer while avoiding collisions with walls and itself. The game features multiple states including a customizable welcome screen, active gameplay, and a game over screen with high score tracking.

Interactive Demo

Try out the Snake Game below. Use the arrow keys to control the snake's direction, eat the red food, and avoid hitting the walls or yourself!

Welcome to Snake Game

Select snake color:

Game Over!
Score: 0
High Score: 0
Score 0
High Score 0

Features

Customizable Snake

Choose your snake's color before starting the game for a personalized gaming experience.

Pause Functionality

Pause the game at any time using the 'P' key or pause button for convenient gameplay.

High Score Tracking

The game saves your highest score, challenging you to beat your own record.

Sound Effects

Engaging audio feedback when eating food or when the game ends for an immersive experience.

Intuitive Controls

Easy to use arrow key controls with responsive movement for smooth gameplay.

Multiple Game States

Features welcome screen, active gameplay, and game over states for a complete arcade experience.

Welcome Screen

The game starts with a colorful welcome screen where you can select the snake's color and initiate the game. It provides a friendly, interactive start to the game.

Welcome Screen Welcome Screen with Colour Picker

Gameplay

Once the game starts, you control the snake using the arrow keys. The snake eats red food blocks to grow longer and increase your score. Be careful to avoid collisions with the edges of the screen or the snake's own body!

Gameplay Screen with High Score Display

Key Features

Game Over Screen

When the game ends, you're presented with a Game Over screen displaying your score and offering a restart button.

Game Over Screen

Technical Details

GamePanel.java

This file contains the core logic of the game:

private void checkCollision() { for (int i = snakeLength; i > 0; i--) { // Check if head collides with body if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) { gameState = GameState.GAME_OVER; // Update game state to GAME_OVER running = false; } } // Check if head collides with walls if (snakeX[0] < 0 || snakeX[0] >= WIDTH || snakeY[0] < 0 || snakeY[0] >= HEIGHT) { gameState = GameState.GAME_OVER; // Update game state to GAME_OVER running = false; } if (!running) { playSound("/Sounds/gameover.wav"); // Play sound on game over if (snakeLength - 1 > highScore) { highScore = snakeLength - 1; // Update high score } timer.stop(); // Stop the timer if the game is over } }

Main.java

This file sets up the game window and integrates the GamePanel into a JFrame for user interaction. It handles the game's overall appearance and window properties.

public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Snake Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); GamePanel gamePanel = new GamePanel(); frame.add(gamePanel); frame.pack(); frame.setLocationRelativeTo(null); // Center the window frame.setVisible(true); } }

How to Play

  1. Open the game and choose the snake color on the welcome screen.
  2. Click "Start Game" to begin.
  3. Use the arrow keys to guide the snake and eat food to score points.
  4. Avoid running into walls or the snake's body.
  5. If the game ends, restart from the Game Over screen or quit.

Development Process

This project was developed using Java and the Swing library for creating the graphical interface. The development process included:

Back to Projects