diff --git a/index.js b/index.js index 19bad7092f..9d534d2d10 100644 --- a/index.js +++ b/index.js @@ -29,14 +29,13 @@ function checkCollision(rock) { 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) // FIXME: The rock is 20 pixel's wide -- how do we get the right edge? - const rockRightEdge = 0; - - if (false /** + const rockRightEdge = rockLeftEdge+ 20; + /** * 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, @@ -45,7 +44,10 @@ function checkCollision(rock) { * 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. - */) { + */ + if ((rockLeftEdge <= dodgerLeftEdge && rockRightEdge >= dodgerLeftEdge) || + (rockLeftEdge >= dodgerLeftEdge && rockRightEdge <= dodgerRightEdge) || + (rockLeftEdge <= dodgerRightEdge && rockRightEdge >= dodgerRightEdge)) { return true } } @@ -66,25 +68,35 @@ function createRock(x) { * Now that we have a rock, we'll need to append * it to GAME and move it downwards. */ - + GAME.appendChild(rock); /** * This function moves the rock. (2 pixels at a time * seems like a good pace.) */ function moveRock() { + rock.style.top = `${top += 2}px` // 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. */ - + if (top < GAME_HEIGHT) + { + window.requestAnimationFrame(moveRock) + }else + { + rock.remove() + } /** * But if the rock *has* reached the bottom of the GAME, * we should remove the rock from the DOM. @@ -92,7 +104,7 @@ function createRock(x) { } // 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) @@ -108,6 +120,12 @@ function createRock(x) { * Finally, alert "YOU LOSE!" to the player. */ function endGame() { + clearInterval(gameInterval) + ROCKS.forEach(function(rock){ rock.remove()}) + document.removeEventListener('keydown', moveDodger); + var e = alert("YOU LOSE!") + return e.close() + } function moveDodger(e) { @@ -119,6 +137,21 @@ function moveDodger(e) { * we've declared for you above.) * And be sure to use the functions declared below! */ + //Test codes asks for propagation , defaults, so checked for either input and inputted + if(e.which === LEFT_ARROW || e.which === RIGHT_ARROW){ + e.preventDefault() + e.stopPropagation() + } + if(e.which === LEFT_ARROW){ + + moveDodgerLeft() + } + + if(e.which === RIGHT_ARROW){ + + moveDodgerRight() + } + } function moveDodgerLeft() { @@ -127,6 +160,15 @@ function moveDodgerLeft() { * This function should move DODGER to the left * (mabye 4 pixels?). Use window.requestAnimationFrame()! */ + window.requestAnimationFrame(function() { + var left = positionToInteger(dodger.style.left) + + if(left > 0) + { + dodger.style.left = `${left - 4}px` + } + }) + } function moveDodgerRight() { @@ -135,6 +177,13 @@ function moveDodgerRight() { * This function should move DODGER to the right * (mabye 4 pixels?). Use window.requestAnimationFrame()! */ + window.requestAnimationFrame(function(){ + var right = positionToInteger(dodger.style.left) + if (right < 360) + { + dodger.style.left= `${right + 4}px` + } + }) } /**