-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (65 loc) · 2.05 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
const barElem = document.getElementById("bar"),
gapElem = document.getElementById("gap"),
birdElem = document.querySelector("#bird img"),
scoreElem = document.getElementById("score"),
loseAudio = document.getElementById("lose"),
loseAlert = document.getElementById("lose-alert"),
loseAlertBg = document.getElementById("lose-alert-bg");
let score = 0;
// Generate Random Place of Gap
const generateRandomGap = () => {
barElem.addEventListener("animationiteration", () => {
let randomTop = Math.random() * (600 - 190);
gapElem.style.top = randomTop + "px";
score++;
scoreElem.innerHTML = score;
});
};
generateRandomGap();
// Bird Gravity
let isJumping = false;
setInterval(() => {
let birdTop = parseInt(getComputedStyle(birdElem).getPropertyValue("top"));
if (!isJumping) {
if (birdTop < 800) {
birdElem.style.top = birdTop + 3 + "px";
}
}
}, 10);
// Bird Jump
document.addEventListener("click", (e) => {
isJumping = true;
let jumpCount = 0;
let jumpInterval = setInterval(() => {
jumpCount++;
let birdTop = parseInt(getComputedStyle(birdElem).getPropertyValue("top"));
if (birdTop > 0 && jumpCount < 15) {
birdElem.style.top = birdTop - 5 + "px";
}
if (jumpCount > 20) {
clearInterval(jumpInterval);
isJumping = false;
jumpCount = 0;
}
}, 10);
loseGame();
});
//Game Over
const loseGame = () => {
let barLeft = parseInt(getComputedStyle(barElem).getPropertyValue("left"));
let gapTop = parseInt(getComputedStyle(gapElem).getPropertyValue("top"));
let birdTop = parseInt(getComputedStyle(birdElem).getPropertyValue("top"));
if (barLeft < 30 && (birdTop > gapTop + 190 || birdTop < gapTop)) {
loseAudio.play();
loseAlert.style.display = "block";
loseAlertBg.style.display = "block";
loseAlert.innerHTML = `You Lose ! Your Score is : ${score}`;
setTimeout(() => {
loseAlert.style.display = "none";
loseAlertBg.style.display = "none";
}, 2000);
birdElem.style.top = 40 + "px";
barElem.style.left = 500 + "%";
score = 0;
}
};