Skip to content

Instantly share code, notes, and snippets.

@stabgan
Created June 14, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stabgan/e7e4377808a9bd28a36e1af25f39d1b2 to your computer and use it in GitHub Desktop.
Save stabgan/e7e4377808a9bd28a36e1af25f39d1b2 to your computer and use it in GitHub Desktop.
Javascript dodger browser game
const DODGER = document.getElementById('dodger')
const GAME = document.getElementById('game')
const GAME_HEIGHT = 400
const GAME_WIDTH = 400
const LEFT_ARROW = 37 // use e.which!
const RIGHT_ARROW = 39 // use e.which!
const START = document.getElementById('start')
const ROCKS = []
var SCORE = 0
var gameInterval = null
function checkCollision(rock) {
const top = positionToInteger(rock.style.top)
// rocks are 20px high
// DODGER is 20px high
// GAME_HEIGHT - 20 - 20 = 360px;
if (top > 360) {
const dodgerLeftEdge = positionToInteger(DODGER.style.left)
const dodgerRightEdge = dodgerLeftEdge + 40;
const rockLeftEdge = positionToInteger(rock.style.left)
const rockRightEdge = rockLeftEdge + 20;
return (
(rockLeftEdge <= dodgerLeftEdge && rockRightEdge >= dodgerLeftEdge) ||
(rockLeftEdge >= dodgerLeftEdge && rockRightEdge <= dodgerRightEdge) ||
(rockLeftEdge <= dodgerRightEdge && rockRightEdge >= dodgerRightEdge)
)
}
}
function checkScore(rock) {
const top = positionToInteger(rock.style.top)
if (top > 360) {
return true
}
return false
}
function createRock(x) {
const rock = document.createElement('div')
rock.className = 'rock'
rock.style.left = `${x}px`
var top = rock.style.top = 0
GAME.appendChild(rock)
function moveRock() {
rock.style.top = `${top += 2}px`;
if (checkCollision(rock)) {
return endGame()
}
if (checkScore(rock)){
SCORE += 1
}
if (top < GAME_HEIGHT) {
window.requestAnimationFrame(moveRock)
} else {
rock.remove()
}
}
window.requestAnimationFrame(moveRock)
ROCKS.push(rock)
return rock
}
function endGame() {
clearInterval(gameInterval)
let l = ROCKS.length
for(var i=0 ; i < l ; i++)
{
ROCKS[i].remove();
}
document.removeEventListener('keydown', moveDodger)
START.innerHTML = `SCORE: ${SCORE}\nPlay again?`
START.style.display = 'inline'
SCORE = 0
return alert('YOU LOSE!')
}
function moveDodger(e) {
const code = e.which
if ([LEFT_ARROW, RIGHT_ARROW].indexOf(code) > -1) {
e.preventDefault()
e.stopPropagation()
}
if (code === LEFT_ARROW) {
moveDodgerLeft()
} else if (code === RIGHT_ARROW) {
moveDodgerRight()
}
}
function moveDodgerLeft() {
window.requestAnimationFrame(function() {
const left = positionToInteger(DODGER.style.left)
if (left > 0) {
DODGER.style.left = `${left - 10}px`;
}
})
}
function moveDodgerRight() {
window.requestAnimationFrame(function() {
const left = positionToInteger(DODGER.style.left)
if (left < 360) {
DODGER.style.left = `${left + 10}px`;
}
})
}
function positionToInteger(p) {
return parseInt(p.split('px')[0]) || 0
}
function start() {
document.addEventListener('keydown', moveDodger)
START.style.display = `none`
gameInterval = setInterval(function() {
createRock(Math.floor(Math.random() * (GAME_WIDTH - 20)))
}, 1000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment