-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (40 loc) · 1.62 KB
/
script.js
File metadata and controls
48 lines (40 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
score = 0;
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);
} else if (e.keyCode == 39) {
dino = document.querySelector('.dino')
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
dino.style.left = (dinoX + 112) + "px"
} else if (e.keyCode == 37) {
dino = document.querySelector('.dino')
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
dino.style.left = (dinoX - 112) + "px"
}
}
setInterval(() => {
let dino = document.querySelector('.dino')
let gameover = document.querySelector('.gameOver')
let obstacle = document.querySelector('.obstacle')
let dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'))
let dy = parseInt(window.getComputedStyle(dino, null).getPropertyValue('top'))
let ox = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('left'))
let oy = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('top'))
let offsetX = Math.abs(dx - ox);
let offsetY = Math.abs(dy - oy);
if (offsetX < 93 && offsetY < 52) {
gameover.style.visibility = 'visible'
obstacle.classList.remove('obstaceAni')
} else {
score ++;
scoreCont.innerHtml = "Your Score: " + score
}
}, 100);
function updateScore(score) {
scoreCont.innerHtml = "Your Score: " + score
}