Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to create a snake game in html css js

<h1>Nokia 3310 snake</h1>
<div class="scoreDisplay"></div>
<div class="grid"></div>
<div class="button">
  <button class="top">top</button>
  <button class="bottom">bottom</button>
  <button class="left">left</button>
  <button class="right">right</button>
</div>
<div class="popup">
  <button class="playAgain">play Again</button>
</div>
Comment

how to create a snake game in html css js

body {
  background: rgb(212, 211, 211);
}

.grid {
  width: 200px;
  height: 200px;
  border: 1px solid red;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
}

.grid div {
  width: 20px;
  height: 20px;
  /*border:1px black solid;
box-sizing:border-box*/
}

.snake {
  background: blue;
}

.apple {
  background: yellow;
  border-radius: 20px;
}

.popup {
  background: rgb(32, 31, 31);
  width: 100px;
  height: 100px;
  position: fixed;
  top: 100px;
  left: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
Comment

how to create a snake game in html css js

let grid = document.querySelector(".grid");
let popup = document.querySelector(".popup");
let playAgain = document.querySelector(".playAgain");
let scoreDisplay = document.querySelector(".scoreDisplay");
let left = document.querySelector(".left");
let bottom = document.querySelector(".bottom");
let right = document.querySelector(".right");
let up = document.querySelector(".top");
let width = 10;
let currentIndex = 0;
let appleIndex = 0;
let currentSnake = [2, 1, 0];
let direction = 1;
let score = 0;
let speed = 0.8;
let intervalTime = 0;
let interval = 0;
Comment

how to create a snake game in html css js

document.addEventListener("DOMContentLoaded", function () {
  document.addEventListener("keyup", control);
  createBoard();
  startGame();
  playAgain.addEventListener("click", replay);
});
Comment

how to create a snake game in html css js

function createBoard() {
  popup.style.display = "none";
  for (let i = 0; i < 100; i++) {
    let div = document.createElement("div");
    grid.appendChild(div);
  }
}
Comment

how to create a snake game in html css js

function startGame() {
  let squares = document.querySelectorAll(".grid div");
  randomApple(squares);
  //random apple
  direction = 1;
  scoreDisplay.innerHTML = score;
  intervalTime = 1000;
  currentSnake = [2, 1, 0];
  currentIndex = 0;
  currentSnake.forEach((index) => squares[index].classList.add("snake"));
  interval = setInterval(moveOutcome, intervalTime);
}
Comment

how to create a snake game in html css js

function moveSnake(squares) {
  let tail = currentSnake.pop();
  squares[tail].classList.remove("snake");
  currentSnake.unshift(currentSnake[0] + direction);
  // movement ends here
  eatApple(squares, tail);
  squares[currentSnake[0]].classList.add("snake");
}
Comment

how to create a snake game in html css js

function checkForHits(squares) {
  if (
    (currentSnake[0] + width >= width * width && direction === width) ||
    (currentSnake[0] % width === width - 1 && direction === 1) ||
    (currentSnake[0] % width === 0 && direction === -1) ||
    (currentSnake[0] - width <= 0 && direction === -width) ||
    squares[currentSnake[0] + direction].classList.contains("snake")
  ) {
    return true;
  } else {
    return false;
  }
}
Comment

how to create a snake game in html css js

function eatApple(squares, tail) {
  if (squares[currentSnake[0]].classList.contains("apple")) {
    squares[currentSnake[0]].classList.remove("apple");
    squares[tail].classList.add("snake");
    currentSnake.push(tail);
    randomApple(squares);
    score++;
    scoreDisplay.textContent = score;
    clearInterval(interval);
    intervalTime = intervalTime * speed;
    interval = setInterval(moveOutcome, intervalTime);
  }
}
Comment

how to create a snake game in html css js

function randomApple(squares) {
  do {
    appleIndex = Math.floor(Math.random() * squares.length);
  } while (squares[appleIndex].classList.contains("snake"));
  squares[appleIndex].classList.add("apple");
}
Comment

how to create a snake game in html css js

function control(e) {
  if (e.keycode === 39) {
    direction = 1; // right
  } else if (e.keycode === 38) {
    direction = -width; //if we press the up arrow, the snake will go ten divs up
  } else if (e.keycode === 37) {
    direction = -1; // left, the snake will go left one div
  } else if (e.keycode === 40) {
    direction = +width; // down the snake head will instantly appear 10 divs below from the current div
  }
}
Comment

how to create a snake game in html css js

up.addEventListener("click", () => (direction = -width));
bottom.addEventListener("click", () => (direction = +width));
left.addEventListener("click", () => (direction = -1));
right.addEventListener("click", () => (direction = 1));
Comment

how to create a snake game in html css js

function replay() {
  grid.innerHTML = "";
  createBoard();
  startGame();
  popup.style.display = "none";
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs RPL 
Javascript :: cookie clicker get all badges hack 
Javascript :: Check for particular values in the response body 
Javascript :: focus on child components on single page applications - 2 
Javascript :: Reactjs class exemple componentDidMount 
Javascript :: pixijs circle 
Javascript :: build class component react 
Javascript :: remove console messages of react-i18next 
Javascript :: show tempdata in javascript 
Javascript :: karma get attribute 
Javascript :: get day in google app script 
Javascript :: jquery confirm dialog yes no 
Javascript :: useLinkClickHandler 
Javascript :: circle rect collision 
Javascript :: double bitwise not shorthand javascript 
Javascript :: sequelize autocomplete vscode 
Javascript :: using fetch hook 
Javascript :: trigger many calls JavaScript 
Javascript :: array object make api format javascript 
Javascript :: Flutter retrieve data from Json url 
Javascript :: vscode redirect back 
Javascript :: keep records of number rolled in array javascript 
Javascript :: jquery dropdown options in laravel 
Javascript :: strapi extend user model 
Javascript :: javascript keyup original src element 
Javascript :: npm resize div 
Javascript :: javascript node retry promise.all 
Javascript :: Example of Logical OR assignment operator in es12 
Javascript :: copy file using java script 
Javascript :: Determining Truth With Logical Operators 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =