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
Score0
High Score0
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.
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!
Key Features
Pause Functionality: Use the 'P' key to pause and resume the game at any time.
Score Tracking: Your current score is displayed during the game, motivating you to beat your own high scores.
Snake Color Selection: Choose your snake's color before starting, adding a layer of personalization.
Restart Button: If the game ends, you can restart easily.
Dynamic Game Over Screen: When the game ends, a "Game Over" screen appears with options to restart or quit.
Game Over Screen
When the game ends, you're presented with a Game Over screen displaying your score and offering a restart button.
Technical Details
GamePanel.java
This file contains the core logic of the game:
Game State Management: Handles transitions between Welcome, Playing, and Game Over states.
Collision Detection: Detects collisions with walls, food, and the snake itself.
Snake Movement: Implements the logic for the snake's direction and position updates.
Food Spawning: Randomly places food on the grid, ensuring it doesn't spawn on the snake.
Sound Effects: Plays audio cues for key game events like eating food or game over.
privatevoidcheckCollision() {
for (int i = snakeLength; i > 0; i--) {
// Check if head collides with bodyif (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 wallsif (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 overif (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.
publicclassMain {
publicstaticvoidmain(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
Open the game and choose the snake color on the welcome screen.
Click "Start Game" to begin.
Use the arrow keys to guide the snake and eat food to score points.
Avoid running into walls or the snake's body.
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:
Designing the game mechanics and UI
Implementing the snake movement and controls
Adding food generation and collision detection
Creating game states (welcome, playing, game over)
Implementing high score tracking
Adding audio feedback for enhanced user experience