-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (80 loc) · 2.65 KB
/
script.js
File metadata and controls
89 lines (80 loc) · 2.65 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let score = 0;
let cross = true;
const gameMusic = new Audio('assets/game-music.mp3');
const gameOver = new Audio('assets/game-over.wav');
const dinoClass = document.querySelector('.dino');
const obstacleClass = document.querySelector('.obstacle');
const gameTitle = document.querySelector('.game-title');
const gameStartTitle = document.querySelector('.game-title p');
const scoreTitle = document.querySelector('.game-score');
setTimeout(() => {
gameMusic.play();
}, 700);
window.addEventListener('keydown', (e) => {
if (gameTitle.innerHTML !== 'Game Over! - Reload to Play again') {
if (e.key === 'ArrowUp' || e.keyCode === 38 || e.code === 'ArrowUp') {
gameStartTitle.textContent = '';
dinoClass.classList.add('dino-animation');
obstacleClass.classList.add('obstacle-animation');
setTimeout(() => {
dinoClass.classList.remove('dino-animation');
}, 700);
}
if (e.key === 'ArrowRight' || e.keyCode === 39 || e.code === 'ArrowRight') {
const dinoX = parseInt(
window.getComputedStyle(dinoClass, null).getPropertyValue('left')
);
dinoClass.style.left = dinoX + 120 + 'px';
}
if (e.key === 'ArrowLeft' || e.keyCode === 37 || e.code === 'ArrowLeft') {
const dinoX = parseInt(
window.getComputedStyle(dinoClass, null).getPropertyValue('left')
);
dinoClass.style.left = dinoX - 120 + 'px';
}
}
});
setInterval(() => {
const dx = parseInt(
window.getComputedStyle(dinoClass, null).getPropertyValue('left')
);
const dy = parseInt(
window.getComputedStyle(dinoClass, null).getPropertyValue('top')
);
const ox = parseInt(
window.getComputedStyle(obstacleClass, null).getPropertyValue('left')
);
const oy = parseInt(
window.getComputedStyle(obstacleClass, null).getPropertyValue('top')
);
const offsetX = Math.abs(dx - ox);
const offsetY = Math.abs(dy - oy);
if (offsetX < 73 && offsetY < 52) {
gameTitle.innerHTML = 'Game Over! - Reload to Play again';
obstacleClass.classList.remove('obstacle-animation');
gameOver.play();
setTimeout(() => {
gameOver.pause();
gameMusic.pause();
}, 1000);
} else if (offsetX < 145 && cross) {
score += 1;
updateScore(score);
cross = false;
setTimeout(() => {
cross = true;
}, 1000);
setTimeout(() => {
const aniDur = parseFloat(
window
.getComputedStyle(obstacleClass, null)
.getPropertyValue('animation-duration')
);
const newDur = aniDur - 0.1;
obstacleClass.style.animationDuration = newDur + 's';
}, 500);
}
}, 10);
function updateScore(score) {
scoreTitle.innerHTML = 'Your Score: ' + score;
}