-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.js
197 lines (166 loc) · 5.97 KB
/
pong.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
const fs = require('fs');
const crypto = require('crypto');
const ssh2 = require('ssh2');
// Generate or load host key
let hostKey;
if (fs.existsSync('host_key')) {
hostKey = fs.readFileSync('host_key');
} else {
hostKey = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
}).privateKey.export({ type: 'pkcs1', format: 'pem' });
fs.writeFileSync('host_key', hostKey);
}
// Game state
let gameState = {
paddleLeft: 0,
paddleRight: 0,
ballX: 0,
ballY: 0,
ballSpeedX: 0.5,
ballSpeedY: 0.5,
scoreLeft: 0,
scoreRight: 0,
paused: false,
rows: 24,
columns: 80
};
// Game loop
function gameLoop(stream) {
if (gameState.paused) {
setTimeout(() => gameLoop(stream), 50);
return;
}
// Update ball position
gameState.ballX += gameState.ballSpeedX;
gameState.ballY += gameState.ballSpeedY;
// Check collisions with walls
if (gameState.ballY <= 0 || gameState.ballY >= gameState.rows - 1) {
gameState.ballSpeedY *= -1;
}
// Check collisions with paddles
if (gameState.ballX <= 1 && gameState.ballY >= gameState.paddleLeft && gameState.ballY <= gameState.paddleLeft + 5) {
gameState.ballSpeedX = Math.abs(gameState.ballSpeedX);
gameState.ballSpeedY += (Math.random() - 0.5) * 0.2; // Reduced randomness
}
if (gameState.ballX >= gameState.columns - 2 && gameState.ballY >= gameState.paddleRight && gameState.ballY <= gameState.paddleRight + 5) {
gameState.ballSpeedX = -Math.abs(gameState.ballSpeedX);
gameState.ballSpeedY += (Math.random() - 0.5) * 0.2; // Reduced randomness
}
// Ensure ball stays within vertical bounds
gameState.ballY = Math.max(0, Math.min(gameState.rows - 1, gameState.ballY));
// Check for scoring
if (gameState.ballX < 0) {
gameState.scoreRight++;
resetBall();
} else if (gameState.ballX >= gameState.columns) {
gameState.scoreLeft++;
resetBall();
}
// Move AI paddle
moveAIPaddle();
// Render game
renderGame(stream);
// Schedule next frame
setTimeout(() => gameLoop(stream), 50);
}
function resetBall() {
gameState.ballX = Math.floor(gameState.columns / 2);
gameState.ballY = Math.floor(gameState.rows / 2);
gameState.ballSpeedX = (Math.random() > 0.5 ? 0.5 : -0.5) * (0.8 + Math.random() * 0.4);
gameState.ballSpeedY = (Math.random() > 0.5 ? 0.5 : -0.5) * (0.8 + Math.random() * 0.4);
}
function moveAIPaddle() {
const paddleCenter = gameState.paddleRight + 2.5;
const moveSpeed = 0.4; // Slightly reduced AI speed
if (gameState.ballY < paddleCenter - 1) {
gameState.paddleRight = Math.max(0, gameState.paddleRight - moveSpeed);
} else if (gameState.ballY > paddleCenter + 1) {
gameState.paddleRight = Math.min(gameState.rows - 6, gameState.paddleRight + moveSpeed);
}
}
function renderGame(stream) {
let output = '\x1b[2J\x1b[H'; // Clear screen and move cursor to top-left
// Render paddles
for (let i = 0; i < 6; i++) {
output += `\x1b[${Math.floor(gameState.paddleLeft) + i + 1};1H\x1b[32m|\x1b[0m`;
output += `\x1b[${Math.floor(gameState.paddleRight) + i + 1};${gameState.columns}H\x1b[32m|\x1b[0m`;
}
// Render middle bar
for (let i = 0; i < gameState.rows; i++) {
output += `\x1b[${i + 1};${Math.floor(gameState.columns / 2)}H\x1b[37m|\x1b[0m`;
}
// Render ball
output += `\x1b[${Math.floor(gameState.ballY) + 1};${Math.floor(gameState.ballX) + 1}H\x1b[31m●\x1b[0m`;
// Render player/computer indicators and scores
output += `\x1b[${gameState.rows};${Math.floor(gameState.columns / 4) - 10}H\x1b[36mPLAYER: ${gameState.scoreLeft}\x1b[0m`;
output += `\x1b[${gameState.rows};${Math.floor(3 * gameState.columns / 4) - 14}H\x1b[36mCOMPUTER: ${gameState.scoreRight}\x1b[0m`;
stream.write(output);
}
// Function to start the game
function startGame(stream) {
// Initialize game state
gameState.paddleLeft = Math.floor(gameState.rows / 2) - 3;
gameState.paddleRight = Math.floor(gameState.rows / 2) - 3;
resetBall();
// Handle input
stream.on('data', (data) => {
const key = data.toString();
if (key === '\u001b') { // ESC key
gameState.paused = !gameState.paused;
if (!gameState.paused) gameLoop(stream);
} else if (key === 'w' && gameState.paddleLeft > 0) {
gameState.paddleLeft -= 1;
} else if (key === 's' && gameState.paddleLeft < gameState.rows - 6) {
gameState.paddleLeft += 1;
}
});
// Start game loop
gameLoop(stream);
}
// SSH server
new ssh2.Server({
hostKeys: [hostKey]
}, (client) => {
console.log('Client connected!');
client.on('authentication', (ctx) => {
// Accept any authentication
ctx.accept();
});
client.on('ready', () => {
console.log('Client authenticated!');
client.on('session', (accept, reject) => {
const session = accept();
session.on('pty', (accept, reject, info) => {
console.log('PTY requested');
if (info && info.rows && info.cols) {
gameState.rows = info.rows;
gameState.columns = info.cols;
}
accept();
});
session.on('shell', (accept, reject) => {
const stream = accept();
console.log('Shell requested');
stream.write('Welcome to Pong! Use W/S to move. ESC to pause. No commands allowed.\n\r');
// Start the game after a short delay to ensure the welcome message is displayed
setTimeout(() => startGame(stream), 1000);
});
session.on('exec', (accept, reject, info) => {
console.log('Exec requested');
const stream = accept();
stream.write('Welcome to Pong! Use W/S to move. ESC to pause. No commands allowed.\n\r');
// Start the game after a short delay to ensure the welcome message is displayed
setTimeout(() => startGame(stream), 1000);
});
session.on('window-change', (accept, reject, info) => {
if (info && info.rows && info.cols) {
gameState.rows = info.rows;
gameState.columns = info.cols;
}
});
});
});
}).listen(2244, '0.0.0.0', function() {
console.log('Pong SSH server listening on port 2244');
});