NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

{
const squares = document.querySelectorAll('.grid div')
const scoreDisplay = document.querySelector('span')
const startBtn = document.querySelector('.start')

const width = 10
let currentIndex = 0 //so first div in our grid
let appleIndex = 0 //so first div in our grid
let currentSnake = [2,1,0]
let direction = 1
let score = 0
let speed = 0.9
let intervalTime = 0
let interval = 0


//to start, and restart the game
function startGame() {
currentSnake.forEach(index => squares[index].classList.remove('snake'))
squares[appleIndex].classList.remove('apple')
clearInterval(interval)
score = 0
randomApple()
direction = 1
scoreDisplay.innerText = score
intervalTime = 1000
currentSnake = [2,1,0]
currentIndex = 0
currentSnake.forEach(index => squares[index].classList.add('snake'))
interval = setInterval(moveOutcomes, intervalTime)
}


//function that deals with ALL the ove outcomes of the Snake
function moveOutcomes() {

//deals with snake hitting border and snake hitting self
if (
(currentSnake[0] + width >= (width * width) && direction === width ) || //if snake hits bottom
(currentSnake[0] % width === width -1 && direction === 1) || //if snake hits right wall
(currentSnake[0] % width === 0 && direction === -1) || //if snake hits left wall
(currentSnake[0] - width < 0 && direction === -width) || //if snake hits the top
squares[currentSnake[0] + direction].classList.contains('snake') //if snake goes into itself
) {
return clearInterval(interval) //this will clear the interval if any of the above happen
}

const tail = currentSnake.pop() //removes last ite of the array and shows it
squares[tail].classList.remove('snake') //removes class of snake from the TAIL
currentSnake.unshift(currentSnake[0] + direction) //gives direction to the head of the array

//deals with snake getting apple
if(squares[currentSnake[0]].classList.contains('apple')) {
squares[currentSnake[0]].classList.remove('apple')
squares[tail].classList.add('snake')
currentSnake.push(tail)
randomApple()
score++
scoreDisplay.textContent = score
clearInterval(interval)
intervalTime = intervalTime * speed
interval = setInterval(moveOutcomes, intervalTime)
}
squares[currentSnake[0]].classList.add('snake')
}


//generate new apple once apple is eaten
function randomApple() {
do{
appleIndex = Math.floor(Math.random() * squares.length)
} while(squares[appleIndex].classList.contains('snake')) //making sure apples dont appear on the snake
squares[appleIndex].classList.add('apple')
}


//assign functions to keycodes
function control(e) {
squares[currentIndex].classList.remove('snake')

if(e.keyCode === 39) {
direction = 1 //if we press the right arrow on our keyboard, the snake will go right one
} else if (e.keyCode === 38) {
direction = -width // if we press the up arrow, the snake will go back ten divs, appearing to go up
} else if (e.keyCode === 37) {
direction = -1 // if we press left, the snake will go left one div
} else if (e.keyCode === 40) {
direction = +width //if we press down, the snake head will instantly appear in the div ten divs from where you are now
}
}

document.addEventListener('keyup', control)
startBtn.addEventListener('click', startGame)
})
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.