From 89c04e9fa0704efcaf4abba1f5117b33ca41f7e8 Mon Sep 17 00:00:00 2001 From: christian Date: Sat, 12 Sep 2020 03:51:07 +0000 Subject: [PATCH] Done. --- index.js | 152 +++++++++++++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 90 deletions(-) diff --git a/index.js b/index.js index 19bad7092f..fb47b51b0e 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,3 @@ -/** - * Don't change these constants! - */ const DODGER = document.getElementById('dodger') const GAME = document.getElementById('game') const GAME_HEIGHT = 400 @@ -12,135 +9,110 @@ const START = document.getElementById('start') var gameInterval = null -/** - * Be aware of what's above this line, - * but all of your work should happen below. - */ - function checkCollision(rock) { - // implement me! - // use the comments below to guide you! - const top = positionToInteger(rock.style.top) - - // rocks are 20px high - // DODGER is 20px high - // GAME_HEIGHT - 20 - 20 = 360px; + + const top = positionToInteger(rock.style.top); if (top > 360) { + const dodgerLeftEdge = positionToInteger(DODGER.style.left) - - // FIXME: The DODGER is 40 pixels wide -- how do we get the right edge? - const dodgerRightEdge = 0; - + const dodgerRightEdge = dodgerLeftEdge + 40; const rockLeftEdge = positionToInteger(rock.style.left) + const rockRightEdge = rockLeftEdge + 20; - // FIXME: The rock is 20 pixel's wide -- how do we get the right edge? - const rockRightEdge = 0; - - if (false /** - * Think about it -- what's happening here? - * There's been a collision if one of three things is true: - * 1. The rock's left edge is < the DODGER's left edge, - * and the rock's right edge is > the DODGER's left edge; - * 2. The rock's left edge is > the DODGER's left edge, - * and the rock's right edge is < the DODGER's right edge; - * 3. The rock's left edge is < the DODGER's right edge, - * and the rock's right edge is > the DODGER's right edge. - */) { - return true - } + if ( ((rockLeftEdge <= dodgerLeftEdge) && (rockRightEdge >= dodgerLeftEdge)) || ((rockLeftEdge >= dodgerLeftEdge) && (rockRightEdge <= dodgerRightEdge)) || ((rockLeftEdge <= dodgerRightEdge) && (rockRightEdge >= dodgerRightEdge)) ) return true; + return false; + } } function createRock(x) { const rock = document.createElement('div') - - rock.className = 'rock' - rock.style.left = `${x}px` + + rock.className = 'rock'; + rock.style.left = `${x}px`; // Hmmm, why would we have used `var` here? - var top = 0 + var top = 0; - rock.style.top = top - - /** - * Now that we have a rock, we'll need to append - * it to GAME and move it downwards. - */ + rock.style.top = top; + GAME.appendChild(rock); /** * This function moves the rock. (2 pixels at a time * seems like a good pace.) */ function moveRock() { - // implement me! - // (use the comments below to guide you!) - /** - * If a rock collides with the DODGER, - * we should call endGame(). - */ + + if (checkCollision(rock)) endGame(); /** * Otherwise, if the rock hasn't reached the bottom of * the GAME, we want to move it again. */ - - /** - * But if the rock *has* reached the bottom of the GAME, - * we should remove the rock from the DOM. - */ + + if (top < 380){ + rock.style.top = `${top += 2}px`; + window.requestAnimationFrame(moveRock); + } + + else rock.remove(); + } // We should kick off the animation of the rock around here. + window.requestAnimationFrame(moveRock); - // Add the rock to ROCKS so that we can remove all rocks - // when there's a collision. - ROCKS.push(rock) - - // Finally, return the rock element you've created. - return rock + ROCKS.push(rock); + return rock; } -/** - * End the game by clearing `gameInterval`, - * removing all ROCKS from the DOM, - * and removing the `moveDodger` event listener. - * Finally, alert "YOU LOSE!" to the player. - */ function endGame() { + clearInterval(gameInterval); + for (let i=0; i 0) { + window.requestAnimationFrame(step); + } } function moveDodgerRight() { - // implement me! - /** - * This function should move DODGER to the right - * (mabye 4 pixels?). Use window.requestAnimationFrame()! - */ + var leftNumbers = dodger.style.left.replace('px', '') + var left = parseInt(leftNumbers, 10) + + function step() { + dodger.style.left = `${left + 4}px` + } + + if (left < 360 ) { + window.requestAnimationFrame(step); + } } -/** - * @param {string} p The position property - * @returns {number} The position as an integer (without 'px') - */ function positionToInteger(p) { return parseInt(p.split('px')[0]) || 0 } @@ -151,6 +123,6 @@ function start() { START.style.display = 'none' gameInterval = setInterval(function() { - createRock(Math.floor(Math.random() * (GAME_WIDTH - 20))) + createRock(Math.floor(Math.random() * (GAME_WIDTH - 20))) }, 1000) }