-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
143 lines (116 loc) · 4.15 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
document.addEventListener("DOMContentLoaded", () => {
// selecting required elements..
const bird = document.querySelector(".bird");
const gameDisplay = document.querySelector(".game-container");
const ground = document.querySelector(".ground");
var scoreTrack = document.createElement("div");
scoreTrack.classList.add("score-display");
let score = 0;
function increaseScore() {
score++;
scoreTrack.innerHTML = "Score: " + score;
}
// Insert the score display at the top of the game container
gameDisplay.appendChild(scoreTrack);
let scoreTimer;
let birdLeft = 220;
let birdBottom = 450;
let gravity = 2;
let isGameOver = false;
function startGame() {
score = 0; // Reset score when starting the game
birdBottom = 450; // Reset bird position
scoreTrack.innerHTML = "Score: " + score; // Reset score display
isGameOver = false; // Reset game over state
scoreTimer = setInterval(increaseScore, 1250); // Start score timer
gameTimerId = setInterval(moveBird, 20); // Start game loop
generateObstacle(); // Start generating obstacles
}
function moveBird() {
birdBottom -= gravity;
bird.style.bottom = birdBottom + "px";
bird.style.left = birdLeft + "px";
}
function jump() {
if (birdBottom < 600 && birdBottom >= 153) {
birdBottom += 40;
}
bird.style.bottom = birdBottom + "px";
}
document.addEventListener("keyup", jump);
document.addEventListener("touchstart", jump);
function generateObstacle() {
let timerId2 = setInterval(moveObstacle, 10);
let generationTimerId = setTimeout(generateObstacle, 1250);
if (isGameOver) {
clearTimeout(generationTimerId);
clearInterval(timerId2);
return;
}
let obstacleLeft = 500;
let randomHeight = Math.random() * 60;
let obstacleBottom = randomHeight + 30;
const obstacle = document.createElement("div");
const upperobstacle = document.createElement("div");
upperobstacle.classList.add("upperobstacle");
obstacle.classList.add("obstacle");
gameDisplay.appendChild(obstacle);
gameDisplay.appendChild(upperobstacle);
obstacle.style.left = obstacleLeft + "px";
obstacle.style.bottom = obstacleBottom + "px";
upperobstacle.style.left = obstacleLeft + "px";
let upperobstacle_bottom = obstacleBottom + 300 + Math.random() * 100 + 140;
upperobstacle.style.bottom = upperobstacle_bottom + "px";
function moveObstacle() {
obstacleLeft -= 2;
obstacle.style.left = obstacleLeft + "px";
upperobstacle.style.left = obstacleLeft + "px";
if (obstacleLeft <= -5) {
clearInterval(timerId2);
gameDisplay.removeChild(obstacle);
gameDisplay.removeChild(upperobstacle);
}
if (
birdBottom <= 150 ||
(((obstacleLeft > 220 && obstacleLeft < 270) ||
(obstacleLeft + 26 > 220 && obstacleLeft + 26 < 270)) &&
(obstacleBottom + 300 > birdBottom ||
upperobstacle_bottom <= birdBottom + 48))
) {
clearInterval(timerId2);
gameOver();
}
}
}
function overDisplay() {
let button = document.createElement("button");
let restart = document.createElement("div");
restart.classList.add("overDisplay");
button.classList.add("restart-btn");
button.innerHTML = "Restart Game";
function onButtonClick() {
window.location.reload();
}
button.addEventListener("click", onButtonClick);
gameDisplay.appendChild(restart);
gameDisplay.appendChild(button);
restart.innerHTML = "Final Score: " + score; // Display final score
}
function gameOver() {
clearInterval(gameTimerId);
clearInterval(scoreTimer);
overDisplay();
isGameOver = true;
document.removeEventListener("keyup", jump);
}
// Create and add the start button
const startButton = document.createElement("button");
startButton.innerHTML = "Start Game";
startButton.classList.add("start-btn");
document.body.insertBefore(startButton, gameDisplay);
// Start the game when the start button is clicked
startButton.addEventListener("click", () => {
startButton.style.display = "none";
startGame(); // Start the game
});
});