|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + const squares = document.querySelectorAll('.grid div') |
| 3 | + const scoreDisplay = document.querySelector('span') |
| 4 | + const startBtn = document.querySelector('.start') |
| 5 | + |
| 6 | + const width = 10 |
| 7 | + let currentIndex = 0 //so first div in our grid |
| 8 | + let appleIndex = 0 //so first div in our grid |
| 9 | + let currentSnake = [2,1,0] |
| 10 | + let direction = 1 |
| 11 | + let score = 0 |
| 12 | + let speed = 0.9 |
| 13 | + let intervalTime = 0 |
| 14 | + let interval = 0 |
| 15 | + |
| 16 | + |
| 17 | + //to start, and restart the game |
| 18 | + function startGame() { |
| 19 | + currentSnake.forEach(index => squares[index].classList.remove('snake')) |
| 20 | + squares[appleIndex].classList.remove('apple') |
| 21 | + clearInterval(interval) |
| 22 | + score = 0 |
| 23 | + randomApple() |
| 24 | + direction = 1 |
| 25 | + scoreDisplay.innerText = score |
| 26 | + intervalTime = 1000 |
| 27 | + currentSnake = [2,1,0] |
| 28 | + currentIndex = 0 |
| 29 | + currentSnake.forEach(index => squares[index].classList.add('snake')) |
| 30 | + interval = setInterval(moveOutcomes, intervalTime) |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + //function that deals with ALL the ove outcomes of the Snake |
| 35 | + function moveOutcomes() { |
| 36 | + |
| 37 | + //deals with snake hitting border and snake hitting self |
| 38 | + if ( |
| 39 | + (currentSnake[0] + width >= (width * width) && direction === width ) || //if snake hits bottom |
| 40 | + (currentSnake[0] % width === width -1 && direction === 1) || //if snake hits right wall |
| 41 | + (currentSnake[0] % width === 0 && direction === -1) || //if snake hits left wall |
| 42 | + (currentSnake[0] - width < 0 && direction === -width) || //if snake hits the top |
| 43 | + squares[currentSnake[0] + direction].classList.contains('snake') //if snake goes into itself |
| 44 | + ) { |
| 45 | + return clearInterval(interval) //this will clear the interval if any of the above happen |
| 46 | + } |
| 47 | + |
| 48 | + const tail = currentSnake.pop() //removes last ite of the array and shows it |
| 49 | + squares[tail].classList.remove('snake') //removes class of snake from the TAIL |
| 50 | + currentSnake.unshift(currentSnake[0] + direction) //gives direction to the head of the array |
| 51 | + |
| 52 | + //deals with snake getting apple |
| 53 | + if(squares[currentSnake[0]].classList.contains('apple')) { |
| 54 | + squares[currentSnake[0]].classList.remove('apple') |
| 55 | + squares[tail].classList.add('snake') |
| 56 | + currentSnake.push(tail) |
| 57 | + randomApple() |
| 58 | + score++ |
| 59 | + scoreDisplay.textContent = score |
| 60 | + clearInterval(interval) |
| 61 | + intervalTime = intervalTime * speed |
| 62 | + interval = setInterval(moveOutcomes, intervalTime) |
| 63 | + } |
| 64 | + squares[currentSnake[0]].classList.add('snake') |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + //generate new apple once apple is eaten |
| 69 | + function randomApple() { |
| 70 | + do{ |
| 71 | + appleIndex = Math.floor(Math.random() * squares.length) |
| 72 | + } while(squares[appleIndex].classList.contains('snake')) //making sure apples dont appear on the snake |
| 73 | + squares[appleIndex].classList.add('apple') |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + //assign functions to keycodes |
| 78 | + function control(e) { |
| 79 | + squares[currentIndex].classList.remove('snake') |
| 80 | + |
| 81 | + if(e.keyCode === 39) { |
| 82 | + direction = 1 //if we press the right arrow on our keyboard, the snake will go right one |
| 83 | + } else if (e.keyCode === 38) { |
| 84 | + direction = -width // if we press the up arrow, the snake will go back ten divs, appearing to go up |
| 85 | + } else if (e.keyCode === 37) { |
| 86 | + direction = -1 // if we press left, the snake will go left one div |
| 87 | + } else if (e.keyCode === 40) { |
| 88 | + direction = +width //if we press down, the snake head will instantly appear in the div ten divs from where you are now |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + document.addEventListener('keyup', control) |
| 93 | + startBtn.addEventListener('click', startGame) |
| 94 | +}) |
0 commit comments