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>Pong Game</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
padding: 0;
|
11 |
+
display: flex;
|
12 |
+
justify-content: center;
|
13 |
+
align-items: center;
|
14 |
+
height: 100vh;
|
15 |
+
background-color: #222;
|
16 |
+
font-family: Arial, sans-serif;
|
17 |
+
overflow: hidden;
|
18 |
+
}
|
19 |
+
|
20 |
+
#game-container {
|
21 |
+
position: relative;
|
22 |
+
width: 800px;
|
23 |
+
height: 500px;
|
24 |
+
border: 2px solid #fff;
|
25 |
+
background-color: #000;
|
26 |
+
overflow: hidden;
|
27 |
+
}
|
28 |
+
|
29 |
+
#ball {
|
30 |
+
position: absolute;
|
31 |
+
width: 20px;
|
32 |
+
height: 20px;
|
33 |
+
background-color: #fff;
|
34 |
+
border-radius: 50%;
|
35 |
+
}
|
36 |
+
|
37 |
+
.paddle {
|
38 |
+
position: absolute;
|
39 |
+
width: 15px;
|
40 |
+
height: 100px;
|
41 |
+
background-color: #fff;
|
42 |
+
}
|
43 |
+
|
44 |
+
#player-paddle {
|
45 |
+
left: 30px;
|
46 |
+
}
|
47 |
+
|
48 |
+
#ai-paddle {
|
49 |
+
right: 30px;
|
50 |
+
}
|
51 |
+
|
52 |
+
#center-line {
|
53 |
+
position: absolute;
|
54 |
+
top: 0;
|
55 |
+
left: 50%;
|
56 |
+
width: 4px;
|
57 |
+
height: 100%;
|
58 |
+
background-color: rgba(255, 255, 255, 0.2);
|
59 |
+
transform: translateX(-50%);
|
60 |
+
}
|
61 |
+
|
62 |
+
#score-display {
|
63 |
+
position: absolute;
|
64 |
+
top: 20px;
|
65 |
+
left: 0;
|
66 |
+
right: 0;
|
67 |
+
text-align: center;
|
68 |
+
font-size: 40px;
|
69 |
+
color: rgba(255, 255, 255, 0.5);
|
70 |
+
z-index: 1;
|
71 |
+
}
|
72 |
+
|
73 |
+
#player-score, #ai-score {
|
74 |
+
display: inline-block;
|
75 |
+
width: 100px;
|
76 |
+
}
|
77 |
+
|
78 |
+
#start-screen {
|
79 |
+
position: absolute;
|
80 |
+
top: 0;
|
81 |
+
left: 0;
|
82 |
+
width: 100%;
|
83 |
+
height: 100%;
|
84 |
+
background-color: rgba(0, 0, 0, 0.7);
|
85 |
+
display: flex;
|
86 |
+
flex-direction: column;
|
87 |
+
justify-content: center;
|
88 |
+
align-items: center;
|
89 |
+
z-index: 2;
|
90 |
+
}
|
91 |
+
|
92 |
+
#game-over {
|
93 |
+
position: absolute;
|
94 |
+
top: 0;
|
95 |
+
left: 0;
|
96 |
+
width: 100%;
|
97 |
+
height: 100%;
|
98 |
+
background-color: rgba(0, 0, 0, 0.7);
|
99 |
+
display: none;
|
100 |
+
flex-direction: column;
|
101 |
+
justify-content: center;
|
102 |
+
align-items: center;
|
103 |
+
z-index: 2;
|
104 |
+
}
|
105 |
+
|
106 |
+
button {
|
107 |
+
padding: 12px 24px;
|
108 |
+
font-size: 18px;
|
109 |
+
background-color: #4CAF50;
|
110 |
+
color: white;
|
111 |
+
border: none;
|
112 |
+
border-radius: 4px;
|
113 |
+
cursor: pointer;
|
114 |
+
margin-top: 20px;
|
115 |
+
}
|
116 |
+
|
117 |
+
button:hover {
|
118 |
+
background-color: #45a049;
|
119 |
+
}
|
120 |
+
|
121 |
+
h1, h2 {
|
122 |
+
color: white;
|
123 |
+
margin: 10px 0;
|
124 |
+
}
|
125 |
+
|
126 |
+
.difficulty {
|
127 |
+
margin-top: 20px;
|
128 |
+
}
|
129 |
+
|
130 |
+
.difficulty button {
|
131 |
+
margin: 0 10px;
|
132 |
+
padding: 8px 16px;
|
133 |
+
}
|
134 |
+
|
135 |
+
#easy { background-color: #4CAF50; }
|
136 |
+
#medium { background-color: #ff9800; }
|
137 |
+
#hard { background-color: #f44336; }
|
138 |
+
</style>
|
139 |
+
</head>
|
140 |
+
<body>
|
141 |
+
<div id="game-container">
|
142 |
+
<div id="score-display">
|
143 |
+
<span id="player-score">0</span>
|
144 |
+
<span id="ai-score">0</span>
|
145 |
+
</div>
|
146 |
+
<div id="center-line"></div>
|
147 |
+
<div id="ball"></div>
|
148 |
+
<div id="player-paddle" class="paddle"></div>
|
149 |
+
<div id="ai-paddle" class="paddle"></div>
|
150 |
+
|
151 |
+
<div id="start-screen">
|
152 |
+
<h1>PONG</h1>
|
153 |
+
<h2>Select Difficulty</h2>
|
154 |
+
<div class="difficulty">
|
155 |
+
<button id="easy">Easy</button>
|
156 |
+
<button id="medium">Medium</button>
|
157 |
+
<button id="hard">Hard</button>
|
158 |
+
</div>
|
159 |
+
</div>
|
160 |
+
|
161 |
+
<div id="game-over">
|
162 |
+
<h1 id="game-result">Game Over</h1>
|
163 |
+
<button id="restart">Play Again</button>
|
164 |
+
</div>
|
165 |
+
</div>
|
166 |
+
|
167 |
+
<script>
|
168 |
+
// Game elements
|
169 |
+
const ball = document.getElementById('ball');
|
170 |
+
const playerPaddle = document.getElementById('player-paddle');
|
171 |
+
const aiPaddle = document.getElementById('ai-paddle');
|
172 |
+
const playerScoreElem = document.getElementById('player-score');
|
173 |
+
const aiScoreElem = document.getElementById('ai-score');
|
174 |
+
const startScreen = document.getElementById('start-screen');
|
175 |
+
const gameOverScreen = document.getElementById('game-over');
|
176 |
+
const gameResult = document.getElementById('game-result');
|
177 |
+
const restartButton = document.getElementById('restart');
|
178 |
+
const container = document.getElementById('game-container');
|
179 |
+
|
180 |
+
// Difficulty buttons
|
181 |
+
const easyButton = document.getElementById('easy');
|
182 |
+
const mediumButton = document.getElementById('medium');
|
183 |
+
const hardButton = document.getElementById('hard');
|
184 |
+
|
185 |
+
// Game state
|
186 |
+
let ballX = 400;
|
187 |
+
let ballY = 250;
|
188 |
+
let ballSpeedX = 5;
|
189 |
+
let ballSpeedY = 5;
|
190 |
+
let playerY = 200;
|
191 |
+
let aiY = 200;
|
192 |
+
let playerScore = 0;
|
193 |
+
let aiScore = 0;
|
194 |
+
let aiSpeed = 3; // Default speed, will be set based on difficulty
|
195 |
+
let maxScore = 5;
|
196 |
+
let ballSize = 20;
|
197 |
+
let paddleHeight = 100;
|
198 |
+
let paddleWidth = 15;
|
199 |
+
let isPlaying = false;
|
200 |
+
let animationFrameId;
|
201 |
+
|
202 |
+
// Initial positioning
|
203 |
+
function initPositions() {
|
204 |
+
ballX = container.offsetWidth / 2;
|
205 |
+
ballY = container.offsetHeight / 2;
|
206 |
+
playerY = container.offsetHeight / 2 - paddleHeight / 2;
|
207 |
+
aiY = container.offsetHeight / 2 - paddleHeight / 2;
|
208 |
+
|
209 |
+
ball.style.left = `${ballX - ballSize / 2}px`;
|
210 |
+
ball.style.top = `${ballY - ballSize / 2}px`;
|
211 |
+
playerPaddle.style.top = `${playerY}px`;
|
212 |
+
aiPaddle.style.top = `${aiY}px`;
|
213 |
+
}
|
214 |
+
|
215 |
+
// Initialize game
|
216 |
+
function init() {
|
217 |
+
// Set paddle height and positioning
|
218 |
+
playerPaddle.style.height = `${paddleHeight}px`;
|
219 |
+
aiPaddle.style.height = `${paddleHeight}px`;
|
220 |
+
|
221 |
+
initPositions();
|
222 |
+
|
223 |
+
// Reset scores
|
224 |
+
playerScore = 0;
|
225 |
+
aiScore = 0;
|
226 |
+
playerScoreElem.textContent = playerScore;
|
227 |
+
aiScoreElem.textContent = aiScore;
|
228 |
+
}
|
229 |
+
|
230 |
+
// Start game with selected difficulty
|
231 |
+
function startGame(difficulty) {
|
232 |
+
switch(difficulty) {
|
233 |
+
case 'easy':
|
234 |
+
aiSpeed = 2.5;
|
235 |
+
break;
|
236 |
+
case 'medium':
|
237 |
+
aiSpeed = 4;
|
238 |
+
break;
|
239 |
+
case 'hard':
|
240 |
+
aiSpeed = 5.5;
|
241 |
+
break;
|
242 |
+
default:
|
243 |
+
aiSpeed = 3;
|
244 |
+
}
|
245 |
+
|
246 |
+
startScreen.style.display = 'none';
|
247 |
+
isPlaying = true;
|
248 |
+
gameLoop();
|
249 |
+
}
|
250 |
+
|
251 |
+
// Main game loop
|
252 |
+
function gameLoop() {
|
253 |
+
if (!isPlaying) return;
|
254 |
+
|
255 |
+
updateBall();
|
256 |
+
updateAI();
|
257 |
+
checkCollision();
|
258 |
+
checkScore();
|
259 |
+
|
260 |
+
animationFrameId = requestAnimationFrame(gameLoop);
|
261 |
+
}
|
262 |
+
|
263 |
+
// Update ball position
|
264 |
+
function updateBall() {
|
265 |
+
ballX += ballSpeedX;
|
266 |
+
ballY += ballSpeedY;
|
267 |
+
|
268 |
+
// Wall collision (top/bottom)
|
269 |
+
if (ballY <= 0 || ballY >= container.offsetHeight) {
|
270 |
+
ballSpeedY = -ballSpeedY;
|
271 |
+
// Ensure ball doesn't get stuck
|
272 |
+
if (ballY <= 0) {
|
273 |
+
ballY = 1;
|
274 |
+
} else {
|
275 |
+
ballY = container.offsetHeight - 1;
|
276 |
+
}
|
277 |
+
}
|
278 |
+
|
279 |
+
ball.style.left = `${ballX - ballSize / 2}px`;
|
280 |
+
ball.style.top = `${ballY - ballSize / 2}px`;
|
281 |
+
}
|
282 |
+
|
283 |
+
// Update AI paddle position
|
284 |
+
function updateAI() {
|
285 |
+
const aiPaddleCenter = aiY + paddleHeight / 2;
|
286 |
+
|
287 |
+
// Add some delay for easier difficulty
|
288 |
+
if (ballX > container.offsetWidth / 2) {
|
289 |
+
if (aiPaddleCenter < ballY - 10) {
|
290 |
+
aiY += aiSpeed;
|
291 |
+
} else if (aiPaddleCenter > ballY + 10) {
|
292 |
+
aiY -= aiSpeed;
|
293 |
+
}
|
294 |
+
}
|
295 |
+
|
296 |
+
// Keep paddle within bounds
|
297 |
+
if (aiY < 0) aiY = 0;
|
298 |
+
if (aiY > container.offsetHeight - paddleHeight) aiY = container.offsetHeight - paddleHeight;
|
299 |
+
|
300 |
+
aiPaddle.style.top = `${aiY}px`;
|
301 |
+
}
|
302 |
+
|
303 |
+
// Check for paddle collisions
|
304 |
+
function checkCollision() {
|
305 |
+
// Player paddle collision
|
306 |
+
if (
|
307 |
+
ballX <= 30 + paddleWidth + ballSize / 2 &&
|
308 |
+
ballX >= 30 &&
|
309 |
+
ballY >= playerY &&
|
310 |
+
ballY <= playerY + paddleHeight
|
311 |
+
) {
|
312 |
+
// Calculate angle based on where ball hits paddle
|
313 |
+
let hitPosition = (ballY - (playerY + paddleHeight / 2)) / (paddleHeight / 2);
|
314 |
+
let angle = hitPosition * (Math.PI / 4); // Max 45 degree angle
|
315 |
+
|
316 |
+
// Increase speed slightly with each hit
|
317 |
+
let speed = Math.sqrt(ballSpeedX * ballSpeedX + ballSpeedY * ballSpeedY);
|
318 |
+
speed = Math.min(speed + 0.2, 12); // Cap max speed
|
319 |
+
|
320 |
+
ballSpeedX = Math.cos(angle) * speed;
|
321 |
+
ballSpeedY = Math.sin(angle) * speed;
|
322 |
+
|
323 |
+
// Play sound (add sound effects if you want to enhance the game)
|
324 |
+
// playSound('paddle');
|
325 |
+
}
|
326 |
+
|
327 |
+
// AI paddle collision
|
328 |
+
if (
|
329 |
+
ballX >= container.offsetWidth - 30 - paddleWidth - ballSize / 2 &&
|
330 |
+
ballX <= container.offsetWidth - 30 &&
|
331 |
+
ballY >= aiY &&
|
332 |
+
ballY <= aiY + paddleHeight
|
333 |
+
) {
|
334 |
+
// Calculate angle based on where ball hits paddle
|
335 |
+
let hitPosition = (ballY - (aiY + paddleHeight / 2)) / (paddleHeight / 2);
|
336 |
+
let angle = hitPosition * (Math.PI / 4); // Max 45 degree angle
|
337 |
+
|
338 |
+
// Increase speed slightly with each hit
|
339 |
+
let speed = Math.sqrt(ballSpeedX * ballSpeedX + ballSpeedY * ballSpeedY);
|
340 |
+
speed = Math.min(speed + 0.2, 12); // Cap max speed
|
341 |
+
|
342 |
+
ballSpeedX = -Math.cos(angle) * speed;
|
343 |
+
ballSpeedY = Math.sin(angle) * speed;
|
344 |
+
|
345 |
+
// Play sound
|
346 |
+
// playSound('paddle');
|
347 |
+
}
|
348 |
+
}
|
349 |
+
|
350 |
+
// Check if a player scored
|
351 |
+
function checkScore() {
|
352 |
+
// Ball went past the player's paddle
|
353 |
+
if (ballX < 0) {
|
354 |
+
aiScore++;
|
355 |
+
aiScoreElem.textContent = aiScore;
|
356 |
+
resetBall(1);
|
357 |
+
// playSound('score');
|
358 |
+
|
359 |
+
if (aiScore >= maxScore) {
|
360 |
+
endGame(false);
|
361 |
+
}
|
362 |
+
}
|
363 |
+
|
364 |
+
// Ball went past the AI's paddle
|
365 |
+
if (ballX > container.offsetWidth) {
|
366 |
+
playerScore++;
|
367 |
+
playerScoreElem.textContent = playerScore;
|
368 |
+
resetBall(-1);
|
369 |
+
// playSound('score');
|
370 |
+
|
371 |
+
if (playerScore >= maxScore) {
|
372 |
+
endGame(true);
|
373 |
+
}
|
374 |
+
}
|
375 |
+
}
|
376 |
+
|
377 |
+
// Reset ball after scoring
|
378 |
+
function resetBall(direction) {
|
379 |
+
ballX = container.offsetWidth / 2;
|
380 |
+
ballY = container.offsetHeight / 2;
|
381 |
+
|
382 |
+
// Start ball in the direction of who just scored
|
383 |
+
let speed = 5; // Reset to initial speed
|
384 |
+
let angle = (Math.random() - 0.5) * (Math.PI / 4); // Random angle up to +/- 45 degrees
|
385 |
+
|
386 |
+
ballSpeedX = direction * Math.cos(angle) * speed;
|
387 |
+
ballSpeedY = Math.sin(angle) * speed;
|
388 |
+
}
|
389 |
+
|
390 |
+
// End game and show results
|
391 |
+
function endGame(playerWon) {
|
392 |
+
isPlaying = false;
|
393 |
+
cancelAnimationFrame(animationFrameId);
|
394 |
+
|
395 |
+
if (playerWon) {
|
396 |
+
gameResult.textContent = 'You Win!';
|
397 |
+
} else {
|
398 |
+
gameResult.textContent = 'AI Wins!';
|
399 |
+
}
|
400 |
+
|
401 |
+
gameOverScreen.style.display = 'flex';
|
402 |
+
}
|
403 |
+
|
404 |
+
// Mouse/touch movement for the player paddle
|
405 |
+
function handleMouseMove(e) {
|
406 |
+
if (!isPlaying) return;
|
407 |
+
|
408 |
+
// Get relative mouse position
|
409 |
+
const containerRect = container.getBoundingClientRect();
|
410 |
+
let mouseY;
|
411 |
+
|
412 |
+
if (e.type === 'mousemove') {
|
413 |
+
mouseY = e.clientY - containerRect.top;
|
414 |
+
} else if (e.type === 'touchmove') {
|
415 |
+
mouseY = e.touches[0].clientY - containerRect.top;
|
416 |
+
e.preventDefault(); // Prevent scrolling
|
417 |
+
}
|
418 |
+
|
419 |
+
// Move paddle and keep within bounds
|
420 |
+
playerY = mouseY - paddleHeight / 2;
|
421 |
+
|
422 |
+
if (playerY < 0) playerY = 0;
|
423 |
+
if (playerY > container.offsetHeight - paddleHeight) playerY = container.offsetHeight - paddleHeight;
|
424 |
+
|
425 |
+
playerPaddle.style.top = `${playerY}px`;
|
426 |
+
}
|
427 |
+
|
428 |
+
// Event listeners
|
429 |
+
easyButton.addEventListener('click', () => startGame('easy'));
|
430 |
+
mediumButton.addEventListener('click', () => startGame('medium'));
|
431 |
+
hardButton.addEventListener('click', () => startGame('hard'));
|
432 |
+
|
433 |
+
restartButton.addEventListener('click', () => {
|
434 |
+
gameOverScreen.style.display = 'none';
|
435 |
+
startScreen.style.display = 'flex';
|
436 |
+
init();
|
437 |
+
});
|
438 |
+
|
439 |
+
// Mouse and touch events for paddle movement
|
440 |
+
container.addEventListener('mousemove', handleMouseMove);
|
441 |
+
container.addEventListener('touchmove', handleMouseMove);
|
442 |
+
|
443 |
+
// Initialize the game
|
444 |
+
init();
|
445 |
+
</script>
|
446 |
+
</body>
|
447 |
+
</html>
|