| j | 90d95e4 | 2020-12-10 19:54:57 -0800 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | |
| 4 | <head> |
| 5 | <link type="text/css" rel="stylesheet" href="https://cdn.jottocraft.com/fluid/v5.min.css" |
| 6 | media="screen,projection" /> |
| 7 | <script> |
| 8 | var info = { |
| 9 | title: "404", |
| 10 | description: "The requested URL was not found", |
| 11 | status: "fail", |
| 12 | brand: { |
| 13 | name: "Power+", |
| 14 | image: "https://cdn.jottocraft.com/images/dtpsBitmap.png", |
| 15 | favicon: "/favicon.png", |
| 16 | color: "#e3ba4b" |
| 17 | } |
| 18 | } |
| 19 | document.title = info.title + ((info.brand && (" | " + info.brand.name)) || ""); |
| 20 | if (info.brand && info.brand.image) document.documentElement.style.setProperty('--brandImage', "url('" + info.brand.image + "')"); |
| 21 | if (info.brand && info.brand.color) document.documentElement.style.setProperty('--brandColor', info.brand.color); |
| 22 | </script> |
| 23 | <script>document.write(`<link rel="shortcut icon" href="${info.brand && info.brand.favicon}" type="image/png">`)</script> |
| 24 | <style> |
| 25 | h1 { |
| 26 | margin-top: 0px; |
| 27 | font-size: 68px; |
| 28 | text-shadow: 0px 0px 4px var(--secText); |
| 29 | } |
| 30 | |
| 31 | h1 span { |
| 32 | font-weight: bold; |
| 33 | vertical-align: middle; |
| 34 | } |
| 35 | |
| 36 | div.status { |
| 37 | vertical-align: middle; |
| 38 | display: inline-block; |
| 39 | width: 40px; |
| 40 | height: 40px; |
| 41 | border-radius: 50%; |
| 42 | margin-right: 5px; |
| 43 | } |
| 44 | |
| 45 | div.status.fail { |
| 46 | border: 3px solid #792020; |
| 47 | background: #e83434ba; |
| 48 | box-shadow: 0 0px 20px rgb(255 60 60 / 32%); |
| 49 | } |
| 50 | |
| 51 | div.status.success { |
| 52 | border: 3px solid #185f27; |
| 53 | background: #23b542ad; |
| 54 | box-shadow: 0 0px 20px #3cff5952; |
| 55 | } |
| 56 | |
| 57 | div.status.game { |
| 58 | border: 3px solid #211467; |
| 59 | background: #391ece8f; |
| 60 | box-shadow: 0 0px 20px rgb(74 48 191 / 32%); |
| 61 | } |
| 62 | |
| 63 | i.fail::before { |
| 64 | content: "lens"; |
| 65 | color: #c44848; |
| 66 | font-size: 32px; |
| 67 | } |
| 68 | |
| 69 | i.success::before { |
| 70 | content: "lens"; |
| 71 | color: #48c45d; |
| 72 | font-size: 32px; |
| 73 | } |
| 74 | |
| 75 | #background { |
| 76 | background-image: var(--brandImage, url('https://cdn.jottocraft.com/images/bitmap.png')); |
| 77 | opacity: 0.5; |
| 78 | bottom: 80px; |
| 79 | position: fixed; |
| 80 | z-index: -1; |
| 81 | width: 100%; |
| 82 | height: 50%; |
| 83 | background-position: bottom; |
| 84 | background-size: auto min(50%, 600px); |
| 85 | background-repeat-y: no-repeat; |
| 86 | } |
| 87 | </style> |
| 88 | </head> |
| 89 | |
| 90 | |
| 91 | <body> |
| 92 | <div class="container center"> |
| 93 | <h1> |
| 94 | <script>document.write(`<div id="pageStatus" class="status ${info.status}"></div>`)</script> |
| 95 | <span id="pageTitle"><script>document.write(info.title)</script></span> |
| 96 | <h5 id="pageDescription"> |
| 97 | <script>document.write(info.description)</script> |
| 98 | </h5> |
| 99 | </h1> |
| 100 | |
| 101 | <canvas style="margin-top: 40px; display: none;" width="788" height="500" id="game"></canvas> |
| 102 | </div> |
| 103 | <div id="background"></div> |
| 104 | |
| 105 | <script> |
| 106 | const canvas = document.getElementById('game'); |
| 107 | const context = canvas.getContext('2d'); |
| 108 | |
| 109 | const level1 = [ |
| 110 | [], |
| 111 | [], |
| 112 | [], |
| 113 | [], |
| 114 | ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'], |
| 115 | ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'], |
| 116 | ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'], |
| 117 | ['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'], |
| 118 | ['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'], |
| 119 | ['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'], |
| 120 | ['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'], |
| 121 | ['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'], |
| 122 | ['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'] |
| 123 | ]; |
| 124 | |
| 125 | // create a mapping between color short code (R, O, G, Y) and color name |
| 126 | const colorMap = { |
| 127 | //'R': '#27d69c', |
| 128 | 'O': getComputedStyle(document.body).getPropertyValue('--brandColor') || '#505ae0', |
| 129 | 'G': getComputedStyle(document.body).getPropertyValue('--brandColor') || '#4487cd', |
| 130 | 'Y': getComputedStyle(document.body).getPropertyValue('--brandColor') || '#56acbe' |
| 131 | }; |
| 132 | |
| 133 | // use a 2px gap between each brick |
| 134 | const brickGap = 2; |
| 135 | const brickWidth = 44; |
| 136 | const brickHeight = 12; |
| 137 | |
| 138 | // the wall width takes up the remaining space of the canvas width. with 14 bricks |
| 139 | // and 13 2px gaps between them, thats: 400 - (14 * 25 + 2 * 13) = 24px. so each |
| 140 | // wall will be 12px |
| 141 | const wallSize = 4; |
| 142 | const bricks = []; |
| 143 | var score = 0; |
| 144 | |
| 145 | // create the level by looping over each row and column in the level1 array |
| 146 | // and creating an object with the bricks position (x, y) and color |
| 147 | for (let row = 0; row < level1.length; row++) { |
| 148 | for (let col = 0; col < level1[row].length; col++) { |
| 149 | const colorCode = level1[row][col]; |
| 150 | |
| 151 | bricks.push({ |
| 152 | x: wallSize + (brickWidth + brickGap) * col, |
| 153 | y: wallSize + (brickHeight + brickGap) * row, |
| 154 | color: colorMap[colorCode], |
| 155 | width: brickWidth, |
| 156 | height: brickHeight |
| 157 | }); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | const paddle = { |
| 162 | // place the paddle horizontally in the middle of the screen |
| 163 | x: canvas.width / 2 - brickWidth / 2, |
| 164 | y: 440, |
| 165 | width: brickWidth * 3, |
| 166 | height: brickHeight, |
| 167 | |
| 168 | // paddle x velocity |
| 169 | dx: 0 |
| 170 | }; |
| 171 | |
| 172 | const ball = { |
| 173 | x: 130, |
| 174 | y: 260, |
| 175 | width: 8, |
| 176 | height: 8, |
| 177 | |
| 178 | // how fast the ball should go in either the x or y direction |
| 179 | speed: 2, |
| 180 | |
| 181 | // ball velocity |
| 182 | dx: 0, |
| 183 | dy: 0 |
| 184 | }; |
| 185 | |
| 186 | // check for collision between two objects using axis-aligned bounding box (AABB) |
| 187 | // @see https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection |
| 188 | function collides(obj1, obj2) { |
| 189 | return obj1.x < obj2.x + obj2.width && |
| 190 | obj1.x + obj1.width > obj2.x && |
| 191 | obj1.y < obj2.y + obj2.height && |
| 192 | obj1.y + obj1.height > obj2.y; |
| 193 | } |
| 194 | |
| 195 | // game loop |
| 196 | function loop() { |
| 197 | requestAnimationFrame(loop); |
| 198 | context.clearRect(0, 0, canvas.width, canvas.height); |
| 199 | |
| 200 | // move paddle by it's velocity |
| 201 | paddle.x += paddle.dx; |
| 202 | |
| 203 | // prevent paddle from going through walls |
| 204 | if (paddle.x < wallSize) { |
| 205 | paddle.x = wallSize |
| 206 | } |
| 207 | else if (paddle.x + (brickWidth * 3) > canvas.width - wallSize) { |
| 208 | paddle.x = canvas.width - wallSize - (brickWidth * 3); |
| 209 | } |
| 210 | |
| 211 | // move ball by it's velocity |
| 212 | ball.x += ball.dx; |
| 213 | ball.y += ball.dy; |
| 214 | |
| 215 | // prevent ball from going through walls by changing its velocity |
| 216 | // left & right walls |
| 217 | if (ball.x < wallSize) { |
| 218 | ball.x = wallSize; |
| 219 | ball.dx *= -1; |
| 220 | } |
| 221 | else if (ball.x + ball.width > canvas.width - wallSize) { |
| 222 | ball.x = canvas.width - wallSize - ball.width; |
| 223 | ball.dx *= -1; |
| 224 | } |
| 225 | // top wall |
| 226 | if (ball.y < wallSize) { |
| 227 | ball.y = wallSize; |
| 228 | ball.dy *= -1; |
| 229 | } |
| 230 | |
| 231 | // reset ball if it goes below the screen |
| 232 | if (ball.y > canvas.height) { |
| 233 | ball.x = 130; |
| 234 | ball.y = 260; |
| 235 | ball.dx = 0; |
| 236 | ball.dy = 0; |
| 237 | } |
| 238 | |
| 239 | // check to see if ball collides with paddle. if they do change y velocity |
| 240 | if (collides(ball, paddle)) { |
| 241 | ball.dy *= -1; |
| 242 | |
| 243 | // move ball above the paddle otherwise the collision will happen again |
| 244 | // in the next frame |
| 245 | ball.y = paddle.y - ball.height; |
| 246 | } |
| 247 | |
| 248 | // check to see if ball collides with a brick. if it does, remove the brick |
| 249 | // and change the ball velocity based on the side the brick was hit on |
| 250 | for (let i = 0; i < bricks.length; i++) { |
| 251 | const brick = bricks[i]; |
| 252 | |
| 253 | if (collides(ball, brick)) { |
| 254 | // remove brick from the bricks array |
| 255 | bricks.splice(i, 1); |
| 256 | score++; |
| 257 | document.getElementById("pageDescription").innerText = "Score: " + score; |
| 258 | |
| 259 | // ball is above or below the brick, change y velocity |
| 260 | // account for the balls speed since it will be inside the brick when it |
| 261 | // collides |
| 262 | if (ball.y + ball.height - ball.speed <= brick.y || |
| 263 | ball.y >= brick.y + brick.height - ball.speed) { |
| 264 | ball.dy *= -1; |
| 265 | } |
| 266 | // ball is on either side of the brick, change x velocity |
| 267 | else { |
| 268 | ball.dx *= -1; |
| 269 | } |
| 270 | |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // draw walls |
| 276 | context.fillStyle = getComputedStyle(document.body).getPropertyValue('--elements'); |
| 277 | context.fillRect(0, 0, canvas.width, wallSize); |
| 278 | context.fillRect(0, 0, wallSize, canvas.height); |
| 279 | context.fillRect(canvas.width - wallSize, 0, wallSize, canvas.height); |
| 280 | |
| 281 | // draw ball if it's moving |
| 282 | context.fillStyle = getComputedStyle(document.body).getPropertyValue('--text'); |
| 283 | if (ball.dx || ball.dy) { |
| 284 | context.fillRect(ball.x, ball.y, ball.width, ball.height); |
| 285 | } |
| 286 | |
| 287 | // draw bricks |
| 288 | bricks.forEach(function (brick) { |
| 289 | context.fillStyle = brick.color; |
| 290 | context.fillRect(brick.x, brick.y, brick.width, brick.height); |
| 291 | }); |
| 292 | |
| 293 | // draw paddle |
| 294 | context.fillStyle = getComputedStyle(document.body).getPropertyValue('--theme'); |
| 295 | context.fillRect(paddle.x, paddle.y, paddle.width, paddle.height); |
| 296 | } |
| 297 | |
| 298 | // listen to keyboard events to move the paddle |
| 299 | document.addEventListener('keydown', function (e) { |
| 300 | // left arrow key |
| 301 | if (e.which === 37) { |
| 302 | paddle.dx = -3; |
| 303 | } |
| 304 | // right arrow key |
| 305 | else if (e.which === 39) { |
| 306 | paddle.dx = 3; |
| 307 | } |
| 308 | |
| 309 | // space key |
| 310 | // if they ball is not moving, we can launch the ball using the space key. ball |
| 311 | // will move towards the bottom right to start |
| 312 | if (ball.dx === 0 && ball.dy === 0 && e.which === 32) { |
| 313 | ball.dx = ball.speed; |
| 314 | ball.dy = ball.speed; |
| 315 | } |
| 316 | |
| 317 | if (e.which === 32) { |
| 318 | document.getElementById("game").style.display = ""; |
| 319 | document.getElementById("background").style.display = "none"; |
| 320 | document.getElementById("pageStatus").classList.add("game"); |
| 321 | document.getElementById("pageTitle").innerText = "302"; |
| 322 | document.getElementById("pageDescription").innerText = "Score: " + score; |
| 323 | } |
| 324 | }); |
| 325 | |
| 326 | // listen to keyboard events to stop the paddle if key is released |
| 327 | document.addEventListener('keyup', function (e) { |
| 328 | if (((e.which === 37) && (paddle.dx == -3)) || ((e.which === 39) && (paddle.dx == 3))) { |
| 329 | paddle.dx = 0; |
| 330 | } |
| 331 | }); |
| 332 | |
| 333 | // start the game |
| 334 | requestAnimationFrame(loop); |
| 335 | </script> |
| 336 | </body> |
| 337 | |
| 338 | </html> |