Snake Game

The Snake Game is a classic arcade game where the player controls a snake to consume fruits that appear randomly on the screen. As the snake eats the fruits, it grows longer, and the game becomes progressively more challenging. The game ends if the snake collides with the walls or itself. This project is implemented using Java and JavaFX to provide a simple yet engaging gaming experience.

Features

  • Grid-based Movement: The snake moves within a defined grid layout.
  • Fruit Consumption: A randomly placed fruit appears on the grid, and the snake grows longer as it consumes the fruit.
  • Collision Detection: The game detects collisions with the grid boundaries and the snake's own body.
  • Score Tracking: The player's score increases as the snake consumes fruits.
  • Speed Variation: The snake's speed increases as the score reaches multiples of five, adding to the challenge.
  • Game Over Screen: Displays a "GAME OVER" message when the game ends.
  • Keyboard Controls: Use the W, A, S, D keys to control the snake's direction.

How to Play

  1. Start the Game:

    Run the Main.java file in your IDE or from the command line. A game window will open, displaying the game grid with a green snake and a red fruit.

  2. Control the Snake:

    Use the following keys to control the snake:

    • W: Move up
    • A: Move left
    • S: Move down
    • D: Move right
  3. Objective:

    Navigate the snake to consume the red fruit. Avoid collisions with the grid boundaries and the snake's own body. Increase your score by eating more fruits. The game becomes more challenging as the snake's speed increases every five points.

  4. Game Over:

    The game ends if the snake collides with the grid boundaries or itself. "GAME OVER" will be displayed on the screen. To play again, restart the application.

Project Structure

  • Main.java: The main class that initializes the game, handles the game loop, and manages user input and rendering.
  • Point.java: A utility class representing the coordinates of points on the grid, used for the snake's body segments and the fruit's position.

Installation and Setup

  1. Requirements:
    • Java Development Kit (JDK) 8 or later
    • JavaFX SDK (included in JDK 11 or later)
    • An IDE like IntelliJ IDEA, Eclipse, or NetBeans, or a text editor like Visual Studio Code with Java support
  2. Setup:
    • Clone the repository or download the source code.
    • Open the project in your preferred IDE.
    • Ensure that JavaFX is properly set up in your project.
    • Run Main.java to start the game.

Gameplay Logic

  1. Initialization:

    The game starts with a snake of length one, positioned at the center of the grid. A fruit is placed randomly on the grid.

  2. Game Loop:

    The game continuously runs a loop where it:

    • Clears the canvas and redraws the grid, snake, and fruit.
    • Updates the snake's position based on the user's input.
    • Checks for collisions with the boundaries and the snake's body.
    • Checks if the snake has consumed the fruit and updates the score and snake's length accordingly.
  3. Collision Handling:

    If the snake's head moves outside the grid or collides with any part of its body, the game sets the gameOver flag to true and displays the "GAME OVER" message.

  4. Scoring and Levels:

    The score increases by one for each fruit consumed. The snake's speed increases every five points, adding difficulty.

Future Enhancements

  • Pause/Resume Functionality: Add the ability to pause and resume the game.
  • High Score Tracking: Implement a feature to track and display the highest score.
  • Multiple Levels: Introduce distinct levels with different grid sizes and obstacles.
  • Mobile Compatibility: Adapt the game for mobile devices with touch controls.

Conclusion

This Snake Game project provides a foundational understanding of game development using Java and JavaFX. It offers a fun and nostalgic gaming experience while demonstrating essential programming concepts such as user input handling, game loops, collision detection, and dynamic rendering.

Author

Developed by Aditya Gupta(adityacse207@gmail.com).