A modern implementation of the classic Snake game built with React and Vite. Control a snake as it moves around the board, eating food to grow longer while avoiding collisions with walls and itself.
- Responsive game board: 15x15 grid with smooth gameplay
- Classic snake mechanics: Move in four directions using arrow keys
- Food consumption: Grow your snake with each food item eaten
- Score tracking: Keep track of your current score
- Special food items: Purple food that reverses your snake's direction
- Game reset: Automatic restart on game over
- React - UI library
- Vite - Build tool and development server
- Custom CSS for styling
- JavaScript ES6+ features
- Custom LinkedList implementation for snake movement
- Node.js (v14 or higher)
- npm or yarn
-
Clone the repository:
git clone https://github.com/yourusername/snake-game.git cd snake-game -
Install dependencies:
npm install # or yarn install -
Start the development server:
npm run dev # or yarn dev -
Open your browser and navigate to
http://localhost:5173
- Start: The game begins automatically when the page loads
- Controls: Use arrow keys to change direction
- ↑ (Up Arrow): Move Up
- → (Right Arrow): Move Right
- ↓ (Down Arrow): Move Down
- ← (Left Arrow): Move Left
- Objective: Eat the red food items to grow your snake and increase your score
- Special Items: Purple food will reverse your snake's direction when eaten
- Game Over: Occurs when your snake hits a wall or itself. The game automatically restarts.
The game uses a linked list data structure to represent the snake, which allows for efficient movement and growth operations:
- The snake moves by adding a new node at the head and removing the tail
- When food is consumed, the snake grows by adding a node without removing the tail
- The special purple food triggers a direction reversal, implemented by reversing the linked list
- Regular food (red) appears randomly on the board
- Special direction-reversing food (purple) has a 30% chance of appearing instead of regular food
- Food never appears on cells already occupied by the snake
snake-game/
├── public/
├── src/
│ ├── Board/
│ │ ├── Board.jsx # Main game component with game logic
│ │ └── Board.css # Styling for the game board
│ ├── lib/
│ │ └── utils.js # Utility functions (intervals, randomization, etc.)
│ ├── App.jsx # Root component
│ ├── App.css # Global styles
│ ├── index.css # Base styles
│ └── main.jsx # Entry point
├── index.html # HTML template
├── package.json # Project dependencies and scripts
└── vite.config.js # Vite configuration
- Add a proper start/restart game UI
- Implement difficulty levels (speed options)
- Add high score tracking with local storage
- Add mobile support with touch controls
- Add sound effects and music
- Create different themes/skins for the snake and board
Contributions are welcome! If you'd like to contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by [Gaurav Chaudhary ]