Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Done #243

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ 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 = positionToInteger(DODGER.style.left) + 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;
const rockRightEdge = positionToInteger(rock.style.left) + 20;

if (false /**
if ((rockLeftEdge <= dodgerLeftEdge && rockRightEdge >= dodgerLeftEdge) || (rockLeftEdge >= dodgerLeftEdge && rockRightEdge <= dodgerRightEdge) || (rockLeftEdge <= dodgerRightEdge && rockRightEdge >= dodgerRightEdge) /**
* 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,
Expand Down Expand Up @@ -66,7 +66,7 @@ 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
Expand All @@ -79,20 +79,33 @@ function createRock(x) {
* If a rock collides with the DODGER,
* we should call endGame().
*/


top += 2
rock.style.top = `${top}px`

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.
*/
}

// 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)
Expand All @@ -108,6 +121,9 @@ function createRock(x) {
* Finally, alert "YOU LOSE!" to the player.
*/
function endGame() {
clearInterval(gameInterval)
ROCKS.forEach(rock => rock.remove())
alert("YOU LOSE!")
}

function moveDodger(e) {
Expand All @@ -119,6 +135,17 @@ function moveDodger(e) {
* we've declared for you above.)
* And be sure to use the functions declared below!
*/
if (e.which === 37) {
moveDodgerLeft()
e.preventDefault()
e.stopPropagation()
}

if (e.which === 39) {
moveDodgerRight()
e.preventDefault()
e.stopPropagation()
}
}

function moveDodgerLeft() {
Expand All @@ -127,6 +154,12 @@ function moveDodgerLeft() {
* This function should move DODGER to the left
* (mabye 4 pixels?). Use window.requestAnimationFrame()!
*/
var leftNumbers = DODGER.style.left.replace('px', '')
var left = parseInt(leftNumbers, 10)

if (left > 0) {
DODGER.style.left = `${left - 4}px`
}
}

function moveDodgerRight() {
Expand All @@ -135,8 +168,15 @@ function moveDodgerRight() {
* 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)

if (left < 360) {
DODGER.style.left = `${left + 4}px`
}
}


/**
* @param {string} p The position property
* @returns {number} The position as an integer (without 'px')
Expand Down