-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
62 lines (54 loc) · 1.99 KB
/
script.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
score = 0;
cross = true;
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);
// console.log(offsetx, offsety)
if (offsetx < 73 && offsety < 52) {
gameover.style.visibility = 'visible';
obstacle.classList.remove('obstacleAni')
}
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';
}, 500);
}
}, 10);
function updatescore(score) {
scorecont.innerHTML = "Your score: " + score
}