forked from Akki-jaiswal/pong-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
555 lines (478 loc) · 17.5 KB
/
script.js
File metadata and controls
555 lines (478 loc) · 17.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Import audio functions and elements from audio.js
import {
playSound,
startBackgroundMusicRotation,
stopBackgroundMusicRotation,
paddleHitSound,
wallHitSound,
scoreSound,
gameOverSound,
playerWinSound,
countdownBeepSound,
backgroundMusicTracks
} from './audio.js';
const canvas = document.getElementById('gameCanvas');
// No console.error for missing canvas - fail silently or assume existence after DOMContentLoaded
if (!canvas) {
throw new Error("Canvas element with ID 'gameCanvas' not found.");
}
const ctx = canvas.getContext('2d');
// No console.error for missing context - fail silently or assume existence
if (!ctx) {
throw new Error("2D rendering context for canvas not available.");
}
// Game state variables
let ballX;
let ballY;
let ballSpeedX = 0;
let ballSpeedY = 0;
const ballRadius = 10;
const paddleWidth = 10;
const paddleHeight = 100;
let playerPaddleY;
let aiPaddleY;
let playerScore = 0;
let aiScore = 0;
const minimumWinningScore = 3;
const scoreDifferenceToWin = 2;
let gamePaused = true;
let animationFrameId = null;
let countdownActive = false;
let countdownValue = 3;
// Global variable to store the countdown interval ID
let countdownIntervalId = null;
let playerName = "Player";
// DOM elements
const welcomeScreen = document.getElementById('welcomeScreen');
const startGameButton = document.getElementById('startGameButton');
const playerNameInput = document.getElementById('playerNameInput');
const pauseButton = document.getElementById('pauseButton');
const difficultySelect = document.getElementById('difficulty');
const playerScoreDisplay = document.getElementById('playerScore');
const aiScoreDisplay = document.getElementById('aiScore');
// DOM elements for Game Over Screen
const gameOverScreen = document.getElementById('gameOverScreen');
const gameOverMessage = document.getElementById('gameOverMessage');
const playAgainButton = document.getElementById('playAgainButton');
// Difficulty level variable
let difficultyLevel = 'medium';
// --- Difficulty Settings ---
const difficultySettings = {
easy: {
ballInitialSpeed: 5,
aiPaddleSpeed: 3
},
medium: {
ballInitialSpeed: 6,
aiPaddleSpeed: 5
},
hard: {
ballInitialSpeed: 7,
aiPaddleSpeed: 7
}
};
// --- Game Functions ---
function resetBall() {
ballX = canvas.width / 2;
ballY = canvas.height / 2;
const currentSettings = difficultySettings[difficultyLevel];
ballSpeedX = (Math.random() > 0.5 ? 1 : -1) * currentSettings.ballInitialSpeed;
ballSpeedY = (Math.random() * 2 - 1) * currentSettings.ballInitialSpeed;
}
// Helper function for rounded rectangles (fallback for older browsers)
function drawRoundedRect(ctx, x, y, width, height, radius) {
if (typeof ctx.roundRect === 'function') {
ctx.roundRect(x, y, width, height, radius);
} else {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
}
function drawEverything() {
// Clear the canvas - let CSS gradient show through
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw paddles with enhanced styling and rounded corners
ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';
ctx.shadowColor = 'rgba(255, 255, 255, 0.5)';
ctx.shadowBlur = 10;
// Draw player paddle with rounded corners
ctx.beginPath();
drawRoundedRect(ctx, 0, playerPaddleY, paddleWidth, paddleHeight, 5);
ctx.fill();
// Draw AI paddle with rounded corners
ctx.beginPath();
drawRoundedRect(ctx, canvas.width - paddleWidth, aiPaddleY, paddleWidth, paddleHeight, 5);
ctx.fill();
// Draw ball with glow effect
ctx.shadowColor = 'rgba(255, 255, 255, 0.8)';
ctx.shadowBlur = 15;
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2, false);
ctx.fillStyle = 'rgba(255, 255, 255, 0.95)';
ctx.fill();
// Reset shadow for text
ctx.shadowBlur = 0;
// Draw center line with modern styling
ctx.setLineDash([5, 10]);
ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 0);
ctx.lineTo(canvas.width / 2, canvas.height);
ctx.stroke();
ctx.setLineDash([]); // Reset line dash
// Draw countdown number ONLY if active
if (countdownActive) {
ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';
ctx.font = '80px Segoe UI';
ctx.textAlign = 'center';
ctx.shadowColor = 'rgba(255, 255, 255, 0.5)';
ctx.shadowBlur = 10;
// Display countdownValue, which will update from 3 down to 0 visually
ctx.fillText(countdownValue === 0 ? "GO!" : countdownValue, canvas.width / 2, canvas.height / 2 + 30);
ctx.shadowBlur = 0;
}
// Update score display with player name
playerScoreDisplay.textContent = `${playerName}: ${playerScore}`;
aiScoreDisplay.textContent = `AI: ${aiScore}`;
}
function moveEverything() {
// Crucial check: Stop movement if game is paused or during countdown
if (gamePaused || countdownActive) return;
ballX += ballSpeedX;
ballY += ballSpeedY;
// Wall collision
if (ballY + ballRadius > canvas.height || ballY - ballRadius < 0) {
ballSpeedY = -ballSpeedY;
playSound(wallHitSound);
}
// --- Scoring Logic ---
// Check if ball goes off screen to the left (AI scores)
if (ballX < 0) {
aiScore++;
updateScoreDisplay();
playSound(scoreSound);
// Check for AI win condition
if (aiScore >= minimumWinningScore && (aiScore - playerScore >= scoreDifferenceToWin)) {
gamePaused = true;
playSound(gameOverSound);
setTimeout(() => {
endGame("AI Wins!");
}, 500);
} else {
gamePaused = true;
resetBall();
startCountdown();
}
}
// Check if ball goes off screen to the right (Player scores)
if (ballX > canvas.width) {
playerScore++;
updateScoreDisplay();
playSound(scoreSound);
// Check for Player win condition
if (playerScore >= minimumWinningScore && (playerScore - aiScore >= scoreDifferenceToWin)) {
gamePaused = true;
playSound(playerWinSound);
setTimeout(() => {
endGame(`${playerName} Wins!`);
}, 500);
} else {
gamePaused = true;
resetBall();
startCountdown();
}
}
// --- Paddle Collision Logic ---
// Player paddle collision
if (ballX - ballRadius < paddleWidth && ballY > playerPaddleY && ballY < playerPaddleY + paddleHeight) {
ballSpeedX = -ballSpeedX;
let deltaY = ballY - (playerPaddleY + paddleHeight / 2);
ballSpeedY = deltaY * 0.35;
playSound(paddleHitSound);
}
// AI paddle collision
if (ballX + ballRadius > canvas.width - paddleWidth && ballY > aiPaddleY && ballY < aiPaddleY + paddleHeight) {
ballSpeedX = -ballSpeedX;
let deltaY = ballY - (aiPaddleY + paddleHeight / 2);
ballSpeedY = deltaY * 0.35;
playSound(paddleHitSound);
}
// AI Paddle Movement Logic
const aiCenter = aiPaddleY + (paddleHeight / 2);
const aiSpeed = difficultySettings[difficultyLevel].aiPaddleSpeed;
if (aiCenter < ballY - 35) {
aiPaddleY += aiSpeed;
} else if (aiCenter > ballY + 35) {
aiPaddleY -= aiSpeed;
}
// Keep AI paddle within canvas bounds
if (aiPaddleY < 0) aiPaddleY = 0;
if (aiPaddleY + paddleHeight > canvas.height) aiPaddleY = canvas.height - paddleHeight;
keyboardPaddleControl();
}
function gameLoop() {
moveEverything();
drawEverything();
animationFrameId = requestAnimationFrame(gameLoop);
}
function updateScoreDisplay() {
playerScoreDisplay.textContent = `${playerName}: ${playerScore}`;
aiScoreDisplay.textContent = `AI: ${aiScore}`;
}
// Function to handle game over logic (now waits for permission to restart)
function endGame(message) {
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
gamePaused = true;
stopBackgroundMusicRotation();
gameOverMessage.textContent = message;
gameOverScreen.style.display = 'flex';
}
function resetGame() {
playerScore = 0;
aiScore = 0;
updateScoreDisplay();
playerPaddleY = (canvas.height - paddleHeight) / 2;
aiPaddleY = (canvas.height - paddleHeight) / 2;
resetBall();
gamePaused = true;
pauseButton.textContent = "Resume";
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
drawEverything();
}
// --- Countdown Function ---
function startCountdown() {
// Clear any existing countdown interval to prevent overlaps
if (countdownIntervalId) {
clearInterval(countdownIntervalId);
countdownIntervalId = null;
}
countdownActive = true;
countdownValue = 3;
drawEverything(); // Draw initial 3
playSound(countdownBeepSound);
// Assign interval ID to the global variable
countdownIntervalId = setInterval(() => {
countdownValue--;
drawEverything(); // Draw updated number
if (countdownValue < 0) {
clearInterval(countdownIntervalId);
countdownIntervalId = null;
}
}, 1000);
// This setTimeout runs after 3 seconds (for 3, 2, 1, GO! sequence)
setTimeout(() => {
countdownActive = false;
gamePaused = false;
pauseButton.textContent = "Pause";
if (!animationFrameId) {
gameLoop();
}
}, 3000);
}
// --- Initial Game Start Handler (from Welcome Screen) ---
startGameButton.addEventListener('click', () => {
playerName = playerNameInput.value.trim();
if (playerName === "") {
playerName = "Player";
}
welcomeScreen.style.display = 'none';
startBackgroundMusicRotation();
resetGame();
startCountdown();
});
// --- Event Listener for Play Again Button ---
playAgainButton.addEventListener('click', () => {
gameOverScreen.style.display = 'none';
resetGame();
startBackgroundMusicRotation();
startCountdown();
});
// --- Event Listeners for In-Game Controls ---
// Keep existing mousemove for desktop
canvas.addEventListener('mousemove', (evt) => {
// Allow player paddle movement only if not paused AND not during countdown
if (gamePaused || countdownActive) return;
let rect = canvas.getBoundingClientRect();
let mousePos = evt.clientY - rect.top;
playerPaddleY = mousePos - (paddleHeight / 2);
if (playerPaddleY < 0) playerPaddleY = 0;
if (playerPaddleY + paddleHeight > canvas.height) playerPaddleY = canvas.height - paddleHeight;
});
// --- NEW: Touch Event Listeners for Mobile ---
canvas.addEventListener('touchstart', function(event) {
event.preventDefault();
handleTouchMove(event);
}, { passive: false });
canvas.addEventListener('touchmove', function(event) {
event.preventDefault();
handleTouchMove(event);
}, { passive: false });
function handleTouchMove(event) {
// Corrected: Use paddleHeight instead of playerPaddle.height
let touchY = event.touches[0].clientY;
// Get canvas position to calculate relative Y
let canvasRect = canvas.getBoundingClientRect();
let relativeTouchY = touchY - canvasRect.top;
// Update player paddle's Y position
// Center paddle on the touch point
playerPaddleY = relativeTouchY - paddleHeight / 2;
// Clamp paddle position to stay within canvas bounds
if (playerPaddleY < 0) {
playerPaddleY = 0;
} else if (playerPaddleY + paddleHeight > canvas.height) {
playerPaddleY = canvas.height - paddleHeight;
}
}
// The pause button now only toggles Pause/Resume for an *already started* game
pauseButton.addEventListener('click', () => {
if (!countdownActive) { // Only allow pause/resume when not in active countdown
if (gamePaused) { // If currently paused, user wants to resume
startBackgroundMusicRotation();
gamePaused = false;
pauseButton.textContent = "Pause";
if (!animationFrameId) {
gameLoop();
}
} else { // If currently playing, user wants to pause
gamePaused = true;
pauseButton.textContent = "Resume";
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
stopBackgroundMusicRotation();
}
}
});
difficultySelect.addEventListener('change', (event) => {
difficultyLevel = event.target.value;
resetGame();
// If game was paused for difficulty change, restart countdown
if (gameState === GAME_STATES.PLAYING || gameState === GAME_STATES.PAUSED) {
startCountdown();
} else {
drawEverything();
}
});
// --- Initial Setup (Show Welcome Screen) ---
// Define GAME_STATES enum outside, or ensure it's accessible.
// For now, let's just make sure gameState is available.
const GAME_STATES = {
WELCOME: 'WELCOME',
PLAYING: 'PLAYING',
PAUSED: 'PAUSED',
GAME_OVER: 'GAME_OVER'
};
let gameState = GAME_STATES.WELCOME; // Initialize game state
document.addEventListener('DOMContentLoaded', () => {
welcomeScreen.style.display = 'flex'; // Show the welcome screen
gameOverScreen.style.display = 'none'; // Ensure game over screen is hidden initially
// Update playerPaddleY and aiPaddleY only after canvas dimensions are known
playerPaddleY = (canvas.height - paddleHeight) / 2;
aiPaddleY = (canvas.height - paddleHeight) / 2;
drawEverything(); // Draw initial state on canvas
});
// Minor adjustment in startGameButton to set gameState
startGameButton.addEventListener('click', () => {
playerName = playerNameInput.value.trim();
if (playerName === "") {
playerName = "Player";
}
welcomeScreen.style.display = 'none';
gameState = GAME_STATES.PLAYING; // Set game state to playing
startBackgroundMusicRotation();
resetGame();
startCountdown();
});
// Minor adjustment in playAgainButton to set gameState
playAgainButton.addEventListener('click', () => {
gameOverScreen.style.display = 'none';
gameState = GAME_STATES.PLAYING; // Set game state to playing
resetGame();
startBackgroundMusicRotation();
startCountdown();
});
// Minor adjustment in pauseButton to use gameState
pauseButton.addEventListener('click', () => {
if (!countdownActive) {
if (gameState === GAME_STATES.PLAYING) {
gamePaused = true;
gameState = GAME_STATES.PAUSED; // Set game state to paused
pauseButton.textContent = "Resume";
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
stopBackgroundMusicRotation();
} else if (gameState === GAME_STATES.PAUSED) {
gamePaused = false;
gameState = GAME_STATES.PLAYING; // Set game state to playing
pauseButton.textContent = "Pause";
startBackgroundMusicRotation();
if (!countdownActive) {
startCountdown();
} else {
gameLoop();
}
}
}
});
// Adjust difficultySelect event listener to use gameState
difficultySelect.addEventListener('change', (event) => {
difficultyLevel = event.target.value;
resetGame();
// Only start countdown if the game was actively playing or paused (not on welcome/game over)
if (gameState === GAME_STATES.PLAYING || gameState === GAME_STATES.PAUSED) {
startCountdown();
} else {
drawEverything();
}
});
// Adjust mousemove listener to use gameState
canvas.addEventListener('mousemove', (evt) => {
if (gameState === GAME_STATES.PLAYING && !countdownActive) {
let rect = canvas.getBoundingClientRect();
let mousePos = evt.clientY - rect.top;
playerPaddleY = mousePos - (paddleHeight / 2);
if (playerPaddleY < 0) playerPaddleY = 0;
if (playerPaddleY + paddleHeight > canvas.height) playerPaddleY = canvas.height - paddleHeight;
}
});
// --- Keyboard Paddle Controls for Up/Down Arrow Keys (add at end of script.js) ---
let upArrowPressed = false;
let downArrowPressed = false;
const paddleMoveSpeed = 8; // we can change this number for faster/slower paddle movement
document.addEventListener('keydown', function(e) {
if (typeof gameState !== "undefined" && gameState === "PLAYING" && !countdownActive) {
if (e.key === "ArrowUp") upArrowPressed = true;
if (e.key === "ArrowDown") downArrowPressed = true;
}
});
document.addEventListener('keyup', function(e) {
if (e.key === "ArrowUp") upArrowPressed = false;
if (e.key === "ArrowDown") downArrowPressed = false;
});
// This function will move the paddle when up/down keys are pressed
function keyboardPaddleControl() {
if (upArrowPressed) playerPaddleY -= paddleMoveSpeed;
if (downArrowPressed) playerPaddleY += paddleMoveSpeed;
// Do not let paddle go outside the screen:
if (playerPaddleY < 0) playerPaddleY = 0;
if (playerPaddleY + paddleHeight > canvas.height) playerPaddleY = canvas.height - paddleHeight;
}