Skip to content

st-64/snake-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Snake Game Pro

HTML5 CSS3 JavaScript

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.


๐ŸŽฎ Live Demo

Play the game here:

https://st-64.github.io/snake-game/


๐ŸŒŸ Features

๐Ÿ Classic Snake Gameplay

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.


โšก Dynamic Level System

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.


โฑ๏ธ Game Timer

A built-in live timer tracks how long the player survives in the game.

Timer format:

MM-SS

Example:

02-35

๐Ÿ’พ Persistent High Score

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

๐Ÿ”Š Immersive Audio System

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.


๐ŸŽจ Modern UI Design

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

โšก Performance Optimizations

This project uses several techniques to improve performance.

DocumentFragment Rendering

The grid is generated using DocumentFragment so all blocks are inserted into the DOM at once.

This reduces DOM reflows and improves loading performance.


Targeted Board Clearing

Instead of clearing the entire board each frame:

  • Only the previous snake positions are cleared.

This significantly reduces unnecessary DOM updates.


Object-based Grid Lookup

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.


Dynamic CSS Grid

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

๐Ÿ› ๏ธ Technologies Used

HTML5

Provides the semantic structure of the game.

Main components:

  • Scoreboard
  • Game board
  • Start screen
  • Game Over modal
  • Level Up notification

CSS3

Used for modern UI styling including:

  • CSS Variables
  • Flexbox
  • CSS Grid
  • Shadows
  • Glassmorphism modals
  • Responsive spacing system

JavaScript (ES6)

Handles the entire game engine including:

  • Game loop
  • Snake movement
  • Collision detection
  • Food generation
  • Score system
  • Level progression
  • Timer system
  • Audio handling
  • LocalStorage persistence

๐Ÿ“‚ Project Structure

๐Ÿ“ฆ snake-game
โ”ฃ ๐Ÿ“‚ Sounds
โ”ƒ โ”ฃ ๐ŸŽต bgm.mp3
โ”ƒ โ”ฃ ๐ŸŽต dead.mp3
โ”ƒ โ”ฃ ๐ŸŽต eat.mp3
โ”ƒ โ”ฃ ๐ŸŽต levelup.mp3
โ”ƒ โ”— ๐ŸŽต start.mp3
โ”ฃ ๐Ÿ“œ index.html
โ”ฃ ๐Ÿ“œ style.css
โ”— ๐Ÿ“œ script.js

๐ŸŽฎ Game Controls

Key Action
โ†‘ Arrow Move Up
โ†“ Arrow Move Down
โ† Arrow Move Left
โ†’ Arrow Move Right
Enter Start / Restart Game

โš™๏ธ How to Run Locally

This is a pure Vanilla Web Project, so no installation or build tools are required.

1๏ธโƒฃ Clone the repository

git clone https://github.com/st-64/snake-game.git

2๏ธโƒฃ Open the project folder

cd snake-game

3๏ธโƒฃ Run the game

Open the file:

index.html

in your browser.

For development, you can use:

VS Code โ†’ Live Server Extension


๐Ÿ“š What This Project Demonstrates

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

๐Ÿš€ Future Improvements

Possible improvements for this project:

  • Mobile touch controls
  • Multiplayer mode
  • Global leaderboard
  • Obstacles or walls
  • Power-up items
  • Multiple map themes

โญ Support

If you like this project:

  • โญ Star the repository
  • ๐Ÿด Fork the project
  • ๐Ÿง  Try adding new features

๐Ÿ‘จโ€๐Ÿ’ป Author

st-64

GitHub
https://github.com/st-64


Built with โค๏ธ using HTML, CSS, and JavaScript

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors