-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (60 loc) · 2.34 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
63
64
65
66
67
68
69
70
71
72
73
var Score = 0;
var cross = true;
var audio = new Audio('gameAudio.mp3');
audio.addEventListener('canplaythrough', function() {
audio.play().catch(function(error) {
console.error('Audio playback failed:', error);
});
});
setTimeout(() => {
audio.play().catch(function(error) {
console.error('Audio playback failed:', error);
});
}, 1000);
document.onkeydown = function (e) {
console.log("key code is ", e.keyCode);
var thief = document.querySelector('.thief');
var thiefX = parseInt(window.getComputedStyle(thief, null).getPropertyValue('left'));
if (e.keyCode == 38 || e.keyCode == 32) {
thief.classList.add('animatethief');
setTimeout(() => {
thief.classList.remove('animatethief');
}, 700);
} else if (e.keyCode == 39) {
thiefX += 112;
thief.style.left = thiefX + "px";
} else if (e.keyCode == 37) {
thiefX -= 112;
thief.style.left = thiefX + "px";
}
var gameover = document.querySelector('.gameOver');
var obstacle = document.querySelector('.police');
var dx = window.getComputedStyle(thief, null).getPropertyValue('left');
var dy = window.getComputedStyle(thief, null).getPropertyValue('top');
var ox = window.getComputedStyle(obstacle, null).getPropertyValue('left');
var oy = window.getComputedStyle(obstacle, null).getPropertyValue('top');
var offsetX = Math.abs(parseFloat(dx) - parseFloat(ox));
var offsetY = Math.abs(parseFloat(dy) - parseFloat(oy));
if (offsetX < 73 && offsetY < 52) {
gameover.innerHTML = "Game Over - Reload To Start";
gameover.style.visibility = 'visible';
obstacle.classList.remove('policeAni');
} else if (offsetX < 145 && cross) {
Score += 1;
updateScore(Score);
cross = false;
setTimeout(() => {
cross = true;
}, 1000);
setTimeout(() => {
cross = true;
}, 1000);
aniDur = parseFloat(window.getComputedStyle(obstacle, null).getPropertyValue('left'));
newDur = aniDur - 0.1;
police.style.animationDuration = newDur + 's';
}
};
function updateScore(score) {
var scoreCount = document.getElementById('scoreCount');
scoreCount.innerHTML = "Your Score: " + score * 100;
}