Repository navigation
Expand file tree
/
Copy pathDistroInvaders.java
More file actions
496 lines (418 loc) · 18.8 KB
/
Copy pathDistroInvaders.java
File metadata and controls
496 lines (418 loc) · 18.8 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
import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class DistroInvaders extends JPanel implements ActionListener, KeyListener {
private static final int LOGICAL_WIDTH = 800;
private static final int LOGICAL_HEIGHT = 600;
private static final int PLAYER_SIZE = 48;
private static final long SHOT_COOLDOWN_MS = 150;
enum GameState { MENU, PLAYING, HOW_TO, CREATORS }
private GameState currentState = GameState.MENU;
enum Language { TR, EN }
private Language currentLang = Language.EN;
private int menuSelection = 0;
enum DistroType {
UBUNTU("Ubuntu", new Color(233, 84, 32), "ubuntu.png"),
FEDORA("Fedora", new Color(60, 110, 180), "fedora.png"),
DEBIAN("Debian", new Color(215, 7, 81), "debian.png"),
MANJARO("Manjaro", new Color(53, 191, 92), "manjaro.png"),
WINDOWS("Windows", new Color(0, 164, 239), "windows.png");
final String label;
final Color color;
final String fileName;
DistroType(String label, Color color, String fileName) {
this.label = label;
this.color = color;
this.fileName = fileName;
}
}
static class Entity {
double x, y;
int width, height;
Entity(double x, double y, int width, int height) {
this.x = x; this.y = y; this.width = width; this.height = height;
}
Rectangle getBounds() {
return new Rectangle((int) x, (int) y, width, height);
}
}
static class Enemy extends Entity {
DistroType type;
double speed;
Enemy(double x, double y, DistroType type, double speed) {
super(x, y, 46, 46);
this.type = type;
this.speed = speed;
}
@Override
Rectangle getBounds() {
return new Rectangle((int) x + 5, (int) y + 5, width - 10, height - 10);
}
}
static class Star {
int x, y, speed, size;
Star(int x, int y, int speed, int size) {
this.x = x; this.y = y; this.speed = speed; this.size = size;
}
}
private int playerX = 376;
private final int playerY = 500;
private int score = 0;
private int purgedCount = 0;
private boolean gameOver = false;
private long lastShotTime = 0;
private long gameStartTime = 0;
private long elapsedTimeSeconds = 0;
private final Timer gameLoop;
private final Random random = new Random();
private final Map<String, BufferedImage> sprites = new HashMap<>();
private BufferedImage crtOverlay;
private Clip bgmClip;
private final List<Enemy> enemies = new ArrayList<>();
private final List<Entity> lasers = new ArrayList<>();
private final List<Star> stars = new ArrayList<>();
public DistroInvaders() {
setBackground(new Color(8, 10, 15));
setFocusable(true);
addKeyListener(this);
loadSprites();
initStars();
initCRTOverlay();
playMusic("bgm.wav");
gameLoop = new Timer(16, this);
gameLoop.start();
}
private String getText(String tr, String en) {
return currentLang == Language.TR ? tr : en;
}
private String[] getMenuOptions() {
if (currentLang == Language.TR) {
return new String[] {
"[ KOK_ERISIMI ] - OYUNU BASLAT",
"[ MAN_SAYFASI ] - NASIL OYNANIR",
"[ SIS_YONETICILERI ] - GELISTIRICILER",
"[ KAPANIS ] - SISTEMDEN CIKIS"
};
} else {
return new String[] {
"[ ROOT_ACCESS ] - INITIALIZE PLAY",
"[ MAN_PAGE ] - HOW TO PLAY",
"[ SYS_ADMINS ] - CREATORS",
"[ SHUTDOWN ] - EXIT SYSTEM"
};
}
}
private void playMusic(String filename) {
try {
File file = new File(filename);
if (!file.exists()) return;
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
bgmClip = AudioSystem.getClip();
bgmClip.open(audioStream);
if (bgmClip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volumeControl = (FloatControl) bgmClip.getControl(FloatControl.Type.MASTER_GAIN);
volumeControl.setValue(-8.0f);
}
bgmClip.loop(Clip.LOOP_CONTINUOUSLY);
bgmClip.start();
} catch (Exception ignored) {}
}
private void loadSprites() {
loadTexture("arch", "arch.png");
for (DistroType type : DistroType.values()) {
loadTexture(type.name().toLowerCase(), type.fileName);
}
}
private void loadTexture(String key, String filename) {
try {
File file = new File(filename);
if (file.exists()) sprites.put(key, ImageIO.read(file));
} catch (IOException ignored) {}
}
private void initStars() {
for (int i = 0; i < 90; i++) {
stars.add(new Star(random.nextInt(LOGICAL_WIDTH), random.nextInt(LOGICAL_HEIGHT), random.nextInt(4) + 1, random.nextInt(2) + 1));
}
}
private void initCRTOverlay() {
crtOverlay = new BufferedImage(LOGICAL_WIDTH, LOGICAL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = crtOverlay.createGraphics();
g2.setColor(new Color(0, 0, 0, 45));
for (int i = 0; i < LOGICAL_HEIGHT; i += 3) g2.drawLine(0, i, LOGICAL_WIDTH, i);
RadialGradientPaint vignette = new RadialGradientPaint(
new Point(LOGICAL_WIDTH / 2, LOGICAL_HEIGHT / 2),
LOGICAL_WIDTH - 50,
new float[] {0.0f, 1.0f},
new Color[] {new Color(0, 0, 0, 0), new Color(0, 0, 0, 190)}
);
g2.setPaint(vignette);
g2.fillRect(0, 0, LOGICAL_WIDTH, LOGICAL_HEIGHT);
g2.dispose();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
double scaleX = (double) getWidth() / LOGICAL_WIDTH;
double scaleY = (double) getHeight() / LOGICAL_HEIGHT;
g2.scale(scaleX, scaleY);
g2.setColor(new Color(255, 255, 255, 120));
for (Star s : stars) {
g2.fillRect(s.x, s.y, s.size, s.size);
}
if (currentState == GameState.MENU) {
drawMenu(g2);
} else if (currentState == GameState.HOW_TO) {
drawHowTo(g2);
} else if (currentState == GameState.CREATORS) {
drawCreators(g2);
} else if (currentState == GameState.PLAYING) {
drawGame(g2);
}
if (crtOverlay != null) {
g2.drawImage(crtOverlay, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync();
}
private void drawMenu(Graphics2D g2) {
g2.setFont(new Font("Monospaced", Font.BOLD, 52));
g2.setColor(new Color(0, 255, 200));
String title = "DISTRO INVADERS";
FontMetrics fm = g2.getFontMetrics();
g2.drawString(title, (LOGICAL_WIDTH - fm.stringWidth(title)) / 2, 180);
g2.setFont(new Font("Monospaced", Font.BOLD, 20));
String[] menuOptions = getMenuOptions();
for (int i = 0; i < menuOptions.length; i++) {
if (i == menuSelection) {
g2.setColor(new Color(255, 60, 60));
g2.drawString("> " + menuOptions[i] + " <", 200, 300 + (i * 45));
} else {
g2.setColor(new Color(0, 255, 128));
g2.drawString(" " + menuOptions[i], 200, 300 + (i * 45));
}
}
// Dil değiştirme ipucu
g2.setFont(new Font("Monospaced", Font.PLAIN, 14));
g2.setColor(new Color(180, 180, 180));
String langHint = getText("[L] Dili Degistir (TR/EN)", "[L] Change Language (TR/EN)");
g2.drawString(langHint, (LOGICAL_WIDTH - g2.getFontMetrics().stringWidth(langHint)) / 2, 520);
}
private void drawHowTo(Graphics2D g2) {
g2.setColor(new Color(0, 0, 0, 180));
g2.fillRect(0, 0, LOGICAL_WIDTH, LOGICAL_HEIGHT);
g2.setFont(new Font("Monospaced", Font.BOLD, 36));
g2.setColor(new Color(0, 255, 200));
g2.drawString(getText("MAN_PAGE: KONTROLLER VE AMAC", "MAN_PAGE: CONTROLS AND OBJECTIVE"), 80, 120);
g2.setFont(new Font("Monospaced", Font.PLAIN, 18));
g2.setColor(new Color(0, 255, 128));
g2.drawString(getText(">> YON TUSLARI (SOL/SAG) : Sistemi (Arch) hareket ettir.",
">> ARROW KEYS (LEFT/RIGHT) : Move the system (Arch)."), 80, 200);
g2.drawString(getText(">> BOSLUK (SPACE) : Terminal komutu (Lazer) atesle.",
">> SPACEBAR : Fire terminal command (Laser)."), 80, 240);
g2.drawString(getText(">> SISTEMIN AMACI : Gelen diger distrolari (Ubuntu,",
">> SYSTEM OBJECTIVE : Destroy incoming distros"), 80, 290);
g2.drawString(getText(" Windows, vs.) sisteme sızmadan yok et.",
" before they infiltrate the system."), 80, 315);
g2.drawString(getText(">> KERNEL PANIC : Eger bir distro sana carparsa,",
">> KERNEL PANIC : If a distro hits your system,"), 80, 360);
g2.drawString(getText(" sistem çöker (Game Over).",
" the system crashes (Game Over)."), 80, 385);
g2.setColor(new Color(180, 180, 180));
g2.drawString(getText("[ ESC ] -> GERI DON", "[ ESC ] -> GO BACK"), 80, 500);
}
private void drawCreators(Graphics2D g2) {
g2.setColor(new Color(0, 0, 0, 180));
g2.fillRect(0, 0, LOGICAL_WIDTH, LOGICAL_HEIGHT);
g2.setFont(new Font("Monospaced", Font.BOLD, 36));
g2.setColor(new Color(0, 255, 200));
g2.drawString(getText("SYS_ADMINS: GELISTIRICILER", "SYS_ADMINS: DEVELOPERS"), 80, 120);
g2.setFont(new Font("Monospaced", Font.PLAIN, 20));
g2.setColor(new Color(0, 255, 128));
g2.drawString(getText(">> Lider Gelistirici & Mimar : Siratilotty",
">> Lead Developer & Architect: SirAtilotty"), 80, 220);
g2.drawString(getText(">> Cekirdek Mantik & Mimari : SirAtilotty",
">> Core Logic & Architecture : SirAtilotty"), 80, 280);
g2.setColor(new Color(180, 180, 180));
g2.drawString(getText("[ ESC ] -> GERI DON", "[ ESC ] -> GO BACK"), 80, 500);
}
private void drawGame(Graphics2D g2) {
if (!gameOver) {
g2.setColor(new Color(0, 255, 200));
for (Entity laser : lasers) {
g2.fillRect((int) laser.x, (int) laser.y, laser.width, laser.height);
}
BufferedImage archImg = sprites.get("arch");
if (archImg != null) g2.drawImage(archImg, playerX, playerY, PLAYER_SIZE, PLAYER_SIZE, null);
else drawFallbackArch(g2, playerX, playerY);
for (Enemy e : enemies) {
BufferedImage sprite = sprites.get(e.type.name().toLowerCase());
if (sprite != null) g2.drawImage(sprite, (int) e.x, (int) e.y, e.width, e.height, null);
else drawFallbackDistro(g2, e);
}
g2.setFont(new Font("Monospaced", Font.BOLD, 14));
g2.setColor(new Color(0, 255, 128));
double currentSpeedMultiplier = 1.0 + (elapsedTimeSeconds * 0.04);
// HUD Dinamik Çeviri
String hudFormat = getText("> SKOR: %05d | TEMIZLENEN: %03d | SURE: %02ds | CARPAN: %.1fx",
"> SCORE: %05d | PURGED: %03d | TIME: %02ds | MULT: %.1fx");
g2.drawString(String.format(hudFormat, score, purgedCount, elapsedTimeSeconds, currentSpeedMultiplier), 15, 25);
} else {
g2.setColor(new Color(0, 0, 0, 220));
g2.fillRect(0, 0, LOGICAL_WIDTH, LOGICAL_HEIGHT);
g2.setFont(new Font("Monospaced", Font.BOLD, 20));
g2.setColor(new Color(255, 60, 60));
g2.drawString(getText("[!] KERNEL PANIC: SISTEM COKTU", "[!] KERNEL PANIC: SYSTEM OVERWHELMED"), 100, 240);
g2.setColor(new Color(0, 255, 128));
g2.setFont(new Font("Monospaced", Font.PLAIN, 15));
g2.drawString(getText("Final Skoru : ", "Final Score : ") + score, 100, 300);
g2.drawString(getText("Temizlenen Distro Sayisi : ", "Unsupported Distros Purged : ") + purgedCount, 100, 325);
g2.drawString(getText("Calisma Suresi (Uptime) : ", "Uptime : ") + elapsedTimeSeconds + "s", 100, 350);
g2.setColor(new Color(180, 180, 180));
g2.drawString(getText("> Hard Reboot Icin 'R'ye Bas", "> Press 'R' to Hard Reboot System"), 100, 410);
g2.drawString(getText("> Ana Menu Icin 'ESC'ye Bas", "> Press 'ESC' to Main Menu"), 100, 440);
}
}
private void drawFallbackArch(Graphics2D g2, int x, int y) {
int[] px = {x + 24, x + 48, x};
int[] py = {y, y + 48, y + 48};
g2.setColor(new Color(23, 147, 209));
g2.fillPolygon(px, py, 3);
}
private void drawFallbackDistro(Graphics2D g2, Enemy e) {
g2.setColor(e.type.color);
g2.fillRect((int) e.x, (int) e.y, e.width, e.height);
}
@Override
public void actionPerformed(ActionEvent e) {
for (Star s : stars) {
s.y += s.speed;
if (s.y > LOGICAL_HEIGHT) {
s.y = 0;
s.x = random.nextInt(LOGICAL_WIDTH);
}
}
if (currentState != GameState.PLAYING || gameOver) {
repaint();
return;
}
elapsedTimeSeconds = (System.currentTimeMillis() - gameStartTime) / 1000;
double speedMultiplier = Math.min(3.5, 1.0 + (elapsedTimeSeconds * 0.04));
double baseSpeed = 2.5 + random.nextDouble() * 1.5;
double currentEnemySpeed = baseSpeed * speedMultiplier;
int spawnRate = Math.min(42, 6 + (int)(elapsedTimeSeconds / 3));
if (random.nextInt(100) < spawnRate) {
DistroType[] types = DistroType.values();
DistroType type = types[random.nextInt(types.length)];
enemies.add(new Enemy(random.nextInt(LOGICAL_WIDTH - 50), -50, type, currentEnemySpeed));
}
for (int i = lasers.size() - 1; i >= 0; i--) {
Entity l = lasers.get(i);
l.y -= 16;
if (l.y < -20) lasers.remove(i);
}
Rectangle playerBox = new Rectangle(playerX + 5, playerY + 5, PLAYER_SIZE - 10, PLAYER_SIZE - 10);
for (int i = enemies.size() - 1; i >= 0; i--) {
Enemy enemy = enemies.get(i);
enemy.y += enemy.speed;
Rectangle enemyBox = enemy.getBounds();
boolean hit = false;
for (int j = lasers.size() - 1; j >= 0; j--) {
if (enemyBox.intersects(lasers.get(j).getBounds())) {
lasers.remove(j);
hit = true;
break;
}
}
if (hit) {
enemies.remove(i);
score += 25;
purgedCount++;
continue;
}
if (enemy.y > LOGICAL_HEIGHT) {
enemies.remove(i);
score += 5;
} else if (enemyBox.intersects(playerBox)) {
gameOver = true;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (currentState == GameState.MENU) {
if (key == KeyEvent.VK_UP) {
menuSelection--;
if (menuSelection < 0) menuSelection = getMenuOptions().length - 1;
} else if (key == KeyEvent.VK_DOWN) {
menuSelection++;
if (menuSelection >= getMenuOptions().length) menuSelection = 0;
} else if (key == KeyEvent.VK_L) {
currentLang = (currentLang == Language.TR) ? Language.EN : Language.TR;
} else if (key == KeyEvent.VK_ENTER || key == KeyEvent.VK_SPACE) {
if (menuSelection == 0) resetGame();
else if (menuSelection == 1) currentState = GameState.HOW_TO;
else if (menuSelection == 2) currentState = GameState.CREATORS;
else if (menuSelection == 3) System.exit(0);
}
} else if (currentState == GameState.HOW_TO || currentState == GameState.CREATORS) {
if (key == KeyEvent.VK_ESCAPE) {
currentState = GameState.MENU;
}
} else if (currentState == GameState.PLAYING) {
if (key == KeyEvent.VK_ESCAPE) {
if (gameOver) currentState = GameState.MENU;
else System.exit(0);
}
if (!gameOver) {
if (key == KeyEvent.VK_LEFT && playerX > 5) playerX -= 26;
else if (key == KeyEvent.VK_RIGHT && playerX < LOGICAL_WIDTH - PLAYER_SIZE - 5) playerX += 26;
else if (key == KeyEvent.VK_SPACE) {
long now = System.currentTimeMillis();
if (now - lastShotTime >= SHOT_COOLDOWN_MS) {
lasers.add(new Entity(playerX + PLAYER_SIZE / 2.0 - 2, playerY - 10, 4, 20));
lastShotTime = now;
}
}
} else if (key == KeyEvent.VK_R) {
resetGame();
}
}
}
private void resetGame() {
playerX = 376;
score = 0;
purgedCount = 0;
enemies.clear();
lasers.clear();
gameOver = false;
gameStartTime = System.currentTimeMillis();
elapsedTimeSeconds = 0;
currentState = GameState.PLAYING;
}
@Override public void keyTyped(KeyEvent e) {}
@Override public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Distro Invaders");
frame.setUndecorated(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
DistroInvaders game = new DistroInvaders();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}