|
| 1 | +var ball = document.getElementById('ball'); |
| 2 | +var rod1 = document.getElementById('rod1'); |
| 3 | +var rod2 = document.getElementById('rod2'); |
| 4 | + |
| 5 | + |
| 6 | +const storeName = "PPName"; |
| 7 | +const storeScore = "PPMaxScore"; |
| 8 | +const rod1Name = "Rod 1"; |
| 9 | +const rod2Name = "Rod 2"; |
| 10 | + |
| 11 | + |
| 12 | +let score, |
| 13 | + maxScore, |
| 14 | + movement, |
| 15 | + rod, |
| 16 | + ballSpeedX = 2, |
| 17 | + ballSpeedY = 2; |
| 18 | + |
| 19 | +let gameOn = false; |
| 20 | + |
| 21 | +let windowWidth = window.innerWidth, |
| 22 | + windowHeight = window.innerHeight; |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +(function () { |
| 27 | + rod = localStorage.getItem(storeName); |
| 28 | + maxScore = localStorage.getItem(storeScore); |
| 29 | + |
| 30 | + if (rod === "null" || maxScore === "null") { |
| 31 | + alert("This is the first time you are playing this game. LET'S START"); |
| 32 | + maxScore = 0; |
| 33 | + rod = "Rod1" |
| 34 | + } else { |
| 35 | + alert(rod + " has maximum score of " + maxScore * 100); |
| 36 | + } |
| 37 | + |
| 38 | + resetBoard(rod); |
| 39 | +})(); |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +function resetBoard(rodName) { |
| 44 | + |
| 45 | + rod1.style.left = (window.innerWidth - rod1.offsetWidth) / 2 + 'px'; |
| 46 | + rod2.style.left = (window.innerWidth - rod2.offsetWidth) / 2 + 'px'; |
| 47 | + ball.style.left = (windowWidth - ball.offsetWidth) / 2 + 'px'; |
| 48 | + |
| 49 | + |
| 50 | + // Lossing player gets the ball |
| 51 | + if (rodName === rod2Name) { |
| 52 | + ball.style.top = (rod1.offsetTop + rod1.offsetHeight) + 'px'; |
| 53 | + ballSpeedY = 2; |
| 54 | + } else if (rodName === rod1Name) { |
| 55 | + ball.style.top = (rod2.offsetTop - rod2.offsetHeight) + 'px'; |
| 56 | + ballSpeedY = -2; |
| 57 | + } |
| 58 | + |
| 59 | + score = 0; |
| 60 | + gameOn = false; |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +function storeWin(rod, score) { |
| 67 | + |
| 68 | + if (score > maxScore) { |
| 69 | + maxScore = score; |
| 70 | + localStorage.setItem(storeName, rod); |
| 71 | + localStorage.setItem(storeScore, maxScore); |
| 72 | + } |
| 73 | + |
| 74 | + clearInterval(movement); |
| 75 | + resetBoard(rod); |
| 76 | + |
| 77 | + alert(rod + " wins with a score of " + (score * 100) + ". Max score is: " + (maxScore * 100)); |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +window.addEventListener('keypress', function () { |
| 84 | + let rodSpeed = 20; |
| 85 | + |
| 86 | + let rodRect = rod1.getBoundingClientRect(); |
| 87 | + |
| 88 | + |
| 89 | + if (event.code === "KeyD" && ((rodRect.x + rodRect.width) < window.innerWidth)) { |
| 90 | + rod1.style.left = (rodRect.x) + rodSpeed + 'px'; |
| 91 | + rod2.style.left = rod1.style.left; |
| 92 | + } else if (event.code === "KeyA" && (rodRect.x > 0)) { |
| 93 | + rod1.style.left = (rodRect.x) - rodSpeed + 'px'; |
| 94 | + rod2.style.left = rod1.style.left; |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + if (event.code === "Enter") { |
| 99 | + |
| 100 | + if (!gameOn) { |
| 101 | + gameOn = true; |
| 102 | + let ballRect = ball.getBoundingClientRect(); |
| 103 | + let ballX = ballRect.x; |
| 104 | + let ballY = ballRect.y; |
| 105 | + let ballDia = ballRect.width; |
| 106 | + |
| 107 | + let rod1Height = rod1.offsetHeight; |
| 108 | + let rod2Height = rod2.offsetHeight; |
| 109 | + let rod1Width = rod1.offsetWidth; |
| 110 | + let rod2Width = rod2.offsetWidth; |
| 111 | + |
| 112 | + |
| 113 | + movement = setInterval(function () { |
| 114 | + // Move ball |
| 115 | + ballX += ballSpeedX; |
| 116 | + ballY += ballSpeedY; |
| 117 | + |
| 118 | + rod1X = rod1.getBoundingClientRect().x; |
| 119 | + rod2X = rod2.getBoundingClientRect().x; |
| 120 | + |
| 121 | + ball.style.left = ballX + 'px'; |
| 122 | + ball.style.top = ballY + 'px'; |
| 123 | + |
| 124 | + |
| 125 | + if ((ballX + ballDia) > windowWidth || ballX < 0) { |
| 126 | + ballSpeedX = -ballSpeedX; // Reverses the direction |
| 127 | + } |
| 128 | + |
| 129 | + // It specifies the center of the ball on the viewport |
| 130 | + let ballPos = ballX + ballDia / 2; |
| 131 | + |
| 132 | + // Check for Rod 1 |
| 133 | + if (ballY <= rod1Height) { |
| 134 | + ballSpeedY = -ballSpeedY; // Reverses the direction |
| 135 | + score++; |
| 136 | + |
| 137 | + // Check if the game ends |
| 138 | + if ((ballPos < rod1X) || (ballPos > (rod1X + rod1Width))) { |
| 139 | + storeWin(rod2Name, score); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Check for Rod 2 |
| 144 | + else if ((ballY + ballDia) >= (windowHeight - rod2Height)) { |
| 145 | + ballSpeedY = -ballSpeedY; // Reverses the direction |
| 146 | + score++; |
| 147 | + |
| 148 | + // Check if the game ends |
| 149 | + if ((ballPos < rod2X) || (ballPos > (rod2X + rod2Width))) { |
| 150 | + storeWin(rod1Name, score); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + }, 10); |
| 155 | + |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +}); |
0 commit comments