-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
343 lines (289 loc) · 12.6 KB
/
Copy pathscript.js
File metadata and controls
343 lines (289 loc) · 12.6 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
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
334
335
336
337
338
339
340
341
342
343
// DOM Elements
const homeInterface = document.getElementById("home-interface");
const gameOverScreen = document.getElementById("game-over-screen");
const gameContainer = document.getElementById("game-container");
const startGameButton = document.getElementById("start-game-button");
const returnHomeButton = document.getElementById("return-home-button");
const restartGameButton = document.getElementById("restart-game-button");
const pauseButton = document.getElementById("pause-button");
const playButton = document.getElementById("play-button");
const gameArea = document.getElementById("game-area");
const chameleon = document.getElementById("chameleon");
const scoreElement = document.getElementById("score");
const livesElement = document.getElementById("lives");
const finalScoreElement = document.getElementById("final-score");
// High Score Elements
const scoreList = document.getElementById("score-list");
const returnToHomeButton = document.getElementById("return-to-home-button");
// Game Variables
let score = 0;
let lives = 3;
let fallingSpeed = 2000;
let isGameRunning = false;
let isPaused = false;
let chameleonSpeed = 10;
let keys = {};
let gameInterval;
let movementInterval;
let fallingItems = []; // Track falling items
// Array to store top 5 scores
let topScores = JSON.parse(localStorage.getItem("topScores")) || [];
// Start Game
startGameButton.addEventListener("click", () => {
homeInterface.style.display = "none";
gameContainer.style.display = "block";
resetGame();
isGameRunning = true;
startFallingItems(); // Start falling items
enableChameleonMovement(); // Enable chameleon movement
document.body.classList.add("no-scroll"); // Disable scrolling and make the page static
pauseButton.disabled = false; // Enable the pause button when the game starts
});
// Return to Home
returnHomeButton.addEventListener("click", () => {
gameOverScreen.style.display = "none";
homeInterface.style.display = "block";
document.body.classList.remove("no-scroll"); // Enable scrolling when returning to home
});
// Restart Game
restartGameButton.addEventListener("click", () => {
resetGame();
restartGameButton.style.display = "none";
pauseButton.disabled = false; // Enable the pause button when the game restarts
isGameRunning = true;
startFallingItems(); // Start falling items
enableChameleonMovement(); // Enable chameleon movement
document.body.classList.add("no-scroll"); // Disable scrolling during gameplay
});
// Pause Game
pauseButton.addEventListener("click", () => {
if (isPaused) return; // Prevent the pause button from being clicked if it's already paused
isPaused = true;
pauseButton.disabled = true; // Disable the pause button to prevent re-clicking while paused
playButton.style.display = "inline-block";
clearInterval(gameInterval); // Stop falling items
clearInterval(movementInterval); // Stop chameleon movement
// Stop all falling item intervals (already falling items will stop)
fallingItems.forEach(item => {
clearInterval(item.interval); // Stop each falling item's interval
});
document.body.classList.remove("no-scroll"); // Enable scrolling when paused
});
// Resume Game
playButton.addEventListener("click", () => {
isPaused = false;
playButton.style.display = "none";
pauseButton.disabled = false; // Enable the pause button when the game is resumed
pauseButton.style.display = "inline-block"; // Ensure pause button is visible again
document.body.classList.add("no-scroll"); // Disable scrolling during gameplay
// Resume falling items
fallingItems.forEach(item => {
if (item.interval) {
item.interval = setInterval(() => {
item.element.style.top = `${item.element.offsetTop + 5}px`;
const itemRect = item.element.getBoundingClientRect();
const chameleonRect = chameleon.getBoundingClientRect();
// Collision Detection
if (
itemRect.bottom > chameleonRect.top &&
itemRect.left < chameleonRect.right &&
itemRect.right > chameleonRect.left
) {
score++;
scoreElement.textContent = score;
item.element.remove();
clearInterval(item.interval);
// Increase speed at score milestones
if (score % 30 === 0) {
fallingSpeed = Math.max(500, fallingSpeed - 500);
clearInterval(gameInterval);
startFallingItems();
}
}
// Remove item if it falls past the game area
if (item.element.offsetTop > gameArea.offsetHeight) {
lives--;
livesElement.textContent = lives;
item.element.remove();
clearInterval(item.interval);
if (lives <= 0) {
endGame();
}
}
}, 50); // Resume the falling interval
}
});
startFallingItems(); // Continue spawning new falling items
enableChameleonMovement(); // Resume chameleon movement
});
// Reset Game
function resetGame() {
score = 0;
lives = 3;
fallingSpeed = 2000; // Reset falling speed
isGameRunning = false;
isPaused = false;
clearInterval(gameInterval);
clearInterval(movementInterval);
scoreElement.textContent = score;
livesElement.textContent = lives;
// Clear falling items
const items = document.querySelectorAll(".falling-item");
items.forEach((item) => item.remove());
// Reset chameleon position
chameleon.style.left = "50%";
fallingItems = []; // Reset the falling items tracker
}
// Start Falling Items
function startFallingItems() {
if (isPaused || !isGameRunning) return; // Prevent starting if the game is paused
gameInterval = setInterval(() => {
if (isPaused) return; // Prevent falling items if paused
const item = document.createElement("div");
item.classList.add("falling-item");
item.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
item.style.top = "0px";
gameArea.appendChild(item);
const itemData = {
element: item,
interval: setInterval(() => {
item.style.top = `${item.offsetTop + 5}px`;
const itemRect = item.getBoundingClientRect();
const chameleonRect = chameleon.getBoundingClientRect();
// Collision Detection
if (
itemRect.bottom > chameleonRect.top &&
itemRect.left < chameleonRect.right &&
itemRect.right > chameleonRect.left
) {
score++;
scoreElement.textContent = score;
item.remove();
clearInterval(itemData.interval);
// Increase speed at score milestones
if (score % 30 === 0) {
fallingSpeed = Math.max(500, fallingSpeed - 500);
clearInterval(gameInterval);
startFallingItems();
}
}
// Remove item if it falls past the game area
if (item.offsetTop > gameArea.offsetHeight) {
lives--;
livesElement.textContent = lives;
item.remove();
clearInterval(itemData.interval);
if (lives <= 0) {
endGame();
}
}
}, 50),
};
// Store the interval for future reference
fallingItems.push(itemData);
}, fallingSpeed);
}
// Reduce container size
gameContainer.style.width = "60%";
gameContainer.style.height = "60vh";
gameContainer.style.margin = "auto";
gameArea.style.width = "100%";
gameArea.style.height = "100%";
gameArea.style.position = "relative";
gameArea.style.backgroundSize = "cover";
gameArea.style.backgroundPosition = "center";
chameleon.style.margin = "5px";
chameleon.style.padding = "5px";
// Enable Chameleon Movement
function enableChameleonMovement() {
window.addEventListener("keydown", (e) => {
keys[e.key] = true;
});
window.addEventListener("keyup", (e) => {
keys[e.key] = false;
});
movementInterval = setInterval(() => {
if (!isGameRunning || isPaused) return;
const chameleonLeft = parseInt(window.getComputedStyle(chameleon).left);
const gameAreaWidth = gameArea.offsetWidth;
const chameleonWidth = chameleon.offsetWidth;
// Move Left
if (keys["ArrowLeft"] && chameleonLeft > 0) {
chameleon.style.left = `${chameleonLeft - chameleonSpeed}px`;
}
// Move Right
if (keys["ArrowRight"] && chameleonLeft < gameAreaWidth - chameleonWidth) {
chameleon.style.left = `${Math.min(chameleonLeft + chameleonSpeed, gameAreaWidth - chameleonWidth)}px`;
}
}, 20);
// Mobile and Desktop Touch Support
let touchStartX = 0;
let touchEndX = 0;
gameArea.addEventListener("touchstart", (e) => {
touchStartX = e.changedTouches[0].screenX;
});
gameArea.addEventListener("touchmove", (e) => {
// Prevent default scrolling behavior
e.preventDefault();
touchEndX = e.changedTouches[0].screenX;
const chameleonLeft = parseInt(window.getComputedStyle(chameleon).left);
const touchDiff = touchEndX - touchStartX;
if (touchDiff > 10 && chameleonLeft < gameAreaWidth - chameleonWidth) {
// Move right when swiped right
chameleon.style.left = `${Math.min(chameleonLeft + (touchDiff * 0.5), gameAreaWidth - chameleonWidth)}px`;
} else if (touchDiff < -10 && chameleonLeft > 0) {
// Move left when swiped left
chameleon.style.left = `${Math.max(chameleonLeft + (touchDiff * 0.5), 0)}px`;
}
touchStartX = touchEndX; // Update the start point for the next touchmove event
});
// Desktop Touch Support
gameArea.addEventListener("mousedown", (e) => {
touchStartX = e.clientX;
});
gameArea.addEventListener("mousemove", (e) => {
if (e.buttons !== 1) return; // Only move if the mouse button is held down
touchEndX = e.clientX;
const chameleonLeft = parseInt(window.getComputedStyle(chameleon).left);
const touchDiff = touchEndX - touchStartX;
if (touchDiff > 10 && chameleonLeft < gameAreaWidth - chameleonWidth) {
chameleon.style.left = `${Math.min(chameleonLeft + (touchDiff * 0.5), gameAreaWidth - chameleonWidth)}px`;
} else if (touchDiff < -10 && chameleonLeft > 0) {
chameleon.style.left = `${Math.max(chameleonLeft + (touchDiff * 0.5), 0)}px`;
}
touchStartX = touchEndX;
});
}
// End Game
function endGame() {
isGameRunning = false;
clearInterval(gameInterval);
clearInterval(movementInterval);
finalScoreElement.textContent = score;
gameContainer.style.display = "none";
gameOverScreen.style.display = "block";
updateHighScores(score); // Update high scores after game over
displayHighScores(); // Display high scores
}
// Update High Scores
function updateHighScores(newScore) {
topScores.push(newScore);
topScores.sort((a, b) => b - a); // Sort scores in descending order
if (topScores.length > 5) topScores.pop(); // Keep only top 5 scores
localStorage.setItem("topScores", JSON.stringify(topScores));
}
// Display High Scores
function displayHighScores() {
scoreList.innerHTML = ""; // Clear the list
topScores.forEach((score, index) => {
const listItem = document.createElement("li");
listItem.textContent = `#${index + 1}: ${score}`;
scoreList.appendChild(listItem);
});
}
// Event Listener for Return Home Button
returnToHomeButton.addEventListener("click", () => {
document.getElementById("high-score-page").style.display = "none";
document.getElementById("home-interface").style.display = "block";
document.body.classList.remove("no-scroll"); // Remove static behavior when returning home
})