|
| 1 | +let board = document.getElementById('board') |
| 2 | +let scoreCont = document.getElementById('score') |
| 3 | +let maxScoreCont = document.getElementById('maxScoreCont'); |
| 4 | +let HeadEle; |
| 5 | +// console.log(HeadEle); |
| 6 | +let inputDir = { x: 0, y: 0 }; |
| 7 | + |
| 8 | +const foodSound = new Audio('music/food.mp3'); |
| 9 | +const gameOverSound = new Audio('music/gameOver.mp3'); |
| 10 | +const moveSound = new Audio('music/move.mp3'); |
| 11 | +const musicSound = new Audio('music/music.mp3'); |
| 12 | +let speed = 5; |
| 13 | +let lastPaintTime = 0; |
| 14 | +let snakeArr = [ |
| 15 | + { x: 13, y: 15 } |
| 16 | +] |
| 17 | +let food = { |
| 18 | + x: 6, y: 7 |
| 19 | +}; |
| 20 | + |
| 21 | +// Game Functions |
| 22 | +function main(ctime) { |
| 23 | + window.requestAnimationFrame(main); |
| 24 | + if ((ctime - lastPaintTime) / 1000 < (1 / speed)) { |
| 25 | + return; |
| 26 | + } |
| 27 | + // console.log(ctime); |
| 28 | + lastPaintTime = ctime; |
| 29 | + gameEngine(); |
| 30 | + // console.log(ctime); |
| 31 | +} |
| 32 | +function isCollide(snake) { |
| 33 | + // return false; |
| 34 | + //if you into yourself |
| 35 | + |
| 36 | + if (snake[0].x > 18 || snake[0].x < 0 || snake[0].y > 18 || snake[0].y < 0) { |
| 37 | + return true; |
| 38 | + } |
| 39 | +} |
| 40 | +function gameEngine() { |
| 41 | + //part1: updating the snake array and food |
| 42 | + if (isCollide(snakeArr)) { |
| 43 | + gameOverSound.play(); |
| 44 | + musicSound.pause(); |
| 45 | + inputDir = { x: 0, y: 0 }; |
| 46 | + alert("Game over. Press any key to play again"); |
| 47 | + snakeArr = [{ x: 13, y: 15 }]; |
| 48 | + // musicSound.play(); |
| 49 | + } |
| 50 | + |
| 51 | + //IF you have eaten the food, increment the score and regenerate the food |
| 52 | + if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) { |
| 53 | + // console.log("food") |
| 54 | + foodSound.play(); |
| 55 | + |
| 56 | + snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y }); |
| 57 | + // console.log(snakeArr) |
| 58 | + let a = 2; |
| 59 | + let b = 16; |
| 60 | + food = { x: 2 + Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random()) } |
| 61 | + } |
| 62 | + |
| 63 | + //Moving the snake |
| 64 | + // console.log("-----") |
| 65 | + // console.log(snakeArr.l) |
| 66 | + for (let i = snakeArr.length - 2; i >= 0; i--) { |
| 67 | + // const element = array[i]; |
| 68 | + // console.log("hello"); |
| 69 | + snakeArr[i + 1] = { ...snakeArr[i] }; |
| 70 | + // console.log(snakeArr[i + 1].x); |
| 71 | + |
| 72 | + } |
| 73 | + snakeArr[0].x += inputDir.x; |
| 74 | + snakeArr[0].y += inputDir.y; |
| 75 | + |
| 76 | + //part2: display the snake and food |
| 77 | + //display the snake |
| 78 | + board.innerHTML = ""; |
| 79 | + snakeArr.forEach((e, index) => { |
| 80 | + snakeElement = document.createElement('div'); |
| 81 | + snakeElement.style.gridRowStart = e.y; |
| 82 | + snakeElement.style.gridColumnStart = e.x; |
| 83 | + if (index === 0) { |
| 84 | + eyes = document.createElement('div'); |
| 85 | + eyes.classList.add('eyes'); |
| 86 | + eyes2 = document.createElement('div'); |
| 87 | + eyes2.classList.add('eyes'); |
| 88 | + snakeElement.classList.add('head'); |
| 89 | + // HeadEle = document.querySelectorAll('.head'); |
| 90 | + // console.log(e.x, e.y, typeof e.x, typeof e.y) |
| 91 | + if (inputDir.x === 0 && inputDir.y === -1) { |
| 92 | + snakeElement.style.setProperty('--top', '15%'); |
| 93 | + snakeElement.style.setProperty('--bottom', '75%'); |
| 94 | + snakeElement.style.setProperty('--left', '2%'); |
| 95 | + snakeElement.style.setProperty('--right', '2%'); |
| 96 | + snakeElement.style.setProperty('--direction', 'row'); |
| 97 | + } |
| 98 | + else if (inputDir.x === 0 && inputDir.y === 1) { |
| 99 | + snakeElement.style.setProperty('--top', '75%'); |
| 100 | + snakeElement.style.setProperty('--bottom', '15%'); |
| 101 | + snakeElement.style.setProperty('--left', '2%'); |
| 102 | + snakeElement.style.setProperty('--right', '2%'); |
| 103 | + snakeElement.style.setProperty('--direction', 'row'); |
| 104 | + } |
| 105 | + else if (inputDir.x === -1 && inputDir.y === 0) { |
| 106 | + snakeElement.style.setProperty('--top', '2%'); |
| 107 | + snakeElement.style.setProperty('--bottom', '2%'); |
| 108 | + snakeElement.style.setProperty('--left', '15%'); |
| 109 | + snakeElement.style.setProperty('--right', '75%'); |
| 110 | + snakeElement.style.setProperty('--direction', 'column'); |
| 111 | + } |
| 112 | + else if (inputDir.x === 1 && inputDir.y === 0) { |
| 113 | + snakeElement.style.setProperty('--top', '2%'); |
| 114 | + snakeElement.style.setProperty('--bottom', '2%'); |
| 115 | + snakeElement.style.setProperty('--left', '75%'); |
| 116 | + snakeElement.style.setProperty('--right', '15%'); |
| 117 | + snakeElement.style.setProperty('--direction', 'column'); |
| 118 | + } |
| 119 | + board.appendChild(snakeElement); |
| 120 | + snakeElement.appendChild(eyes); |
| 121 | + snakeElement.appendChild(eyes2); |
| 122 | + } |
| 123 | + else { |
| 124 | + snakeElement.classList.add('snake'); |
| 125 | + board.appendChild(snakeElement) |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + //part2: display the snake |
| 130 | + |
| 131 | + foodElement = document.createElement('div'); |
| 132 | + foodElement.style.gridRowStart = food.y; |
| 133 | + foodElement.style.gridColumnStart = food.x; |
| 134 | + foodElement.classList.add('food'); |
| 135 | + board.appendChild(foodElement); |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +//Main logic starts here |
| 149 | +window.requestAnimationFrame(main); |
| 150 | +window.addEventListener('keydown', e => { |
| 151 | + // inputDir = { x: 0, y: 1 } //start the game |
| 152 | + moveSound.play(); |
| 153 | + switch (e.key) { |
| 154 | + case "ArrowUp": |
| 155 | + inputDir.x = 0; |
| 156 | + inputDir.y = -1; |
| 157 | + |
| 158 | + break; |
| 159 | + case "ArrowDown": |
| 160 | + inputDir.x = 0; |
| 161 | + inputDir.y = 1; |
| 162 | + |
| 163 | + break; |
| 164 | + case "ArrowLeft": |
| 165 | + inputDir.x = -1; |
| 166 | + inputDir.y = 0; |
| 167 | + |
| 168 | + break; |
| 169 | + case "ArrowRight": |
| 170 | + inputDir.x = 1; |
| 171 | + inputDir.y = 0; |
| 172 | + break; |
| 173 | + default: |
| 174 | + break; |
| 175 | + } |
| 176 | +}); |
| 177 | + |
0 commit comments