-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
333 lines (268 loc) · 7.58 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const buttonStart = document.getElementById("startGameButton");
const gameInstructions = document.getElementById("gameInstructions");
const instruction = document.getElementById("instruction");
const message = document.getElementById("message");
const finalScore = document.getElementById("final-score");
const rules = [
"1. To win you have to avoid all the obstacles: stones, trees and flowers.",
"2. If you collide with any of the obstacles, you will automatically lose and lose your score.",
"3. To jump press the 'up', 'enter' or 'space' key.",
"4. Good luck and have fun with Jumping game!",
];
const gameContainer = document.getElementById("gameContainer");
const ambience = document.getElementById("ambience");
const player = document.getElementById("player");
let score = 0;
let scoreDisplay = document.getElementById("score");
let gameOver = false;
function addInstructions() {
for (let i = 0; i < 4; i++) {
const instruction1 = document.createElement("p");
instruction1.textContent = rules[i];
instruction.appendChild(instruction1);
}
}
function openInstructions() {
gameInstructions.style.display = "block";
}
function closeInstructions() {
gameInstructions.style.display = "none";
}
addInstructions();
let backgroundMusic = new Audio("./sounds/music.mp3");
backgroundMusic.loop = true;
const playAudio = () => backgroundMusic.play();
const stopAudio = () => backgroundMusic.pause();
const clouds = [
{ name: "big-cloud", width: 15 },
{ name: "medium-cloud", width: 10 },
{ name: "small-cloud", width: 7 },
];
const getRandomCloud = () => {
const randomIndex = Math.floor(Math.random() * clouds.length);
return clouds[randomIndex];
};
let cloudTimer;
function createCloud() {
if (!gameOver) {
const newCloud = document.createElement("image");
const cloudDetail = getRandomCloud();
const cloudStyle = cloudDetail.name;
const cloudWidth = cloudDetail.width;
newCloud.classList.add("cloud", cloudStyle);
ambience.appendChild(newCloud);
let cloudPosition = 50;
newCloud.style.left = cloudPosition + "vw";
cloudTimer = setInterval(() => {
if (!gameOver) {
cloudPosition -= 0.1;
newCloud.style.left = cloudPosition + "vw";
}
if (cloudPosition < -cloudWidth) {
newCloud.remove();
}
}, 10);
}
}
let cloudTimeOut;
function handleEnvironment() {
if (!gameOver) {
createCloud();
cloudTimeOut = setTimeout(handleEnvironment, Math.random() * 4000);
}
}
const clearClouds = () => {
const cloudElements = document.querySelectorAll(".cloud");
cloudElements.forEach(function (cloudElement) {
cloudElement.remove();
});
};
const debounce = (func, wait, immediate) => {
let timeout;
return function () {
let context = this;
let args = arguments;
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
}, wait);
if (callNow) func.apply(context, args);
};
};
const increaseScore = () => {
score++;
scoreDisplay.innerText = score.toString().padStart(3, "0");
console.log(score);
};
const scorePlus = debounce(increaseScore, 1000, true);
const resetScore = () => {
score = 0;
scoreDisplay.innerText = score.toString().padStart(3, "0");
};
const obstacles = ["rock", "tree", "flower"];
let obstacleTimer;
const getRandomObstacle = () => {
const randomIndex = Math.floor(Math.random() * obstacles.length);
return obstacles[randomIndex];
};
const createObstacle = () => {
let obstacle = document.createElement("img");
if (!gameOver) {
let obstacleDetail = getRandomObstacle();
obstacle.classList.add("obstacle", obstacleDetail);
gameContainer.appendChild(obstacle);
let obstaclePosition = 50;
obstacle.style.left = obstaclePosition + "vw";
obstacleTimer = setInterval(() => {
if (obstaclePosition < -50) {
clearInterval(obstacleTimer);
obstacle.remove();
}
if (
obstaclePosition > 13 &&
obstaclePosition < 17 &&
player.classList.contains("jump-up")
) {
scorePlus();
} else if (
obstaclePosition > 13 &&
obstaclePosition < 17 &&
!player.classList.contains("jump-up")
) {
handleGameOver();
}
obstaclePosition -= 0.5;
obstacle.style.left = obstaclePosition + "vw";
}, 10);
}
};
let obstacleTimeout;
const startObstacles = () => {
if (!gameOver) {
createObstacle();
obstacleTimeout = setTimeout(startObstacles, Math.random() * 4000 + 2000);
}
};
const clearObstacles = () => {
const obstacleElements = document.querySelectorAll(".obstacle");
obstacleElements.forEach(function (obstacleElement) {
obstacleElement.remove();
});
};
let playerRunInterval;
const playerRun = () => {
let i = 0;
playerRunInterval = setInterval(() => {
if (i == 9) {
player.classList.remove(`run${i}`);
i = 1;
} else {
i++;
}
player.classList.add(`run${i}`);
if (i > 1) {
player.classList.remove(`run${i - 1}`);
}
}, 50);
};
const barkEffect = new Audio("./sounds/dog-bark.wav");
const jump = () => {
if (!gameOver) {
barkEffect.play();
player.classList.add("jump-up");
setTimeout(() => {
player.classList.remove("jump-up");
player.classList.add("jump-down");
setTimeout(() => {
player.classList.remove("jump-down");
}, 300);
}, 300);
}
};
const jumpKey = (event) => {
switch (event.keyCode) {
case 13:
case 32:
case 38:
case 87:
jump();
break;
}
};
document.addEventListener("keydown", jumpKey);
const gameOverContainer = document.querySelector(".game-over-container");
const restartBtn = document.getElementById("restart_btn");
const quitBtn = document.getElementById("quit_btn");
const quitGame = document.getElementById("quit_game");
const stopGame = () => {
stopAudio();
clearInterval(cloudTimer);
clearInterval(obstacleTimer);
clearInterval(playerRunInterval);
clearTimeout(startGeneratingObstacles);
clearTimeout(obstacleTimeout);
clearTimeout(cloudTimeOut);
};
const clearGame = () => {
resetScore();
clearObstacles();
clearClouds();
hiddenGameOver();
gameOver = false;
};
const gameRestart = () => {
clearGame();
game();
};
const gameQuit = () => {
clearGame();
openInstructions();
};
const showGamePause = () => {
clearGame();
stopGame();
gameOverContainer.classList.remove("hidden");
message.innerText = "GAME PAUSE";
quitBtn.addEventListener("click", gameQuit);
restartBtn.addEventListener("click", gameRestart);
};
const showGameOver = () => {
gameOverContainer.classList.remove("hidden");
message.innerText = "GAME OVER";
finalScore.classList.remove("hidden");
finalScore.innerText = `Final Score: ${score}`;
quitBtn.addEventListener("click", gameQuit);
restartBtn.addEventListener("click", gameRestart);
};
const hiddenGameOver = () => {
gameOverContainer.classList.add("hidden");
};
document.addEventListener("keydown", function (event) {
if (event.key === "Escape" || event.key === "Esc") {
showGamePause();
}
});
quitGame.addEventListener("click", showGamePause);
let startGeneratingObstacles;
const game = () => {
playAudio();
playerRun();
handleEnvironment();
startGeneratingObstacles = setTimeout(startObstacles, 4000);
};
const howl = new Audio("./sounds/howleffect.mp3");
const handleGameOver = () => {
howl.play();
stopGame();
gameOver = true;
showGameOver();
};
buttonStart.addEventListener("click", function (e) {
closeInstructions();
game();
});
openInstructions();