Skip to content

PiyushRepos/snake-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Snake Game

Snake Game Banner

A classic Snake game built with vanilla JavaScript

Play Now โ€ข Features โ€ข Controls


๐Ÿ“– About

A fast, responsive Snake game implementation using pure vanilla JavaScript, HTML5, and CSS3. No frameworks, no dependencies - just classic arcade gaming fun in your browser! Challenge yourself to beat your high score while the snake grows longer with each apple eaten.

โœจ Features

  • ๐ŸŽฎ Classic Gameplay - Smooth snake movement with collision detection
  • ๐Ÿ† High Score Tracking - Persistent high scores saved in local storage
  • โฑ๏ธ Timer - Track how long you've been playing
  • ๐ŸŒ“ Theme Switcher - Toggle between light and dark modes
  • ๐ŸŽš๏ธ Speed Control - Dynamically adjust game speed during gameplay
  • ๐Ÿ“ฑ Responsive Design - Works on desktop and mobile devices
  • โšก Vanilla JavaScript - No frameworks or external dependencies
  • ๐Ÿ’พ Local Storage - Saves high score and theme preferences
  • ๐ŸŽฏ Wrapping Borders - Snake wraps around screen edges
  • ๐Ÿ‘€ Visual Snake Head - Distinct head styling for better visibility

๐ŸŽฎ Controls

Key Action
Arrow Up โฌ†๏ธ Move Up
Arrow Down โฌ‡๏ธ Move Down
Arrow Left โฌ…๏ธ Move Left
Arrow Right โžก๏ธ Move Right
Start/Pause Button Start or pause the game
Restart Button Restart the game
Speed Up Button Increase game speed
Speed Down Button Decrease game speed
Theme Button Toggle light/dark mode

๐Ÿš€ Getting Started

Prerequisites

  • A modern web browser (Chrome, Firefox, Safari, Edge)
  • No additional software required!

Installation

  1. Clone the repository

    git clone https://github.com/PiyushRepos/snake-game.git
  2. Navigate to the project directory

    cd snake-game
  3. Open in browser

    • Simply open index.html in your web browser

    • Or use a local server:

      # Using Python 3
      python -m http.server 8000
      
      # Using Node.js
      npx serve
    • Then visit http://localhost:8000

๐Ÿ“ Project Structure

snake-game/
โ”‚
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ snake-game-banner.svg   # README banner image
โ”‚   โ”œโ”€โ”€ default-og.png          # Open Graph image
โ”‚   โ””โ”€โ”€ light-mode.png          # Screenshot (if available)
โ”œโ”€โ”€ index.html                  # Main HTML file
โ”œโ”€โ”€ script.js                   # Game logic and functionality
โ”œโ”€โ”€ styles.css                  # Styling and themes
โ””โ”€โ”€ README.md                   # Project documentation

๐ŸŽฏ How to Play

  1. Start the Game - Click the "Start" button or press it to begin
  2. Control the Snake - Use arrow keys to change direction
  3. Eat Apples ๐ŸŽ - Guide the snake to the red apple to grow longer
  4. Avoid Collision - Don't run into yourself!
  5. Beat Your High Score - Try to grow the longest snake possible

Game Rules

  • The snake moves continuously in the current direction
  • You cannot reverse direction (e.g., if moving right, can't go directly left)
  • Each apple eaten increases your score by 1
  • The snake grows longer with each apple consumed
  • Game ends when the snake collides with itself
  • The snake wraps around screen edges (goes through walls)

๐Ÿ› ๏ธ Technologies Used

  • HTML5 - Structure and semantics
  • CSS3 - Styling, animations, and responsive design
    • CSS Grid for game board layout
    • CSS Custom Properties for theming
    • CSS Animations for visual effects
  • Vanilla JavaScript - Game logic and interactivity
    • DOM manipulation
    • Event handling
    • Local Storage API
    • Game loop with setInterval

๐ŸŽจ Customization

Changing Game Speed

You can now adjust the game speed in real-time using the Speed Up and Speed Down buttons during gameplay!

Alternatively, edit the initial SPEED variable in script.js:

let SPEED = 150; // milliseconds (lower = faster)

Speed Range: 50ms (fastest) to 500ms (slowest)

Changing Block Size

Modify the --block-size CSS variable in styles.css:

:root {
  --block-size: 30px; /* Change this value */
}

Adding New Themes

Extend the CSS variables in the .dark class or create new theme classes in styles.css.

๐ŸŒŸ Features in Detail

High Score System

  • Automatically saves your highest score to browser local storage
  • Persists across browser sessions
  • Updates in real-time when you beat your record

Dynamic Speed Control

  • Adjust game speed on-the-fly with Speed Up/Down buttons
  • Speed range from 50ms (blazing fast) to 500ms (beginner-friendly)
  • Changes take effect immediately during active gameplay
  • Great for progressive difficulty or accessibility needs

Theme Support

  • Light mode (default) with bright, clean aesthetics
  • Dark mode for comfortable night-time gaming
  • Theme preference saved in local storage
  • Smooth transitions between themes

Responsive Design

  • Adapts to different screen sizes
  • Touch-friendly on mobile devices
  • Flexible grid layout adjusts to available space
  • Visual distinction between snake head and body segments

๐Ÿ“Š Browser Compatibility

  • โœ… Chrome (latest)
  • โœ… Firefox (latest)
  • โœ… Safari (latest)
  • โœ… Edge (latest)
  • โœ… Opera (latest)

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is open source and available under the MIT License.

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

Piyush Kumar

๐Ÿ™ Acknowledgments

  • Inspired by the classic Nokia Snake game
  • Built as a learning project for vanilla JavaScript game development
  • Thanks to the open-source community for inspiration

๐Ÿ“ธ Screenshots

Light Mode

Light Mode Screenshot

Dark Mode

Dark Mode Screenshot


Enjoy the game! ๐ŸŽฎ๐Ÿ

If you like this project, please give it a โญ

Releases

No releases published

Packages

No packages published