|
| 1 | +import Ball from "./Ball.js"; |
| 2 | +import Paddle from "./Paddle.js"; |
| 3 | + |
| 4 | +const ballElement = document.getElementById("ball"); |
| 5 | +const paddleTopElement = document.getElementsByClassName("paddle")[0]; |
| 6 | +const paddleBottomElement = document.getElementsByClassName("paddle")[1]; |
| 7 | +const paddleTopScoreElement = document.getElementById("rod1"); |
| 8 | +const paddleBottomScoreElement = document.getElementById("rod2"); |
| 9 | +const ball = new Ball(ballElement); |
| 10 | +const paddleTop = new Paddle(paddleTopElement); |
| 11 | +const paddleBottom = new Paddle(paddleBottomElement); |
| 12 | + |
| 13 | +ball.score = [paddleTopScoreElement, paddleBottomScoreElement]; |
| 14 | + |
| 15 | +let maxScore; |
| 16 | +let maxRod; |
| 17 | +let whoWon = ""; |
| 18 | +let windowHeight = window.innerHeight; |
| 19 | +let score = 0; |
| 20 | + |
| 21 | +//-------------------------------------------------------------- |
| 22 | +//Welcome Function// |
| 23 | +(function () { |
| 24 | + maxScore = localStorage.getItem("maxScore"); |
| 25 | + maxRod = localStorage.getItem("maxRod"); |
| 26 | + if (maxRod === null || maxScore === null) { |
| 27 | + alert( |
| 28 | + "This is the first time you are playing this game.\nLET'S START !!!" |
| 29 | + ); |
| 30 | + maxScore = 0; |
| 31 | + maxRod = "Rod 1"; |
| 32 | + return; |
| 33 | + } else { |
| 34 | + alert(`${maxRod} has maximum score of ${maxScore}`); |
| 35 | + return; |
| 36 | + } |
| 37 | +})(); |
| 38 | +//-------------------------------------------------------------- |
| 39 | +//Animation Update Function// |
| 40 | +/*There are lags in between the frames. Request animation checks for whenever there's a change in the animation on the screen and runs.*/ |
| 41 | +/*setInterval((time) => { |
| 42 | +update(time) |
| 43 | +}, 10);*/ |
| 44 | + |
| 45 | +//currentTime is how much time has passed till now. At what time we are now. |
| 46 | +// previousTime is the last moment of time before the change in the animation frame. |
| 47 | +// delta is the time frame between the last frame and the new frame. |
| 48 | +//-------------------------------------------------------------- |
| 49 | +//Animation Update Function// |
| 50 | +let previousTime; |
| 51 | +let clearFrame, |
| 52 | + clearFrame2, |
| 53 | + animation = true; |
| 54 | +let updateAnimationFrame = (currentTime) => { |
| 55 | + if (previousTime != null) { |
| 56 | + //Δ Negligible amount of Time |
| 57 | + const delta = currentTime - previousTime; |
| 58 | + //Calling another animation function in Ball with Delta for variation & paddles for the collision |
| 59 | + ball.updateAnimationFrame(delta, [paddleTop.rect, paddleBottom.rect]); |
| 60 | + //Hue variation wrt Delta |
| 61 | + const hue = parseFloat( |
| 62 | + getComputedStyle(document.documentElement).getPropertyValue("--hue") |
| 63 | + ); |
| 64 | + document.documentElement.style.setProperty("--hue", hue + delta * 0.01); |
| 65 | + // If Lost the match? |
| 66 | + if (lost()) { |
| 67 | + //Game is OFF |
| 68 | + ball.GAME_ON = false; |
| 69 | + //Stop Animation |
| 70 | + animation = false; |
| 71 | + } else { |
| 72 | + //Continue Game |
| 73 | + ball.GAME_ON = true; |
| 74 | + //Continue Animation |
| 75 | + animation = true; |
| 76 | + } |
| 77 | + } |
| 78 | + //If animation is over? |
| 79 | + if (!animation) { |
| 80 | + //Cancel the animation frame |
| 81 | + window.cancelAnimationFrame(clearFrame); |
| 82 | + //End the Match |
| 83 | + matchEnds(); |
| 84 | + console.log("clearFrame1: ", clearFrame); |
| 85 | + //Fetch Local Stoarge |
| 86 | + const localScore = localStorage.getItem("maxScore"); |
| 87 | + const localRod = localStorage.getItem("maxRod"); |
| 88 | + alert( |
| 89 | + `${whoWon} wins with a score of ${score}\nMax Score is: ${localScore} by ${localRod}` |
| 90 | + ); |
| 91 | + } else { |
| 92 | + //Current Time will become Previous Time, so that the time moves forward |
| 93 | + previousTime = currentTime; |
| 94 | + //Fetching clearframe ID & start the animation loop |
| 95 | + clearFrame = window.requestAnimationFrame(updateAnimationFrame); |
| 96 | + console.log("RUNNING...."); |
| 97 | + } |
| 98 | +}; |
| 99 | +//-------------------------------------------------------------- |
| 100 | +//Match Lost Deciding Function// |
| 101 | +let lost = () => { |
| 102 | + const ballRect = ball.rect; |
| 103 | + //Ball goes beyond bottom |
| 104 | + if (ballRect.bottom >= windowHeight) { |
| 105 | + whoWon = "Rod 1"; |
| 106 | + ball.paddleTopScore += 100; |
| 107 | + } |
| 108 | + //Ball goes beyond top |
| 109 | + else if (ballRect.top <= 0) { |
| 110 | + whoWon = "Rod 2"; |
| 111 | + ball.paddleBottomScore += 100; |
| 112 | + } |
| 113 | + return ballRect.bottom >= windowHeight || ballRect.top <= 0; |
| 114 | +}; |
| 115 | +//-------------------------------------------------------------- |
| 116 | +//Local Storage Function// |
| 117 | +let storeScore = (maxScore, score, whoWon) => { |
| 118 | + if (maxScore < score) { |
| 119 | + maxScore = score; |
| 120 | + maxRod = whoWon; |
| 121 | + //Storing in Local Storage |
| 122 | + localStorage.setItem("maxScore", maxScore); |
| 123 | + localStorage.setItem("maxRod", maxRod); |
| 124 | + console.log(maxScore, score, whoWon); |
| 125 | + } |
| 126 | +}; |
| 127 | +//-------------------------------------------------------------- |
| 128 | +//Handling the Match Lose Function// |
| 129 | +let matchEnds = () => { |
| 130 | + maxScore = localStorage.getItem("maxScore"); |
| 131 | + if (maxScore === null) { |
| 132 | + maxScore = 0; |
| 133 | + } |
| 134 | + if (whoWon === "Rod 1") { |
| 135 | + score = ball.paddleTopScore; |
| 136 | + } else if (whoWon === "Rod 2") { |
| 137 | + score = ball.paddleBottomScore; |
| 138 | + } |
| 139 | + //Store the score and reset everything else |
| 140 | + storeScore(maxScore, score, whoWon); |
| 141 | + ball.reset(); |
| 142 | + paddleTop.reset(); |
| 143 | + paddleBottom.reset(); |
| 144 | + ball.paddleTopScoreElement.innerText = 0; |
| 145 | + ball.paddleBottomScoreElement.innerText = 0; |
| 146 | +}; |
| 147 | +//-------------------------------------------------------------- |
| 148 | +// D A ENTER - Keypress Events// |
| 149 | +window.addEventListener("keypress", (event) => { |
| 150 | + let paddlePositionX = paddleTop.positionX; |
| 151 | + //Move the Paddle Right |
| 152 | + if (event.keyCode === 100 && paddlePositionX < 86) { |
| 153 | + paddleTop.positionX = paddlePositionX + 2; |
| 154 | + paddleBottom.positionX = paddlePositionX + 2; |
| 155 | + } |
| 156 | + //Move the Paddle Left |
| 157 | + else if (event.keyCode === 97 && paddlePositionX > 0) { |
| 158 | + paddleTop.positionX = paddlePositionX - 2; |
| 159 | + paddleBottom.positionX = paddlePositionX - 2; |
| 160 | + } |
| 161 | + //Start the Game & Move the Ball |
| 162 | + else if (event.keyCode === 13 && ball.GAME_ON === false) { |
| 163 | + //Clear the Initial Animation Frame |
| 164 | + window.cancelAnimationFrame(clearFrame2); |
| 165 | + console.log("clearFrame2: ", clearFrame2); |
| 166 | + //Game ON |
| 167 | + ball.GAME_ON = true; |
| 168 | + //Previous Time is null, so that the animation loop starts from the very first beginning as it has only the current time at the start |
| 169 | + previousTime = null; |
| 170 | + //Animation ON |
| 171 | + animation = true; |
| 172 | + //Reset everything else |
| 173 | + ball.reset(); |
| 174 | + paddleTop.reset(); |
| 175 | + paddleBottom.reset(); |
| 176 | + paddleTopScoreElement.innerText = 0; |
| 177 | + paddleBottomScoreElement.innerText = 0; |
| 178 | + //Fetching clearframe ID & start the animation loop |
| 179 | + clearFrame2 = window.requestAnimationFrame(updateAnimationFrame); |
| 180 | + } |
| 181 | +}); |
| 182 | +//-------------------------------------------------------------- |
| 183 | +// <== ==> - Keydown Events// |
| 184 | +window.addEventListener("keydown", (event) => { |
| 185 | + let paddlePositionX = paddleTop.positionX; |
| 186 | + //Move the Paddle Right |
| 187 | + if (event.keyCode === 39 && paddlePositionX < 86) { |
| 188 | + paddleTop.positionX = paddlePositionX + 2; |
| 189 | + paddleBottom.positionX = paddlePositionX + 2; |
| 190 | + } |
| 191 | + //Move the Paddle Left |
| 192 | + else if (event.keyCode === 37 && paddlePositionX > 0) { |
| 193 | + paddleTop.positionX = paddlePositionX - 2; |
| 194 | + paddleBottom.positionX = paddlePositionX - 2; |
| 195 | + } |
| 196 | +}); |
| 197 | +//-------------------------------------------------------------- |
0 commit comments