-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
298 lines (262 loc) · 8.41 KB
/
game.js
File metadata and controls
298 lines (262 loc) · 8.41 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
#!/usr/bin/env node
const readline = require('readline');
const { getHighScore, saveHighScore } = require('./engine');
// GAME THEME COLORS
const YELLOW = "\x1b[33m";
const RESET = "\x1b[0m";
const BOLD = "\x1b[1m";
const RED_BG = "\x1b[41m";
const GREEN = "\x1b[32m";
const CYAN = "\x1b[36m";
const CYAN_BG = "\x1b[46m";
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
const WIDTH = (process.stdout.columns || 30) - 2;
const HEIGHT = (process.stdout.rows - 5) || 15;
// GAME VARIABLES
let playerX = Math.floor(WIDTH / 2);
let bullets = [];
let enemies = [];
let items = [];
let score = 0;
let playerHP = 3;
let highScore = getHighScore();
let boss = null;
let state = "MENU";
let prevState = null;
let currentMenu = null;
let selectedIndex = 0;
let menuHandler = null;
let menuTitle = "";
// STATE TRANSITION
function setState(newState) {
state = newState;
if (newState === "MENU") {
openMenu("WELCOME TO TERM-INVADER!", ["Start Game", "High Score", "Controls", "Quit"], menuActions.STARTUP);
} else if (newState === "PAUSED") {
openMenu("GAME PAUSED", ["Resume", "Restart", "Controls", "Quit"], menuActions.PAUSED);
}
}
// SHOW MENU
function showMenu(title, options, selected = 0) {
let screen = "\x1b[H\x1b[J";
screen += `${CYAN}${BOLD} ${title} ${RESET}\n\n`;
options.forEach((opt, i) => {
if (i === selected) screen += `${BOLD}${GREEN}> ${opt}${RESET}\n`;
else screen += ` ${opt}\n`;
});
screen += `\n${YELLOW}Use ↑/↓ to navigate, Enter to select${RESET}\n`;
process.stdout.write(screen);
}
// OPEN MENU
function openMenu(title, options, handler) {
menuTitle = title;
currentMenu = options;
selectedIndex = 0;
menuHandler = handler;
showMenu(menuTitle, currentMenu, selectedIndex);
}
// MENU INPUT HANDLER
function handleMenuInput(key) {
if (!currentMenu || !menuHandler) return;
let moved = false;
if (key === '\u001b[A') {
selectedIndex = (selectedIndex - 1 + currentMenu.length) % currentMenu.length;
moved = true;
}
if (key === '\u001b[B') {
selectedIndex = (selectedIndex + 1) % currentMenu.length;
moved = true;
}
if (key === '\r') {
menuHandler(selectedIndex + 1);
return;
}
if (moved) showMenu(menuTitle, currentMenu, selectedIndex);
}
// STATE FUNCTIONS
function updateMenu() {
showMenu(menuTitle, currentMenu, selectedIndex);
}
// GAME PAUSED
function updatePause() {
let screen = "\x1b[H\x1b[J";
screen += `${CYAN_BG}${BOLD} ${menuTitle} ${RESET}\n\n`;
currentMenu.forEach((opt, i) => {
if (i === selectedIndex) screen += `${BOLD}${GREEN}> ${opt}${RESET}\n`;
else screen += ` ${opt}\n`;
});
screen += `\n${YELLOW}Use ↑/↓ to navigate, Enter to select${RESET}\n`;
process.stdout.write(screen);
}
function updateHighScore() {
highScore = getHighScore();
process.stdout.write("\x1b[2J\x1b[H");
process.stdout.write(`${CYAN}${BOLD} HIGH SCORE ${RESET}\n\n`);
process.stdout.write(`Current Best: ${YELLOW}${highScore}${RESET}\n\n`);
process.stdout.write(`${YELLOW}Press any key to return...${RESET}\n`);
}
function updateHelp() {
let help = "\x1b[H\x1b[J";
help += `${CYAN}${BOLD} CONTROLS ${RESET}\n\n`;
help += ` [▲] / W / Space : ${GREEN}Shoot${RESET}\n`;
help += ` [◀] / A : ${GREEN}Move Left${RESET}\n`;
help += ` [▶] / D : ${GREEN}Move Right${RESET}\n\n`;
help += `Grab [✚] for extra HP!\n`;
help += `${YELLOW}Press any key to return...${RESET}`;
process.stdout.write(help);
}
function updateGameOver() {
const isNewRecord = saveHighScore(score);
process.stdout.write("\x1b[2J\x1b[H");
let over = `\n${RED_BG}${BOLD} GAME OVER ${RESET}\n`;
over += `Score: ${score}\n`;
over += isNewRecord ? `${CYAN}🔥 NEW RECORD! 🔥${RESET}\n` : `Best: ${highScore}\n`;
over += `\n${YELLOW}Press R to Retry or Q to Quit${RESET}\n`;
process.stdout.write(over);
}
// GAME LOGIC
function updateGame() {
bullets.forEach(b => b.y--);
bullets = bullets.filter(b => b.y >= 0 && !b.hit);
if (Math.random() < 0.01 && items.length < 1) items.push({ x: Math.floor(Math.random() * (WIDTH-2)), y: 0 });
items.forEach((item, i) => {
item.y += 0.2;
if (Math.floor(item.y) === HEIGHT - 1 && Math.abs(item.x - playerX) <= 1) {
playerHP++;
items.splice(i, 1);
}
});
if (score > 0 && score % 1000 === 0 && !boss) {
boss = { x: Math.floor(WIDTH/2), y: 1, hp: 20, maxHp: 20, dir: 1 };
enemies = [];
}
if (boss) {
boss.x += boss.dir;
if (boss.x <= 1 || boss.x >= WIDTH - 4) boss.dir *= -1;
}
const spawnChance = boss ? 0.99 : 0.94;
const maxEnemies = boss ? 1 : 5;
if (Math.random() > spawnChance && enemies.length < maxEnemies) {
enemies.push({ x: Math.floor(Math.random() * (WIDTH-2))+1, y: 0, hp: 2, explode: false });
}
enemies.forEach((e, i) => {
if (e.hp > 0) {
e.y += 0.22;
if (Math.floor(e.y) >= HEIGHT) {
playerHP--;
enemies.splice(i, 1);
if (playerHP <= 0) state = "GAME_OVER";
}
} else {
if (e.explode) enemies.splice(i, 1);
else e.explode = true;
}
});
bullets.forEach((b) => {
if (boss && b.x >= boss.x && b.x <= boss.x + 2) {
if (Math.abs(b.y - boss.y) < 1.2) {
boss.hp--;
b.hit = true;
if (boss.hp <= 0) { boss = null; score += 1000; }
}
}
enemies.forEach((e) => {
if (e.hp > 0) {
if (Math.abs(b.x - e.x) <= 1 && Math.abs(b.y - e.y) < 1.2) {
e.hp--;
b.hit = true;
if (e.hp === 0) score += 100;
}
}
});
});
let frame = "\x1b[H\x1b[J";
frame += `${BOLD}TERM-INVADER${RESET} | Score: ${YELLOW}${score}${RESET} | ${CYAN}Best: ${highScore}${RESET} | HP: ${GREEN}${'❤️'.repeat(playerHP)}${RESET}\n`;
if (boss) {
const barWidth = 20;
const progress = Math.ceil((boss.hp / boss.maxHp) * barWidth);
frame += `${RED_BG} BOSS ${RESET} [${YELLOW}${'#'.repeat(progress)}${RESET}${'.'.repeat(barWidth - progress)}]\n`;
} else {
frame += "—".repeat(WIDTH) + "\n";
}
for (let y = 0; y < HEIGHT; y++) {
let line = Array(WIDTH).fill(' ');
bullets.forEach(b => { if (Math.floor(b.y) === y) line[b.x] = '↑'; });
items.forEach(item => { if (Math.floor(item.y) === y) line[item.x] = '[✚]'; });
if (boss && y === boss.y) { line[boss.x] = '🛸'; line[boss.x+1] = '👽'; line[boss.x+2] = '🛸'; }
enemies.forEach(e => {
if (Math.floor(e.y) === y) {
if (e.hp === 2) line[e.x] = '🛸';
else if (e.hp === 1) line[e.x] = '👾';
else line[e.x] = '💥';
}
});
if (y === HEIGHT - 1) line[playerX] = '🚀';
frame += line.join('') + "\n";
}
process.stdout.write(frame + "\n" + "—".repeat(WIDTH));
}
// STATE MAP
const states = {
MENU: updateMenu,
PLAYING: updateGame,
PAUSED: updatePause,
HELP: updateHelp,
GAME_OVER: updateGameOver,
HIGH_SCORE: updateHighScore
};
// GAME LOOP
setInterval(() => states[state](), 80);
// RESET GAME
function resetGame() {
playerX = Math.floor(WIDTH / 2);
bullets = [];
enemies = [];
items = [];
score = 0;
playerHP = 3;
boss = null;
setState("MENU");
}
// MENU ACTIONS
const menuActions = {
STARTUP: (choice) => {
if (choice === 1) state = "PLAYING";
if (choice === 2) { prevState = "MENU";
state = "HIGH_SCORE";}
if (choice === 3) { prevState = "MENU"; state = "HELP"; }
if (choice === 4) process.exit();
},
PAUSED: (choice) => {
if (choice === 1) state = "PLAYING";
if (choice === 2) resetGame();
if (choice === 3) { prevState = "PAUSED"; state = "HELP"; }
if (choice === 4) process.exit();
}
};
// INPUT HANDLERS
const inputHandlers = {
MENU: (key) => handleMenuInput(key),
PAUSED: (key) => handleMenuInput(key),
HELP: () => { state = prevState; },
HIGH_SCORE: () => { state = prevState; },
PLAYING: (key) => {
if (key.toLowerCase() === 'p') setState("PAUSED");
if ((key === '\u001b[D' || key === 'a') && playerX > 2) playerX -= 2;
if ((key === '\u001b[C' || key === 'd') && playerX < WIDTH - 3) playerX += 2;
if (key === '\u001b[A' || key === ' ' || key === 'w') bullets.push({ x: playerX, y: HEIGHT - 1, hit: false });
},
GAME_OVER: (key) => {
if (key.toLowerCase() === 'r') resetGame();
if (key.toLowerCase() === 'q') process.exit();
}
};
// INPUT LISTENER
process.stdin.on('data', (data) => {
const key = data.toString();
if (key === '\u0003') process.exit();
inputHandlers[state](key);
});
// STARTUP
setState("MENU");