Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to make Happy birthday cake Animation using HTML & CSS

    29 May 2025

    How to create Toast Catcher Game using HTML CSS and JS

    26 May 2025

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to make Glowing Snake Game using HTML CSS & JavaScript
    JavaScript

    How to make Glowing Snake Game using HTML CSS & JavaScript

    Coding StellaBy Coding Stella23 September 2024No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create a Glowing Snake Game using HTML, CSS, and JavaScript. This project will feature a classic snake game with a modern twist: a glowing effect to make it visually appealing.

    We’ll use HTML to structure the game, CSS to style it and add the glowing effect, and JavaScript to handle the game logic.

    Let’s get started on building the Glowing Snake Game. Whether you’re a beginner or an experienced developer, this project offers a fun and interactive way to practice your web development skills and create an engaging game. Let’s bring some glow to the classic snake game!

    HTML :

    This HTML code sets up a simple webpage for a vanilla JavaScript snake game. It includes a linked CSS file (style.css) for styling and a JavaScript file (script.js) for functionality. The page contains a button to restart the game and a section to display the score. Font Awesome icons are used for the button icon, and the game will likely be displayed inside the #canvas div. The defer attribute on the script tags ensures that the JavaScript is executed after the HTML is loaded.

    <!DOCTYPE html>
    <html lang="en" >
    <head>
      <meta charset="UTF-8">
      <title>Vanilla JS Snake</title>
    
      <script defer src="https://pro.fontawesome.com/releases/v5.10.0/js/all.js"
            integrity="sha384-G/ZR3ntz68JZrH4pfPJyRbjW+c0+ojii5f+GYiYwldYU69A+Ejat6yIfLSxljXxD"
            crossorigin="anonymous"></script>
    
      <link rel="stylesheet" href="./style.css">
    
    </head>
    <body>
    
      <div class="container noselect">
        <div class="wrapper">
          <button id="replay">
            <i class="fas fa-play"></i>
            RESTART
          </button>
          <div id="canvas">
      
          </div>
          <div id="ui">
            <h2>SCORE
            </h2>
            <span id="score">00</span>
          </div>
        </div>
      </div>
    
      <script  src="./script.js"></script>
    
    </body>
    </html>

    CSS :

    This CSS code styles a webpage for a simple game interface. It defines a custom font from Google Fonts called “Poppins,” sets a dark background, and styles various elements like the button, score display, and canvas. The layout is designed to be flexible and centered using flexbox, with elements like the replay button and score rotated for a unique look. There are hover effects on the button and responsive adjustments for screens narrower than 600px. This ensures that the layout remains clean and accessible on smaller devices.

    @font-face {
      font-family: "game";
      src: url("https://fonts.googleapis.com/css2?family=Poppins:wght@500;800&display=swap");
    }
    
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    button:focus {
      outline: 0;
    }
    
    html,
    body {
      height: 100%;
      font-family: "Poppins", sans-serif;
      color: #6e7888;
    }
    
    body {
      background-color: #222738;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #6e7888;
    }
    
    .wrapper {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
      align-items: center;
      margin-bottom: 20px;
    }
    
    canvas {
      background-color: #181825;
    }
    
    .container {
      display: flex;
      width: 100%;
      height: 100%;
      flex-flow: column wrap;
      justify-content: center;
      align-items: center;
    }
    
    #ui {
      display: flex;
      align-items: center;
      font-size: 10px;
      flex-flow: column;
      margin-left: 10px;
    }
    
    h2 {
      font-weight: 900;
      font-size: 10px;
      letter-spacing: 8px;
      margin-bottom: 30px;
      transform: rotate(270deg);
    }
    
    #score {
      margin-top: 20px;
      font-size: 30px;
      font-weight: 800;
      transform: rotate(-90deg);
    }
    
    .noselect {
      user-select: none;
    }
    
    #replay {
      font-size: 10px;
      padding: 10px 20px;
      background: #6e7888;
      border: none;
      color: #222738;
      border-radius: 40px;
      font-weight: 800;
      transform: rotate(270deg);
      cursor: pointer;
      transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
    }
    #replay:hover {
      background: #a6aab5;
      background: #4cffd7;
      transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
    }
    #replay svg {
      margin-right: 8px;
    }
    
    @media (max-width: 600px) {
      #replay {
        margin-bottom: 20px;
      }
      #replay,
      h2 {
        transform: rotate(0deg);
      }
      #ui {
        flex-flow: row wrap;
        margin-bottom: 20px;
      }
      #score {
        margin-top: 0;
        margin-left: 20px;
      }
      .container {
        flex-flow: column wrap;
      }
    }

    JavaScript:

    This JavaScript code is for a simple snake game using the HTML canvas. It sets up the game board and initializes a snake that moves based on arrow key inputs. The snake grows when it “eats” food, which spawns randomly on the grid. If the snake collides with itself or the walls, the game ends. The score is updated each time the snake eats food, and the maximum score is saved in local storage. The game also includes a particle effect that triggers when the snake eats food. A “restart” button allows players to reset and start the game again.

    let dom_replay = document.querySelector("#replay");
    let dom_score = document.querySelector("#score");
    let dom_canvas = document.createElement("canvas");
    document.querySelector("#canvas").appendChild(dom_canvas);
    let CTX = dom_canvas.getContext("2d");
    
    const W = (dom_canvas.width = 400);
    const H = (dom_canvas.height = 400);
    
    let snake,
      food,
      currentHue,
      cells = 20,
      cellSize,
      isGameOver = false,
      tails = [],
      score = 00,
      maxScore = window.localStorage.getItem("maxScore") || undefined,
      particles = [],
      splashingParticleCount = 20,
      cellsCount,
      requestID;
    
    let helpers = {
      Vec: class {
        constructor(x, y) {
          this.x = x;
          this.y = y;
        }
        add(v) {
          this.x += v.x;
          this.y += v.y;
          return this;
        }
        mult(v) {
          if (v instanceof helpers.Vec) {
            this.x *= v.x;
            this.y *= v.y;
            return this;
          } else {
            this.x *= v;
            this.y *= v;
            return this;
          }
        }
      },
      isCollision(v1, v2) {
        return v1.x == v2.x && v1.y == v2.y;
      },
      garbageCollector() {
        for (let i = 0; i < particles.length; i++) {
          if (particles[i].size <= 0) {
            particles.splice(i, 1);
          }
        }
      },
      drawGrid() {
        CTX.lineWidth = 1.1;
        CTX.strokeStyle = "#232332";
        CTX.shadowBlur = 0;
        for (let i = 1; i < cells; i++) {
          let f = (W / cells) * i;
          CTX.beginPath();
          CTX.moveTo(f, 0);
          CTX.lineTo(f, H);
          CTX.stroke();
          CTX.beginPath();
          CTX.moveTo(0, f);
          CTX.lineTo(W, f);
          CTX.stroke();
          CTX.closePath();
        }
      },
      randHue() {
        return ~~(Math.random() * 360);
      },
      hsl2rgb(hue, saturation, lightness) {
        if (hue == undefined) {
          return [0, 0, 0];
        }
        var chroma = (1 - Math.abs(2 * lightness - 1)) * saturation;
        var huePrime = hue / 60;
        var secondComponent = chroma * (1 - Math.abs((huePrime % 2) - 1));
    
        huePrime = ~~huePrime;
        var red;
        var green;
        var blue;
    
        if (huePrime === 0) {
          red = chroma;
          green = secondComponent;
          blue = 0;
        } else if (huePrime === 1) {
          red = secondComponent;
          green = chroma;
          blue = 0;
        } else if (huePrime === 2) {
          red = 0;
          green = chroma;
          blue = secondComponent;
        } else if (huePrime === 3) {
          red = 0;
          green = secondComponent;
          blue = chroma;
        } else if (huePrime === 4) {
          red = secondComponent;
          green = 0;
          blue = chroma;
        } else if (huePrime === 5) {
          red = chroma;
          green = 0;
          blue = secondComponent;
        }
    
        var lightnessAdjustment = lightness - chroma / 2;
        red += lightnessAdjustment;
        green += lightnessAdjustment;
        blue += lightnessAdjustment;
    
        return [
          Math.round(red * 255),
          Math.round(green * 255),
          Math.round(blue * 255)
        ];
      },
      lerp(start, end, t) {
        return start * (1 - t) + end * t;
      }
    };
    
    let KEY = {
      ArrowUp: false,
      ArrowRight: false,
      ArrowDown: false,
      ArrowLeft: false,
      resetState() {
        this.ArrowUp = false;
        this.ArrowRight = false;
        this.ArrowDown = false;
        this.ArrowLeft = false;
      },
      listen() {
        addEventListener(
          "keydown",
          (e) => {
            if (e.key === "ArrowUp" && this.ArrowDown) return;
            if (e.key === "ArrowDown" && this.ArrowUp) return;
            if (e.key === "ArrowLeft" && this.ArrowRight) return;
            if (e.key === "ArrowRight" && this.ArrowLeft) return;
            this[e.key] = true;
            Object.keys(this)
              .filter((f) => f !== e.key && f !== "listen" && f !== "resetState")
              .forEach((k) => {
                this[k] = false;
              });
          },
          false
        );
      }
    };
    
    class Snake {
      constructor(i, type) {
        this.pos = new helpers.Vec(W / 2, H / 2);
        this.dir = new helpers.Vec(0, 0);
        this.type = type;
        this.index = i;
        this.delay = 5;
        this.size = W / cells;
        this.color = "white";
        this.history = [];
        this.total = 1;
      }
      draw() {
        let { x, y } = this.pos;
        CTX.fillStyle = this.color;
        CTX.shadowBlur = 20;
        CTX.shadowColor = "rgba(255,255,255,.3 )";
        CTX.fillRect(x, y, this.size, this.size);
        CTX.shadowBlur = 0;
        if (this.total >= 2) {
          for (let i = 0; i < this.history.length - 1; i++) {
            let { x, y } = this.history[i];
            CTX.lineWidth = 1;
            CTX.fillStyle = "rgba(225,225,225,1)";
            CTX.fillRect(x, y, this.size, this.size);
          }
        }
      }
      walls() {
        let { x, y } = this.pos;
        if (x + cellSize > W) {
          this.pos.x = 0;
        }
        if (y + cellSize > W) {
          this.pos.y = 0;
        }
        if (y < 0) {
          this.pos.y = H - cellSize;
        }
        if (x < 0) {
          this.pos.x = W - cellSize;
        }
      }
      controlls() {
        let dir = this.size;
        if (KEY.ArrowUp) {
          this.dir = new helpers.Vec(0, -dir);
        }
        if (KEY.ArrowDown) {
          this.dir = new helpers.Vec(0, dir);
        }
        if (KEY.ArrowLeft) {
          this.dir = new helpers.Vec(-dir, 0);
        }
        if (KEY.ArrowRight) {
          this.dir = new helpers.Vec(dir, 0);
        }
      }
      selfCollision() {
        for (let i = 0; i < this.history.length; i++) {
          let p = this.history[i];
          if (helpers.isCollision(this.pos, p)) {
            isGameOver = true;
          }
        }
      }
      update() {
        this.walls();
        this.draw();
        this.controlls();
        if (!this.delay--) {
          if (helpers.isCollision(this.pos, food.pos)) {
            incrementScore();
            particleSplash();
            food.spawn();
            this.total++;
          }
          this.history[this.total - 1] = new helpers.Vec(this.pos.x, this.pos.y);
          for (let i = 0; i < this.total - 1; i++) {
            this.history[i] = this.history[i + 1];
          }
          this.pos.add(this.dir);
          this.delay = 5;
          this.total > 3 ? this.selfCollision() : null;
        }
      }
    }
    
    class Food {
      constructor() {
        this.pos = new helpers.Vec(
          ~~(Math.random() * cells) * cellSize,
          ~~(Math.random() * cells) * cellSize
        );
        this.color = currentHue = `hsl(${~~(Math.random() * 360)},100%,50%)`;
        this.size = cellSize;
      }
      draw() {
        let { x, y } = this.pos;
        CTX.globalCompositeOperation = "lighter";
        CTX.shadowBlur = 20;
        CTX.shadowColor = this.color;
        CTX.fillStyle = this.color;
        CTX.fillRect(x, y, this.size, this.size);
        CTX.globalCompositeOperation = "source-over";
        CTX.shadowBlur = 0;
      }
      spawn() {
        let randX = ~~(Math.random() * cells) * this.size;
        let randY = ~~(Math.random() * cells) * this.size;
        for (let path of snake.history) {
          if (helpers.isCollision(new helpers.Vec(randX, randY), path)) {
            return this.spawn();
          }
        }
        this.color = currentHue = `hsl(${helpers.randHue()}, 100%, 50%)`;
        this.pos = new helpers.Vec(randX, randY);
      }
    }
    
    class Particle {
      constructor(pos, color, size, vel) {
        this.pos = pos;
        this.color = color;
        this.size = Math.abs(size / 2);
        this.ttl = 0;
        this.gravity = -0.2;
        this.vel = vel;
      }
      draw() {
        let { x, y } = this.pos;
        let hsl = this.color
          .split("")
          .filter((l) => l.match(/[^hsl()$% ]/g))
          .join("")
          .split(",")
          .map((n) => +n);
        let [r, g, b] = helpers.hsl2rgb(hsl[0], hsl[1] / 100, hsl[2] / 100);
        CTX.shadowColor = `rgb(${r},${g},${b},${1})`;
        CTX.shadowBlur = 0;
        CTX.globalCompositeOperation = "lighter";
        CTX.fillStyle = `rgb(${r},${g},${b},${1})`;
        CTX.fillRect(x, y, this.size, this.size);
        CTX.globalCompositeOperation = "source-over";
      }
      update() {
        this.draw();
        this.size -= 0.3;
        this.ttl += 1;
        this.pos.add(this.vel);
        this.vel.y -= this.gravity;
      }
    }
    
    function incrementScore() {
      score++;
      dom_score.innerText = score.toString().padStart(2, "0");
    }
    
    function particleSplash() {
      for (let i = 0; i < splashingParticleCount; i++) {
        let vel = new helpers.Vec(Math.random() * 6 - 3, Math.random() * 6 - 3);
        let position = new helpers.Vec(food.pos.x, food.pos.y);
        particles.push(new Particle(position, currentHue, food.size, vel));
      }
    }
    
    function clear() {
      CTX.clearRect(0, 0, W, H);
    }
    
    function initialize() {
      CTX.imageSmoothingEnabled = false;
      KEY.listen();
      cellsCount = cells * cells;
      cellSize = W / cells;
      snake = new Snake();
      food = new Food();
      dom_replay.addEventListener("click", reset, false);
      loop();
    }
    
    function loop() {
      clear();
      if (!isGameOver) {
        requestID = setTimeout(loop, 1000 / 60);
        helpers.drawGrid();
        snake.update();
        food.draw();
        for (let p of particles) {
          p.update();
        }
        helpers.garbageCollector();
      } else {
        clear();
        gameOver();
      }
    }
    
    function gameOver() {
      maxScore ? null : (maxScore = score);
      score > maxScore ? (maxScore = score) : null;
      window.localStorage.setItem("maxScore", maxScore);
      CTX.fillStyle = "#4cffd7";
      CTX.textAlign = "center";
      CTX.font = "bold 30px Poppins, sans-serif";
      CTX.fillText("GAME OVER", W / 2, H / 2);
      CTX.font = "15px Poppins, sans-serif";
      CTX.fillText(`SCORE   ${score}`, W / 2, H / 2 + 60);
      CTX.fillText(`MAXSCORE   ${maxScore}`, W / 2, H / 2 + 80);
    }
    
    function reset() {
      dom_score.innerText = "00";
      score = "00";
      snake = new Snake();
      food.spawn();
      KEY.resetState();
      isGameOver = false;
      clearTimeout(requestID);
      loop();
    }
    
    initialize();

    In conclusion, creating a Glowing Snake Game using HTML, CSS, and JavaScript has been an exciting and educational project. By combining HTML for structure, CSS for styling and glowing effects, and JavaScript for the game logic, we’ve crafted a modern and engaging version of the classic snake game.

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    Game snake game
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to make Pepsi Product Card using HTML & CSS
    Next Article How to make Slide-out Navigation with GSAP 3 using HTML CSS & JavaScript
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to create Toast Catcher Game using HTML CSS and JS

    26 May 2025
    JavaScript

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025
    JavaScript

    How to create Cross Road Game using HTML CSS and JS

    2 May 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202419K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202416K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202414K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202412K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to create Toast Catcher Game using HTML CSS and JS

    26 May 2025

    How to make Bouncy Image Gallery using HTML & CSS

    15 January 2024

    How to make Hover to Reveal Password using HTML CSS & JavaScript

    22 October 2024

    How to make Glowing Neon Clock using HTML CSS & JavaScript

    23 June 2024
    Latest Post

    How to make Happy birthday cake Animation using HTML & CSS

    29 May 2025

    How to create Toast Catcher Game using HTML CSS and JS

    26 May 2025

    How to create Animated No Chill Login form using HTML CSS and JS

    22 May 2025

    How to make Social media icons hover effect using HTML & CSS

    14 May 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood