-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (65 loc) · 2.55 KB
/
Copy pathscript.js
File metadata and controls
73 lines (65 loc) · 2.55 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
score = 0;
cross = true;
audio = new Audio('music/music.mp3');
audiogo = new Audio('music/gameover.mp3');
setTimeout(() => {
audio.play()
}, 1000);
document.onkeydown = function (e) {
console.log("Key code is: ", e.keyCode)
if (e.keyCode == 38) { // For Up key
doraemon = document.querySelector('.doraemon');
doraemon.classList.add('animateDoraemon');
setTimeout(() => {
doraemon.classList.remove('animateDoraemon')
}, 700);
}
if (e.keyCode == 39) { // For lefy key
doraemon = document.querySelector('.doraemon');
doraemonX = parseInt(window.getComputedStyle(doraemon, null).getPropertyValue('left'));
doraemon.style.left = doraemonX + 112 + "px";
}
if (e.keyCode == 37) { // For Right key
doraemon = document.querySelector('.doraemon');
doraemonX = parseInt(window.getComputedStyle(doraemon, null).getPropertyValue('left'));
doraemon.style.left = (doraemonX - 112) + "px";
}
}
setInterval(() => {
doraemon = document.querySelector('.doraemon');
gameOver = document.querySelector('.gameOver');
obstacle = document.querySelector('.obstacle');
dx = parseInt(window.getComputedStyle(doraemon, null).getPropertyValue('left'));
dy = parseInt(window.getComputedStyle(doraemon, 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 < 88 && offsetY < 99) {
gameOver.innerHTML = "Game Over - Reload to Play Again"
obstacle.classList.remove('obstacleAni')
audiogo.play();
setTimeout(() => {
audiogo.pause(); // Music Paused When game is Over
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) {
scoreCont.innerHTML = "Your Score: " + score
}