A classic Snake Game built using HTML, CSS, and Vanilla JavaScript, implemented with a DOM-based grid system. The project focuses on clean game logic, state handling, and multi-device controls (keyboard + mobile swipe).
This project was built to strengthen core JavaScript concepts such as DOM manipulation, event handling, timers, collision detection, and basic game architecture.
- 🎮 Classic snake gameplay
- ⌨️ Keyboard controls (Arrow keys)
- 📱 Mobile-friendly swipe / drag controls
- 🧱 Wall collision detection with Game Over modal
- 🐍 Self-collision detection (snake dies on body hit)
- 🔁 Direction reversal prevention (no instant opposite moves)
- 🏆 High Score persistence using
localStorage - ⏱️ Live game timer
- ♻️ Restart game without page reload
- HTML5 – Structure
- CSS3 – Grid layout, UI styling, modals
- JavaScript (ES6) – Game logic, state management, events
No external libraries or frameworks used.
- ⬆️ Arrow Up – Move Up
- ⬇️ Arrow Down – Move Down
- ⬅️ Arrow Left – Move Left
- ➡️ Arrow Right – Move Right
- 👉 Swipe Right – Move Right
- 👈 Swipe Left – Move Left
- 👆 Swipe Up – Move Up
- 👇 Swipe Down – Move Down
Reverse direction (e.g., Up → Down) is intentionally blocked to follow classic snake rules.
-
The board is rendered as a dynamic grid using CSS Grid.
-
Snake position is stored as an array of coordinates.
-
On each game tick:
- Next head position is calculated
- Wall and self-collision are checked
- Snake moves forward
- Food consumption increases score and length
- DOM is updated to reflect the new state
├── index.html
├── style.css
├── script.js
└── README.md
- Clone or download the repository
- Open
index.htmlin any modern browser - Click Start Game and begin playing
No build steps required.
- DOM-based game rendering
- Handling keyboard and touch events together
- Managing game state with JavaScript
- Collision detection logic
- Using
setIntervalsafely - Writing scalable, readable game code
- Dynamic speed increase with score
- Pause / Resume functionality
- Level system
- Canvas-based rendering
- AI-controlled snake
Avishek Dutta B.Tech CSE | JavaScript & Web Development Enthusiast
If you found this project useful or insightful, feel free to explore, fork, or build upon it.