A modern and highly optimized version of the classic Snake Game built using Vanilla JavaScript, HTML5, and CSS3.
This project focuses on clean UI design, optimized DOM rendering, smooth gameplay mechanics, and modern frontend practices such as CSS variables, dynamic CSS Grid layouts, and browser storage.
Play the game here:
https://st-64.github.io/snake-game/
Control the snake using the arrow keys and eat food to grow longer.
Avoid collisions with:
- Walls
- Your own snake body
The longer the snake grows, the more difficult the game becomes.
The game becomes harder as your score increases.
- Every 50 points โ Level increases
- Snake speed increases automatically
- Level Up notification appears on screen
This creates a progressively challenging gameplay experience.
A built-in live timer tracks how long the player survives in the game.
Timer format:
MM-SS
Example:
02-35
The game uses localStorage to store the highest score.
This means:
- High score persists after page refresh
- High score persists across browser sessions
Key used:
snakeHighScore
Multiple sound effects enhance the gameplay experience.
Included sounds:
- ๐ต Background music (looped)
- ๐ Food eating sound
- ๐ Level up sound
- ๐ Game over sound
- โถ Game start sound
Includes a custom Start Screen to gracefully handle strict browser audio autoplay policies, ensuring music starts seamlessly upon user interaction.
The interface uses a dark themed modern UI.
Design features include:
- CSS custom variables (design system)
- Flexbox layouts
- CSS Grid game board
- Glassmorphism (Frosted Glass) Modals
- Smooth shadows and spacing system
UI components:
- Scoreboard
- Game board
- Interactive Start Screen
- Game Over modal
- Level Up popup
This project uses several techniques to improve performance.
The grid is generated using DocumentFragment so all blocks are inserted into the DOM at once.
This reduces DOM reflows and improves loading performance.
Instead of clearing the entire board each frame:
- Only the previous snake positions are cleared.
This significantly reduces unnecessary DOM updates.
Blocks are stored using an object with coordinate keys:
blocks["row-col"]
Example:
blocks["5-10"]
This allows O(1) lookup when rendering snake segments or food.
The board automatically calculates rows and columns based on the container size:
cols = boardWidth / blockWidth
rows = boardHeight / blockHeight
The board is then rendered using:
grid-template-columns
grid-template-rows
Provides the semantic structure of the game.
Main components:
- Scoreboard
- Game board
- Start screen
- Game Over modal
- Level Up notification
Used for modern UI styling including:
- CSS Variables
- Flexbox
- CSS Grid
- Shadows
- Glassmorphism modals
- Responsive spacing system
Handles the entire game engine including:
- Game loop
- Snake movement
- Collision detection
- Food generation
- Score system
- Level progression
- Timer system
- Audio handling
- LocalStorage persistence
๐ฆ snake-game
โฃ ๐ Sounds
โ โฃ ๐ต bgm.mp3
โ โฃ ๐ต dead.mp3
โ โฃ ๐ต eat.mp3
โ โฃ ๐ต levelup.mp3
โ โ ๐ต start.mp3
โฃ ๐ index.html
โฃ ๐ style.css
โ ๐ script.js
| Key | Action |
|---|---|
| โ Arrow | Move Up |
| โ Arrow | Move Down |
| โ Arrow | Move Left |
| โ Arrow | Move Right |
| Enter | Start / Restart Game |
This is a pure Vanilla Web Project, so no installation or build tools are required.
git clone https://github.com/st-64/snake-game.git
cd snake-game
Open the file:
index.html
in your browser.
For development, you can use:
VS Code โ Live Server Extension
This project demonstrates several important frontend development concepts:
- Game loop architecture
- Efficient DOM rendering
- Event handling
- LocalStorage usage
- CSS Grid based layouts
- Modular game state management
- Performance optimization techniques
Possible improvements for this project:
- Mobile touch controls
- Multiplayer mode
- Global leaderboard
- Obstacles or walls
- Power-up items
- Multiple map themes
If you like this project:
- โญ Star the repository
- ๐ด Fork the project
- ๐ง Try adding new features
st-64
GitHub
https://github.com/st-64
Built with โค๏ธ using HTML, CSS, and JavaScript