A classic Snake game, it is based on 1998 mobile video game created by Taneli Armanto as one of the three games included in the Nokia 6110 cellular phone. this game developed by Kent R. Grefiel using Java Swing.
- Introduction
- Gameplay
- Features
- Installation and Running
- Controls
- High Score Persistence
- Design and Implementation
- About the Developer
- Future Enhancements
- License
The Snake game is a timeless arcade classic where the player maneuvers a growing snake around a confined board, aiming to eat food items to increase its length while avoiding collisions with itself or the boundaries. This version, developed by Kent R. Grefiel, brings the nostalgic gameplay to modern Java desktop environments using Swing for the graphical interface.
- The snake starts with a small length and moves continuously in one of four directions.
- The player controls the snake’s direction using the arrow keys.
- Food appears randomly on the board; eating food increases the snake’s length and the player’s score.
- The game ends if the snake collides with itself or the edges of the board.
- The player can pause and resume the game at any time.
- After a game over, the player can restart by pressing the Enter key or clicking the New Game button.
- Classic Snake Mechanics: Smooth and responsive controls with traditional snake movement.
- Dark Mode Theme: A visually appealing dark background with twinkling stars to enhance the gaming experience.
- Pause and Resume: Easily pause the game with a button or keyboard shortcut and resume when ready.
- High Score Tracking: The game saves the highest score locally using Java Preferences API, allowing players to challenge their best performance.
- Start Screen with Story: Before starting, players see a welcome screen with instructions and a brief story about the developer.
- Keyboard and Button Controls: Multiple ways to control the game for convenience.
- Responsive UI: Buttons and game area are neatly arranged and styled for clarity and ease of use.
- Java Development Kit (JDK) 8 or higher installed on your system.
- A terminal or command prompt to compile and run the game.
-
Save the source code as
SnakeGame.java. -
Open a terminal and navigate to the directory containing the file.
-
Compile the source code:
javac SnakeGame.java
-
Run the compiled program:
java SnakeGame
The game window will open, displaying the start screen. Use the buttons or keyboard to begin playing.
| Action | Control |
|---|---|
| Move Up | Up Arrow Key |
| Move Down | Down Arrow Key |
| Move Left | Left Arrow Key |
| Move Right | Right Arrow Key |
| Pause / Resume | P key or Spacebar |
| Start New Game | Click "New Game" button or press Enter after game over |
| Pause / Resume Game | Click "Continue" button |
| View High Score | Click "High Score" button |
The game uses Java's Preferences API to store the highest score locally on the user's machine. This means:
- Your best score is saved automatically when you achieve a new high.
- The high score persists even after closing and reopening the game.
- You can view the current high score anytime by clicking the "High Score" button.
- Java Swing GUI: The game interface is built using Swing components, including
JPanel,JButton, andJFrame. - Game Loop: A
javax.swing.Timerdrives the game loop, updating the snake’s position and checking game state at regular intervals. - Input Handling: Keyboard input is managed via a
KeyAdapterto control snake direction and game state. - Graphics: The snake, food, and background stars are drawn using Java 2D graphics primitives.
- Game State Management: Boolean flags track whether the game is running, paused, or over, controlling the game flow.
- Buttons: Interactive buttons allow starting a new game, pausing/resuming, and viewing the high score.
- Dark Mode: The entire UI and game board use a dark color scheme with white and green accents for visibility and style.
Kent R. Grefiel is a passionate full stack developer but he was interest to make in any classic games and Java programming. This Snake game project was created as a fun way to practice GUI programming and game logic in Java, while paying homage to one of the most beloved arcade games of all time.
Potential improvements and features that could be added include:
- Sound Effects and Music: Adding audio feedback for eating food, collisions, and background music.
- Multiple Difficulty Levels: Adjusting snake speed or board size for different skill levels.
This project is open source and free to use for educational and personal purposes. Feel free to modify and distribute it as you like.
Enjoy playing the Snake game and challenge yourself to beat your high