You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
score = 0;
|
|
cross = true;
|
|
|
|
audio = new Audio('images/music.mp3');
|
|
audiogo = new Audio('images/gameover.mp3');
|
|
|
|
setTimeout(() => {
|
|
audio.play()
|
|
}, 1000);
|
|
|
|
|
|
document.onkeydown = function(e){
|
|
console.log("key code is: ", e.keyCode)
|
|
if(e.keyCode == 38){
|
|
dino = document.querySelector('.dino');
|
|
dino.classList.add('animateDino');
|
|
setTimeout(() => {
|
|
dino.classList.remove('animateDino');
|
|
}, 700);
|
|
}
|
|
|
|
if(e.keyCode == 39){
|
|
dino = document.querySelector('.dino');
|
|
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
|
|
dino.style.left = dinoX + 112 + "px";
|
|
}
|
|
|
|
if(e.keyCode == 37){
|
|
dino = document.querySelector('.dino');
|
|
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
|
|
dino.style.left = (dinoX - 112) + "px";
|
|
}
|
|
}
|
|
|
|
setInterval(() => {
|
|
dino = document.querySelector('.dino');
|
|
gameOver = document.querySelector('.gameOver');
|
|
obstacle = document.querySelector('.obstacle');
|
|
|
|
dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
|
|
dy = parseInt(window.getComputedStyle(dino, null).getPropertyValue('top'));
|
|
|
|
ox = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('left'));
|
|
oy = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('top'));
|
|
|
|
offsetX = Math.abs(dx - ox);
|
|
offsetY = Math.abs(dy - oy);
|
|
|
|
if(offsetX < 73 && offsetY < 52){
|
|
gameOver.innerHTML = "Game over play it again"
|
|
obstacle.classList.remove('obstacleAni')
|
|
audiogo.play();
|
|
setTimeout(() => {
|
|
audiogo.pause();
|
|
audio.pause();
|
|
}, 1000);
|
|
}
|
|
|
|
else if(offsetX < 145 && cross){
|
|
score += 1;
|
|
updateScore(score);
|
|
cross = false;
|
|
setTimeout(() => {
|
|
cross = true;
|
|
}, 1000);
|
|
setTimeout(() => {
|
|
aniDur = parseFloat(window.getComputedStyle(obstacle, null).getPropertyValue('animation-duration'));
|
|
newDur = aniDur - 0.1;
|
|
obstacle.style.animationDuration - newDur + 's';
|
|
console.log('New Animation Duration', newDur);
|
|
}, 500);
|
|
}
|
|
}, 10);
|
|
|
|
function updateScore(score){
|
|
scoreCard.innerHTML = "Your Score:" + score;
|
|
} |