-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
336 lines (281 loc) · 10.2 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
334
335
336
// Global Error and Rejection Handlers
window.addEventListener('error', (event) => {
console.error('Error occurred:', event.message);
alert('An unexpected error occurred. Please reload the game.');
});
window.addEventListener('unhandledrejection', (event) => {
console.error('Unhandled Promise Rejection:', event.reason);
alert('A technical issue occurred. Please try again.');
});
// Element References
const gameArea = document.getElementById('game-area');
const player = document.getElementById('player');
const startGameButton = document.getElementById('start-game-btn');
const preGamePopup = document.getElementById('pre-game-popup');
const pauseButton = document.getElementById('pause-button');
const scoreDisplay = document.getElementById('score');
const countdownDisplay = document.getElementById('countdown');
const gameOverMessage = document.getElementById('game-over');
let gameState = {
isRunning: false,
isPaused: false,
isGameOver: false,
isCountdownActive: false,
score: 0,
playerX: 0,
fallingObjects: [],
lastSpawnTime: 0,
lastUpdateTime: 0,
countdown: 3,
};
const playerSpeed = 10;
const objectFallSpeed = 5;
const spawnInterval = 1200;
// Position player initially
gameState.playerX = gameArea.offsetWidth / 2 - player.offsetWidth / 2;
player.style.left = `${gameState.playerX}px`;
// Utility to update score
function updateScore() {
scoreDisplay.textContent = `Score: ${gameState.score}`;
}
// Countdown Function
function startCountdown() {
if (gameState.isCountdownActive) return; // Prevent multiple countdowns
gameState.isCountdownActive = true;
gameState.countdown = 3;
countdownDisplay.textContent = gameState.countdown;
countdownDisplay.classList.remove('hidden');
const countdownInterval = setInterval(() => {
gameState.countdown--;
countdownDisplay.textContent = gameState.countdown;
if (gameState.countdown <= 0) {
clearInterval(countdownInterval);
countdownDisplay.classList.add('hidden');
gameState.isCountdownActive = false;
startGame();
}
}, 1000);
}
// Game Start
function startGame() {
console.log('Game Started!');
gameState.score = 0; // Reset score
updateScore();
gameState.isRunning = true;
gameState.isGameOver = false;
gameState.fallingObjects = [];
gameLoop();
}
// Main Game Loop (within the game loop, we will move the player based on the key states)
function gameLoop() {
if (!gameState.isRunning || gameState.isPaused || gameState.isGameOver) return;
const now = Date.now();
if (!gameState.lastUpdateTime) gameState.lastUpdateTime = now;
const deltaTime = now - gameState.lastUpdateTime;
gameState.lastUpdateTime = now;
// Move player based on key state (continuous movement)
if (keyState.left && gameState.playerX > 0) {
gameState.playerX -= playerSpeed;
} else if (keyState.right && gameState.playerX < gameArea.offsetWidth - player.offsetWidth) {
gameState.playerX += playerSpeed;
}
//Spawn falling objects
if (now - gameState.lastSpawnTime >= spawnInterval) {
createFallingObject();
gameState.lastSpawnTime = now;
}
// Update falling objects
gameState.fallingObjects.forEach((fallingObject) => {
moveFallingObject(fallingObject, deltaTime);
});
// Update player position
player.style.left = `${gameState.playerX}px`;
// Handle Pause/Resume if Escape key is pressed
if (keyState.escape) {
keyState.escape = false; // Prevent multiple toggle on a single press
if (gameState.isPaused) {
resumeGame();
} else {
pauseGame();
}
}
// Continue the game loop
gameLoopRequest = requestAnimationFrame(gameLoop);
}
// Create Falling Objects
function createFallingObject() {
const object = document.createElement('div');
object.classList.add('falling-object');
object.style.left = `${Math.random() * (gameArea.offsetWidth - 30)}px`;
gameArea.appendChild(object);
gameState.fallingObjects.push({ element: object, position: 0 });
}
// Move Falling Objects
function moveFallingObject(fallingObject, deltaTime) {
fallingObject.position += objectFallSpeed * (deltaTime / 16); // Normalize to 60 FPS
fallingObject.element.style.top = `${fallingObject.position}px`;
if (checkCollision(fallingObject)) {
gameOver();
}
if (fallingObject.position > gameArea.offsetHeight) {
gameState.score++;
updateScore();
fallingObject.element.remove();
gameState.fallingObjects = gameState.fallingObjects.filter((obj) => obj !== fallingObject);
}
}
// Collision Detection
function checkCollision(fallingObject) {
const playerRect = player.getBoundingClientRect();
const objectRect = fallingObject.element.getBoundingClientRect();
return !(playerRect.right < objectRect.left ||
playerRect.left > objectRect.right ||
playerRect.bottom < objectRect.top ||
playerRect.top > objectRect.bottom);
}
// Pause and Resume Game
pauseButton.addEventListener('click', () => {
if (gameState.isGameOver) return;
if (gameState.isPaused) {
resumeGame();
} else {
pauseGame();
}
});
function pauseGame() {
gameState.isPaused = true;
pauseButton.textContent = 'Resume';
cancelAnimationFrame(gameLoopRequest);
}
function resumeGame() {
gameState.isPaused = false;
pauseButton.textContent = 'Pause';
gameState.lastUpdateTime = Date.now(); // Adjust for pause duration
gameLoop();
}
// Game Over
function gameOver() {
console.log('Game Over!');
gameState.isRunning = false;
gameState.isGameOver = true;
gameState.fallingObjects.forEach((obj) => obj.element.remove());
gameOverMessage.innerHTML = `Game Over!<br>Score: ${gameState.score}<br>`;
const resetButton = document.createElement('button');
resetButton.textContent = 'Restart';
resetButton.onclick = resetGame;
gameOverMessage.appendChild(resetButton);
gameOverMessage.classList.remove('hidden');
}
// Reset Game
function resetGame() {
cancelAnimationFrame(gameLoopRequest);
gameState = {
...gameState,
isRunning: false,
isPaused: false,
isGameOver: false,
score: 0,
fallingObjects: [],
};
gameOverMessage.classList.add('hidden');
updateScore();
startCountdown();
}
// Key Control Variables
const keyState = {
left: false,
right: false,
escape: false,
};
// Define key codes for ease of reference
const KEY_LEFT = 'ArrowLeft';
const KEY_RIGHT = 'ArrowRight';
const KEY_ESCAPE = 'Escape';
// Listen for keydown events
document.addEventListener('keydown', (e) => {
if (gameState.isPaused || !gameState.isRunning) return;
// Prevent default browser actions for certain keys
if (e.key === KEY_LEFT || e.key === KEY_RIGHT || e.key === KEY_ESCAPE) {
e.preventDefault();
}
// Update key state when a key is pressed
if (e.key === KEY_LEFT) {
keyState.left = true;
} else if (e.key === KEY_RIGHT) {
keyState.right = true;
} else if (e.key === KEY_ESCAPE) {
keyState.escape = true;
}
});
// Listen for keyup events to stop movement
document.addEventListener('keyup', (e) => {
if (e.key === KEY_LEFT) {
keyState.left = false;
} else if (e.key === KEY_RIGHT) {
keyState.right = false;
} else if (e.key === KEY_ESCAPE) {
keyState.escape = false;
}
});
// Touch Control Variables
let touchStartX = 0;
let touchMoveX = 0;
let isTouching = false; // Flag to check if the user is touching
// Define a threshold for movement (in pixels)
const MOVE_THRESHOLD = 10; // Minimum movement to register a change in position
const DEBOUNCE_TIME = 50; // Debounce time in milliseconds (to limit movement frequency)
let lastMoveTime = 0; // Store the last time movement happened
// Touch Start Event
gameArea.addEventListener('touchstart', (e) => {
// Prevent default scrolling behavior on touch devices
e.preventDefault();
// Only handle the first touch point
if (e.touches.length === 1) {
touchStartX = e.touches[0].clientX;
isTouching = true; // User has started touching the screen
}
});
// Touch Move Event
gameArea.addEventListener('touchmove', (e) => {
// Prevent default scrolling behavior on touch devices
e.preventDefault();
if (!isTouching || e.touches.length !== 1) return; // Only handle one touch point
// Update touch position (horizontal) as the user moves their finger
touchMoveX = e.touches[0].clientX;
// Calculate the difference between the starting touch position and current touch position
const touchDifference = touchMoveX - touchStartX;
// Only move the player if the touch moved a significant distance (threshold) and the debounce time has passed
const now = Date.now();
if (Math.abs(touchDifference) > MOVE_THRESHOLD && now - lastMoveTime > DEBOUNCE_TIME) {
// Move the player based on the difference in touch position
if (touchDifference > 0 && gameState.playerX < gameArea.offsetWidth - player.offsetWidth) {
gameState.playerX += playerSpeed; // Move right
} else if (touchDifference < 0 && gameState.playerX > 0) {
gameState.playerX -= playerSpeed; // Move left
}
// Update the player's position on the screen
player.style.left = `${gameState.playerX}px`;
// Update the last move time
lastMoveTime = now;
// Optionally, update touchStartX to ensure continuous movement
touchStartX = touchMoveX;
}
});
// Touch End Event
gameArea.addEventListener('touchend', () => {
isTouching = false; // User stopped touching
});
// Optional: Handle touch cancel (if the user removes a finger from the screen)
gameArea.addEventListener('touchcancel', () => {
isTouching = false; // Reset the touch state
});
// Touch End Event (optional)
// You can handle any cleanup or logic when the touch ends, but it's not always needed for simple movements.
gameArea.addEventListener('touchend', () => {
// Example cleanup if needed
});
// Start Game Button
startGameButton.addEventListener('click', () => {
preGamePopup.style.display = 'none';
startCountdown();
});