Skip to content

kenjutzue/Snake-Game-using-Java-Swing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game

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.


Table of Contents


Introduction

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.


Gameplay

  • 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.

Features

  • 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.

Installation and Running

Prerequisites

  • Java Development Kit (JDK) 8 or higher installed on your system.
  • A terminal or command prompt to compile and run the game.

Steps

  1. Save the source code as SnakeGame.java.

  2. Open a terminal and navigate to the directory containing the file.

  3. Compile the source code:

    javac SnakeGame.java
  4. Run the compiled program:

    java SnakeGame

The game window will open, displaying the start screen. Use the buttons or keyboard to begin playing.


Controls

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

High Score Persistence

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.

Design and Implementation

  • Java Swing GUI: The game interface is built using Swing components, including JPanel, JButton, and JFrame.
  • Game Loop: A javax.swing.Timer drives the game loop, updating the snake’s position and checking game state at regular intervals.
  • Input Handling: Keyboard input is managed via a KeyAdapter to 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.

About the Developer

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.


Future Enhancements

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.

License

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages