-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.html
More file actions
275 lines (235 loc) · 9.97 KB
/
pong.html
File metadata and controls
275 lines (235 loc) · 9.97 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pong Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333; /* Darker background for less eye strain */
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #fff; /* White border for the canvas */
background-color: #000; /* Black playing field */
box-shadow: 0 0 20px rgba(255, 255, 255, 0.3); /* Subtle glow */
}
</style>
</head>
<body>
<canvas id="pongCanvas"></canvas>
<script>
const canvas = document.getElementById('pongCanvas');
const context = canvas.getContext('2d');
// Game settings
canvas.width = 800;
canvas.height = 600;
const paddleWidth = 15;
const paddleHeight = 100;
const ballRadius = 10;
const playerPaddleSpeed = 8;
const aiPaddleSpeed = playerPaddleSpeed * 0.6; // AI is a bit slower
// Colors
const PADDLE_COLOR = "white";
const BALL_COLOR = "white";
const NET_COLOR = "rgba(255, 255, 255, 0.5)"; // Semi-transparent net
const SCORE_COLOR = "white";
const BACKGROUND_COLOR = "black";
// Player paddle (right side, controlled by arrows)
const playerPaddle = {
x: canvas.width - paddleWidth - 30, // A bit more offset from the edge
y: canvas.height / 2 - paddleHeight / 2,
width: paddleWidth,
height: paddleHeight,
dy: 0, // direction of movement
score: 0
};
// AI paddle (left side)
const aiPaddle = {
x: 30, // A bit more offset from the edge
y: canvas.height / 2 - paddleHeight / 2,
width: paddleWidth,
height: paddleHeight,
score: 0
};
// Ball
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: ballRadius,
speed: 5,
dx: 5, // direction x
dy: 5 // direction y
};
// --- Drawing Functions ---
function drawRect(x, y, w, h, color) {
context.fillStyle = color;
context.fillRect(x, y, w, h);
}
function drawCircle(x, y, r, color) {
context.fillStyle = color;
context.beginPath();
context.arc(x, y, r, 0, Math.PI * 2, false);
context.closePath();
context.fill();
}
function drawText(text, x, y, color, fontSize = "60px", font = "Consolas, 'Courier New', monospace", textAlign = "center", textBaseline = "middle") {
context.fillStyle = color;
context.font = `${fontSize} ${font}`;
context.textAlign = textAlign;
context.textBaseline = textBaseline;
context.fillText(text, x, y);
}
function drawNet() {
for (let i = 0; i <= canvas.height; i += 20) { // Dashes are a bit longer
drawRect(canvas.width / 2 - 1.5, i, 3, 10, NET_COLOR); // Slightly thicker net
}
}
// --- Input Handling (Arrow Keys for Player Paddle) ---
document.addEventListener('keydown', function(event) {
switch(event.key) {
case 'ArrowUp':
playerPaddle.dy = -playerPaddleSpeed;
break;
case 'ArrowDown':
playerPaddle.dy = playerPaddleSpeed;
break;
}
});
document.addEventListener('keyup', function(event) {
switch(event.key) {
case 'ArrowUp':
case 'ArrowDown':
playerPaddle.dy = 0;
break;
}
});
// --- Update Functions ---
function updatePlayerPaddle() {
playerPaddle.y += playerPaddle.dy;
// Keep player paddle within canvas bounds
if (playerPaddle.y < 0) {
playerPaddle.y = 0;
} else if (playerPaddle.y + playerPaddle.height > canvas.height) {
playerPaddle.y = canvas.height - playerPaddle.height;
}
}
function updateAiPaddle() {
const aiPaddleCenter = aiPaddle.y + aiPaddle.height / 2;
const ballCenterY = ball.y;
// AI tries to follow the ball with a small dead zone to prevent jittering
const deadZone = 10; // Pixels
if (Math.abs(aiPaddleCenter - ballCenterY) > deadZone) {
if (aiPaddleCenter < ballCenterY) {
aiPaddle.y += aiPaddleSpeed;
} else if (aiPaddleCenter > ballCenterY) {
aiPaddle.y -= aiPaddleSpeed;
}
}
// Keep AI paddle within canvas bounds
if (aiPaddle.y < 0) {
aiPaddle.y = 0;
} else if (aiPaddle.y + aiPaddle.height > canvas.height) {
aiPaddle.y = canvas.height - aiPaddle.height;
}
}
function resetBall(serveDirectionX) {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.speed = 5; // Reset speed
// Angle for serving, slightly randomized but not too vertical
let angle = (Math.random() * Math.PI / 3) - (Math.PI / 6); // -30 to +30 degrees
ball.dx = serveDirectionX * ball.speed * Math.cos(angle);
ball.dy = ball.speed * Math.sin(angle);
// Ensure the ball doesn't get stuck going too horizontally or vertically initially
if (Math.abs(ball.dy) < 1) {
ball.dy = (Math.random() > 0.5 ? 1 : -1) * 1.5;
}
if (Math.abs(ball.dx) < 2) { // Ensure minimum horizontal speed
ball.dx = serveDirectionX * 2;
}
}
function collides(b, p) { // b for ball, p for paddle
const bTop = b.y - b.radius;
const bBottom = b.y + b.radius;
const bLeft = b.x - b.radius;
const bRight = b.x + b.radius;
const pTop = p.y;
const pBottom = p.y + p.height;
const pLeft = p.x;
const pRight = p.x + p.width;
return bRight > pLeft && bLeft < pRight && bBottom > pTop && bTop < pBottom;
}
function updateBall() {
ball.x += ball.dx;
ball.y += ball.dy;
// Ball collision with top and bottom walls
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.dy *= -1;
// Clamp position to prevent sticking to walls
if (ball.y + ball.radius > canvas.height) {
ball.y = canvas.height - ball.radius;
} else if (ball.y - ball.radius < 0) {
ball.y = 0 + ball.radius;
}
}
// Determine which paddle is relevant for collision
let paddleToHit = (ball.x < canvas.width / 2) ? aiPaddle : playerPaddle;
if (collides(ball, paddleToHit)) {
let collidePoint = (ball.y - (paddleToHit.y + paddleToHit.height / 2));
collidePoint = collidePoint / (paddleToHit.height / 2); // Normalize to -1 to 1
// Max angle of deflection: 45 degrees (PI/4)
let angleRad = (Math.PI / 4) * collidePoint;
if (paddleToHit === aiPaddle) { // Hit AI paddle (left one)
ball.dx = ball.speed * Math.cos(angleRad); // Ball moves right
ball.x = aiPaddle.x + aiPaddle.width + ball.radius; // Reposition to avoid sticking
} else { // Hit player paddle (right one)
ball.dx = -ball.speed * Math.cos(angleRad); // Ball moves left
ball.x = playerPaddle.x - ball.radius; // Reposition
}
ball.dy = ball.speed * Math.sin(angleRad);
ball.speed += 0.25; // Increase ball speed slightly after each hit
if (ball.speed > 15) ball.speed = 15; // Max speed cap
}
// Scoring
if (ball.x + ball.radius < 0) { // Ball went past left edge (AI paddle side)
playerPaddle.score++;
resetBall(-1); // Serve towards AI (who just lost point, so dx is negative)
} else if (ball.x - ball.radius > canvas.width) { // Ball went past right edge (Player paddle side)
aiPaddle.score++;
resetBall(1); // Serve towards Player (who just lost point, so dx is positive)
}
}
// --- Game Loop ---
function gameLoop() {
// 1. Update game state
updatePlayerPaddle();
updateAiPaddle();
updateBall();
// 2. Render (draw) the game
// Clear canvas (draw background)
drawRect(0, 0, canvas.width, canvas.height, BACKGROUND_COLOR);
// Draw net
drawNet();
// Draw paddles
drawRect(playerPaddle.x, playerPaddle.y, playerPaddle.width, playerPaddle.height, PADDLE_COLOR);
drawRect(aiPaddle.x, aiPaddle.y, aiPaddle.width, aiPaddle.height, PADDLE_COLOR);
// Draw ball
drawCircle(ball.x, ball.y, ball.radius, BALL_COLOR);
// Draw scores
drawText(aiPaddle.score.toString(), canvas.width / 4, 50, SCORE_COLOR);
drawText(playerPaddle.score.toString(), 3 * canvas.width / 4, 50, SCORE_COLOR);
// Call gameLoop again on the next frame
requestAnimationFrame(gameLoop);
}
// --- Start the game ---
resetBall(Math.random() > 0.5 ? 1 : -1); // Initial serve with random direction
gameLoop();
</script>
</body>
</html>