-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpong.js
More file actions
executable file
Β·642 lines (563 loc) Β· 20.1 KB
/
pong.js
File metadata and controls
executable file
Β·642 lines (563 loc) Β· 20.1 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/env node
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// π PONG β Terminal Pong (Solo & Multiplayer)
//
// Usage:
// node pong.js solo [easy|medium|hard] β Play vs computer AI
// node pong.js host [port] β Start as Player 1 (left paddle)
// node pong.js join <ip> [port] β Join as Player 2 (right paddle)
//
// Controls:
// W / β Move paddle up
// S / β Move paddle down
// Q Quit
// R Rematch (after game ends)
//
// Zero dependencies β just Node.js.
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const net = require('net');
const readline = require('readline');
// ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const PORT = parseInt(process.argv[3] || process.argv[4] || '4040', 10);
const TICK_MS = 1000 / 60; // 60 fps
const WIDTH = 78;
const HEIGHT = 22;
const PADDLE_H = 5;
const PADDLE_X_OFFSET = 2;
const BALL_SPEED_INIT = 0.6;
const BALL_SPEED_INC = 0.05;
const BALL_MAX_SPEED = 1.8;
const WIN_SCORE = 7;
// ββ Game State ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function createState() {
return {
ball: { x: WIDTH / 2, y: HEIGHT / 2, vx: BALL_SPEED_INIT, vy: BALL_SPEED_INIT * 0.6 },
p1: { y: HEIGHT / 2 - PADDLE_H / 2, score: 0 },
p2: { y: HEIGHT / 2 - PADDLE_H / 2, score: 0 },
msg: '',
running: true,
countdown: 3,
winner: null,
};
}
// ββ Terminal helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const CSI = '\x1b[';
const clear = () => process.stdout.write(`${CSI}2J${CSI}H`);
const moveTo = (x, y) => `${CSI}${y + 1};${x + 1}H`;
const hide = () => process.stdout.write(`${CSI}?25l`);
const show = () => process.stdout.write(`${CSI}?25h`);
const bold = (s) => `${CSI}1m${s}${CSI}0m`;
const dim = (s) => `${CSI}2m${s}${CSI}0m`;
const cyan = (s) => `${CSI}36m${s}${CSI}0m`;
const yellow = (s) => `${CSI}33m${s}${CSI}0m`;
const green = (s) => `${CSI}32m${s}${CSI}0m`;
const red = (s) => `${CSI}31m${s}${CSI}0m`;
const white = (s) => `${CSI}97m${s}${CSI}0m`;
const magenta = (s) => `${CSI}35m${s}${CSI}0m`;
// ββ Rendering βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function render(state, role) {
let buf = '';
buf += `${CSI}H`; // cursor home
const fw = WIDTH + 2; // +2 for border chars
// Score line
const scoreText = ` ${state.p1.score} Β· ${state.p2.score} `;
const roleText = role === 'host' ? ' You: LEFT ' : ' You: RIGHT ';
const pad = fw - scoreText.length - roleText.length;
buf += moveTo(0, 0);
buf += cyan(roleText) + ' '.repeat(Math.max(0, pad)) + bold(white(scoreText));
// Top border
buf += moveTo(0, 1);
buf += dim('β' + 'β'.repeat(WIDTH) + 'β');
// Field rows
for (let row = 0; row < HEIGHT; row++) {
buf += moveTo(0, row + 2);
let line = '';
for (let col = 0; col < WIDTH; col++) {
const isP1 = col === PADDLE_X_OFFSET && row >= state.p1.y && row < state.p1.y + PADDLE_H;
const isP2 = col === WIDTH - PADDLE_X_OFFSET - 1 && row >= state.p2.y && row < state.p2.y + PADDLE_H;
const isBall = Math.round(state.ball.x) === col && Math.round(state.ball.y) === row;
const isCenter = col === Math.floor(WIDTH / 2);
if (isBall) {
line += yellow('β');
} else if (isP1) {
line += green('β');
} else if (isP2) {
line += magenta('β');
} else if (isCenter) {
line += dim(row % 2 === 0 ? 'β' : ' ');
} else {
line += ' ';
}
}
buf += dim('β') + line + dim('β');
}
// Bottom border
buf += moveTo(0, HEIGHT + 2);
buf += dim('β' + 'β'.repeat(WIDTH) + 'β');
// Message line
buf += moveTo(0, HEIGHT + 3);
buf += ' '.repeat(fw);
buf += moveTo(0, HEIGHT + 3);
if (state.winner) {
const winMsg = state.winner === 1 ? 'π Player 1 wins!' : 'π Player 2 wins!';
buf += bold(yellow(` ${winMsg} Press Q to quit, R to rematch`));
} else if (state.countdown > 0) {
buf += bold(cyan(` Starting in ${state.countdown}...`));
} else if (state.msg) {
buf += dim(` ${state.msg}`);
} else {
buf += dim(' W/β = up Β· S/β = down Β· Q = quit');
}
// Controls hint line
buf += moveTo(0, HEIGHT + 4);
buf += ' '.repeat(fw);
process.stdout.write(buf);
}
// ββ Physics (server only) βββββββββββββββββββββββββββββββββββββββββββββββββββ
function tick(state, p1input, p2input) {
if (!state.running || state.winner) return;
if (state.countdown > 0) return;
const speed = 1.0;
// Move paddles
if (p1input.up) state.p1.y = Math.max(0, state.p1.y - speed);
if (p1input.down) state.p1.y = Math.min(HEIGHT - PADDLE_H, state.p1.y + speed);
if (p2input.up) state.p2.y = Math.max(0, state.p2.y - speed);
if (p2input.down) state.p2.y = Math.min(HEIGHT - PADDLE_H, state.p2.y + speed);
// Move ball
state.ball.x += state.ball.vx;
state.ball.y += state.ball.vy;
// Bounce off top/bottom
if (state.ball.y <= 0) {
state.ball.y = 0;
state.ball.vy = Math.abs(state.ball.vy);
}
if (state.ball.y >= HEIGHT - 1) {
state.ball.y = HEIGHT - 1;
state.ball.vy = -Math.abs(state.ball.vy);
}
// Paddle collision β left
if (
state.ball.x <= PADDLE_X_OFFSET + 1 &&
state.ball.x >= PADDLE_X_OFFSET &&
state.ball.y >= state.p1.y &&
state.ball.y < state.p1.y + PADDLE_H
) {
state.ball.vx = Math.abs(state.ball.vx);
// Angle based on where ball hits paddle
const hitPos = (state.ball.y - state.p1.y) / PADDLE_H - 0.5;
state.ball.vy = hitPos * 1.5;
speedUp(state);
}
// Paddle collision β right
if (
state.ball.x >= WIDTH - PADDLE_X_OFFSET - 2 &&
state.ball.x <= WIDTH - PADDLE_X_OFFSET - 1 &&
state.ball.y >= state.p2.y &&
state.ball.y < state.p2.y + PADDLE_H
) {
state.ball.vx = -Math.abs(state.ball.vx);
const hitPos = (state.ball.y - state.p2.y) / PADDLE_H - 0.5;
state.ball.vy = hitPos * 1.5;
speedUp(state);
}
// Score β ball past left edge
if (state.ball.x < 0) {
state.p2.score++;
checkWin(state) || resetBall(state, 1);
}
// Score β ball past right edge
if (state.ball.x > WIDTH - 1) {
state.p1.score++;
checkWin(state) || resetBall(state, -1);
}
}
function speedUp(state) {
const sign = state.ball.vx > 0 ? 1 : -1;
const cur = Math.abs(state.ball.vx);
state.ball.vx = sign * Math.min(cur + BALL_SPEED_INC, BALL_MAX_SPEED);
}
function resetBall(state, dir) {
state.ball.x = WIDTH / 2;
state.ball.y = HEIGHT / 2;
state.ball.vx = BALL_SPEED_INIT * dir;
state.ball.vy = (Math.random() - 0.5) * BALL_SPEED_INIT;
}
function checkWin(state) {
if (state.p1.score >= WIN_SCORE) {
state.winner = 1;
return true;
}
if (state.p2.score >= WIN_SCORE) {
state.winner = 2;
return true;
}
return false;
}
// ββ Input handling ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function setupInput(onKey) {
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
for (const ch of data) {
onKey(ch);
}
// Handle escape sequences (arrow keys)
if (data === '\x1b[A') onKey('UP');
if (data === '\x1b[B') onKey('DOWN');
});
}
// ββ Network Protocol ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Newline-delimited JSON over TCP
function sendMsg(socket, obj) {
try {
socket.write(JSON.stringify(obj) + '\n');
} catch {}
}
function onMessages(socket, handler) {
let buf = '';
socket.on('data', (data) => {
buf += data.toString();
let nl;
while ((nl = buf.indexOf('\n')) !== -1) {
const line = buf.slice(0, nl);
buf = buf.slice(nl + 1);
try {
handler(JSON.parse(line));
} catch {}
}
});
}
// ββ HOST MODE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function runHost() {
let state = createState();
let p1input = { up: false, down: false };
let p2input = { up: false, down: false };
let client = null;
clear();
hide();
state.msg = 'Waiting for Player 2 to join...';
render(state, 'host');
const localIP = getLocalIP();
process.stdout.write(moveTo(0, HEIGHT + 5));
process.stdout.write(dim(` Player 2 command: `) + bold(cyan(`node pong.js join ${localIP} ${PORT}`)));
process.stdout.write(moveTo(0, HEIGHT + 6));
process.stdout.write(dim(` Listening on port ${PORT}...`));
const server = net.createServer((socket) => {
if (client) {
socket.end();
return;
}
client = socket;
state.msg = 'Player 2 connected!';
// Start countdown
let cd = 3;
state.countdown = cd;
const cdTimer = setInterval(() => {
cd--;
state.countdown = cd;
if (cd <= 0) clearInterval(cdTimer);
}, 1000);
onMessages(socket, (msg) => {
if (msg.type === 'input') {
p2input = msg.input;
}
if (msg.type === 'rematch') {
state = createState();
p1input = { up: false, down: false };
p2input = { up: false, down: false };
let cd2 = 3;
state.countdown = cd2;
const cdTimer2 = setInterval(() => {
cd2--;
state.countdown = cd2;
if (cd2 <= 0) clearInterval(cdTimer2);
}, 1000);
}
});
socket.on('close', () => {
client = null;
state.msg = 'Player 2 disconnected. Waiting for reconnect...';
state.running = false;
});
socket.on('error', () => {
client = null;
});
});
server.listen(PORT, '0.0.0.0');
// Input
setupInput((key) => {
if (key === 'q' || key === 'Q' || key === '\x03') cleanup(server);
if (key === 'w' || key === 'W' || key === 'UP') p1input.up = true;
if (key === 's' || key === 'S' || key === 'DOWN') p1input.down = true;
if (key === 'r' || key === 'R') {
if (state.winner) {
state = createState();
p1input = { up: false, down: false };
p2input = { up: false, down: false };
let cd3 = 3;
state.countdown = cd3;
const cdTimer3 = setInterval(() => {
cd3--;
state.countdown = cd3;
if (cd3 <= 0) clearInterval(cdTimer3);
}, 1000);
if (client) sendMsg(client, { type: 'rematch' });
}
}
});
// Key release simulation β reset each tick
setInterval(() => {
// Game tick
tick(state, p1input, p2input);
// Render locally
render(state, 'host');
// Send state to client
if (client) {
sendMsg(client, { type: 'state', state });
}
// Reset momentary inputs
p1input = { up: false, down: false };
}, TICK_MS);
}
// ββ CLIENT MODE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function runClient(host) {
let state = createState();
let myInput = { up: false, down: false };
clear();
hide();
state.msg = `Connecting to ${host}:${PORT}...`;
render(state, 'client');
const socket = net.createConnection({ host, port: PORT }, () => {
state.msg = 'Connected! Waiting for game to start...';
render(state, 'client');
});
onMessages(socket, (msg) => {
if (msg.type === 'state') {
state = msg.state;
render(state, 'client');
}
if (msg.type === 'rematch') {
state = createState();
}
});
socket.on('error', (err) => {
clear();
show();
console.error(red(` Connection failed: ${err.message}`));
console.error(dim(` Make sure the host is running: node pong.js host`));
process.exit(1);
});
socket.on('close', () => {
clear();
show();
console.log(yellow(' Host disconnected. Game over.'));
process.exit(0);
});
// Input β send to server
setupInput((key) => {
if (key === 'q' || key === 'Q' || key === '\x03') {
socket.end();
cleanup();
}
if (key === 'w' || key === 'W' || key === 'UP') myInput.up = true;
if (key === 's' || key === 'S' || key === 'DOWN') myInput.down = true;
if (key === 'r' || key === 'R') {
sendMsg(socket, { type: 'rematch' });
}
});
// Send input at 60fps
setInterval(() => {
if (myInput.up || myInput.down) {
sendMsg(socket, { type: 'input', input: myInput });
}
myInput = { up: false, down: false };
}, TICK_MS);
}
// ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function getLocalIP() {
const os = require('os');
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '127.0.0.1';
}
function cleanup(server) {
show();
clear();
process.stdout.write(moveTo(0, 0));
console.log(dim(' Thanks for playing! π'));
if (server) server.close();
process.exit(0);
}
// ββ ZEN MODE (Autonomous AI vs AI spectator) βββββββββββββββββββββββββββββββ
const ZEN_QUOTES = [
'breathe in... breathe out...',
'the ball knows where to go',
'watch the rhythm, not the score',
'you are not your build errors',
'let the rappters play',
'nothing to do. nowhere to be.',
'the deploy will finish when it finishes',
'observe without attachment',
'each rally is a tiny meditation',
'the terminal is your garden',
'patience is a feature, not a bug',
'be the ball',
'your code is compiling. you are enough.',
];
function runZen() {
let state = createState();
let quoteIdx = Math.floor(Math.random() * ZEN_QUOTES.length);
let quoteTick = 0;
const QUOTE_INTERVAL = 60 * 6; // rotate quote every ~6 seconds
// Two AI personalities β slightly different so rallies feel organic
const leftAI = { speed: 0.72, reactionZone: 1.8, missChance: 0.06, drift: 0.025 };
const rightAI = { speed: 0.68, reactionZone: 2.0, missChance: 0.07, drift: 0.020 };
clear();
hide();
// Countdown
let cd = 3;
state.countdown = cd;
const cdTimer = setInterval(() => {
cd--;
state.countdown = cd;
if (cd <= 0) clearInterval(cdTimer);
}, 1000);
// Only input: Q to quit
setupInput((key) => {
if (key === 'q' || key === 'Q' || key === '\x03') cleanup();
});
function moveAI(paddle, ai, ballHeadingToward) {
const center = paddle.y + PADDLE_H / 2;
const diff = state.ball.y - center;
if (ballHeadingToward) {
if (Math.random() > ai.missChance) {
if (Math.abs(diff) > ai.reactionZone) {
paddle.y += (diff > 0 ? ai.speed : -ai.speed);
}
}
} else {
// Lazily drift toward center
const mid = HEIGHT / 2 - PADDLE_H / 2;
paddle.y += (mid - paddle.y) * ai.drift;
}
paddle.y = Math.max(0, Math.min(HEIGHT - PADDLE_H, paddle.y));
}
// Game loop
setInterval(() => {
if (state.running && !state.winner && state.countdown <= 0) {
moveAI(state.p1, leftAI, state.ball.vx < 0);
moveAI(state.p2, rightAI, state.ball.vx > 0);
}
tick(state, { up: false, down: false }, { up: false, down: false });
// Auto-rematch after a win
if (state.winner) {
setTimeout(() => {
state = createState();
state.countdown = 0; // no countdown on rematch
}, 2500);
state.running = false; // freeze until rematch
}
// Rotate zen quote
quoteTick++;
if (quoteTick >= QUOTE_INTERVAL) {
quoteTick = 0;
quoteIdx = (quoteIdx + 1) % ZEN_QUOTES.length;
}
renderZen(state, ZEN_QUOTES[quoteIdx]);
}, TICK_MS);
}
function renderZen(state, quote) {
let buf = '';
buf += `${CSI}H`;
const fw = WIDTH + 2;
// Header: rappter names + score
const scoreText = ` ${state.p1.score} Β· ${state.p2.score} `;
const titleText = ' π¦ rappterL vs rappterR π¦ ';
const pad = fw - scoreText.length - titleText.length;
buf += moveTo(0, 0);
buf += green(titleText) + ' '.repeat(Math.max(0, pad)) + bold(white(scoreText));
// Top border
buf += moveTo(0, 1);
buf += dim('β' + 'β'.repeat(WIDTH) + 'β');
// Field rows
for (let row = 0; row < HEIGHT; row++) {
buf += moveTo(0, row + 2);
let line = '';
for (let col = 0; col < WIDTH; col++) {
const isP1 = col === PADDLE_X_OFFSET && row >= state.p1.y && row < state.p1.y + PADDLE_H;
const isP2 = col === WIDTH - PADDLE_X_OFFSET - 1 && row >= state.p2.y && row < state.p2.y + PADDLE_H;
const isBall = Math.round(state.ball.x) === col && Math.round(state.ball.y) === row;
const isCenter = col === Math.floor(WIDTH / 2);
if (isBall) {
line += yellow('β');
} else if (isP1) {
line += green('β');
} else if (isP2) {
line += cyan('β');
} else if (isCenter) {
line += dim(row % 2 === 0 ? 'β' : ' ');
} else {
line += ' ';
}
}
buf += dim('β') + line + dim('β');
}
// Bottom border
buf += moveTo(0, HEIGHT + 2);
buf += dim('β' + 'β'.repeat(WIDTH) + 'β');
// Zen quote line
buf += moveTo(0, HEIGHT + 3);
buf += ' '.repeat(fw);
buf += moveTo(0, HEIGHT + 3);
if (state.winner) {
const winMsg = state.winner === 1 ? 'π¦ rappterL takes the round!' : 'π¦ rappterR takes the round!';
buf += bold(yellow(` ${winMsg}`));
} else if (state.countdown > 0) {
buf += bold(cyan(` Starting in ${state.countdown}...`));
} else {
buf += dim(` π§ ${quote}`);
}
// Hint line
buf += moveTo(0, HEIGHT + 4);
buf += ' '.repeat(fw);
buf += moveTo(0, HEIGHT + 4);
buf += dim(' Q = quit Β· just watch and breathe');
process.stdout.write(buf);
}
// ββ Entry point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const mode = process.argv[2];
if (!mode || !['host', 'join', 'zen'].includes(mode)) {
console.log('');
console.log(bold(' π PONG β Terminal Pong'));
console.log(dim(' βββββββββββββββββββββββββββββββββββββ'));
console.log('');
console.log(` ${bold('Zen mode:')} ${cyan('node pong.js zen')} ${dim('β watch two AIs play while you breathe')}`);
console.log(` ${bold('Host a game:')} ${cyan('node pong.js host [port]')}`);
console.log(` ${bold('Join a game:')} ${cyan('node pong.js join <ip> [port]')}`);
console.log('');
console.log(dim(` Default port: ${PORT}`));
console.log(dim(' First to 7 wins. Q to quit.'));
console.log('');
process.exit(0);
}
if (mode === 'zen') {
runZen();
} else if (mode === 'host') {
runHost();
} else if (mode === 'join') {
const host = process.argv[3];
if (!host) {
console.error(red(' Error: specify host IP β node pong.js join <ip> [port]'));
process.exit(1);
}
runClient(host);
}