-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
624 lines (565 loc) · 14.4 KB
/
Game.java
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
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Game {
boolean showInfo = true;
// Audio
List<Audio> audiosShoot = new ArrayList<Audio>();
List<Audio> audiosExplosion = new ArrayList<Audio>();
List<Audio> audiosLoseLive = new ArrayList<Audio>();
// bullets
List<Bullet> bullets = new ArrayList<Bullet>();
Color bulletColor;
long timeOfLastProjectile = System.currentTimeMillis();
int timeDelayBullet = 50;
int speedBullets = 10;
int bulletPower = 1;
// Aliens
List<Alien> aliens = new ArrayList<Alien>();
List<Bullet> bulletsAliens = new ArrayList<Bullet>();
Color aliensColor;
int ShootingRate = 800;
int speedBulletsAlien = 5;
int speedAliens = 30;
int AliensLives = 3;
int AliensHeight = 30;
int AliensWidth = 15;
int numOfAliensPerColumn = 10;
int numOfAliensPerRow = 5;
int marginV = 8;
int marginH = 20;
int marginTop = 50;
int marginBottom = 50;
int initialX;
int initialY;
int totalMovesVertical;
// Ship
Color shipColor;
int speedShip = 4;
Ship ship;
long TimeShoot = 0;
int ShootingRateShip = 400;
// Wall
Color wallColor;
List<Wall> walls = new ArrayList<Wall>();
// StartButton;
int Sx;
int SW;
int Sy;
int SH;
// Explosions;
List<Explosion> explosions = new ArrayList<Explosion>();
// background stars
List<Star> stars = new ArrayList<Star>();
// Game
MaxScore MAXSCORE;
boolean NewMaximum = false;
boolean gameStart = false;
long TimeStart = 0;
int randZERO_ONE = 1;
int level = 0;
long NumOfFrames = 0;
int score = 0;
long durationOfLevel = 30000;
long timeOfLastLevel = 0;
int X = 50;
Graphics g;
Color textColor = new Color(255, 255, 255);
Color backgroundColor = new Color(0, 0, 0);
Color groundColor = new Color(98, 222, 109);
Window f;
Random r = new Random();
Game(Window f) {
this.f = f;
this.g = f.g;
}
void initialize() {
// maxscore
MAXSCORE = new MaxScore();
// Colors
bulletColor = new Color(248, 59, 58); // red
shipColor = new Color(83, 83, 241); // lila
aliensColor = new Color(219, 85, 221);
bulletColor = new Color(248, 59, 58);
wallColor = new Color(98, 222, 109);
ship = new Ship(X, f.HEIGHT / 2, 21, 52, speedShip, shipColor, 3, 2);
// Aliens position
initialX = 2 * (f.WIDTH - (AliensWidth + marginH) * numOfAliensPerRow) / 3 + 100;
initialY = (f.HEIGHT - (AliensHeight + marginV) * numOfAliensPerColumn) / 2 + 15;
// Start Button position
Sx = f.WIDTH / 2 - 150;
Sy = 3 * f.HEIGHT / 4 - 50;
SW = 350;
SH = 100;
// Wall
//
totalMovesVertical = initialY * 2 - 40;
}
void updateVarsOnLevelChange() {
ship.lives = 3;
bulletPower += 1;
AliensLives += 1;
timeDelayBullet -= 1;
speedShip += 1;
speedBullets += 1;
speedBulletsAlien += 1;
ShootingRate += -50;
if (ShootingRateShip > 50)
ShootingRate += -50;
if (level % 2 == 0) {
ship.numOfGuns += 1;
numOfAliensPerRow += 1;
}
}
boolean gameOver() {
if (ship.lives <= 0) {
gameStart = false;
if (score >= MAXSCORE.score) {
NewMaximum = true;
MAXSCORE.score = score;
MAXSCORE.write();
}
return true;
}
return false;
}
void start(int x, int y) {
if ((x >= Sx && x <= Sx + SW) && (y >= Sy && y <= Sy + SH)) {
gameStart = true;
}
}
void GenerateWall() {
walls.add(new Wall(X + 70, 100, 5, 6, 20, wallColor));
walls.add(new Wall(X + 70, 250, 5, 6, 20, wallColor));
walls.add(new Wall(X + 70, 450, 5, 6, 20, wallColor));
}
void GenerateStars() {
List<Color> color = new ArrayList<Color>();
color.add(new Color(255, 255, 255));
color.add(new Color(168, 123, 255));
color.add(new Color(255, 250, 134));
color.add(new Color(202, 216, 255));
for (int i = 0; i < 40; i++) {
int W = Math.abs(r.nextInt()) % (f.WIDTH);
int H = Math.abs(r.nextInt()) % (f.HEIGHT - 50) + 50;
int S1 = Math.abs(r.nextInt()) % (7) + 1;
int S2 = Math.abs(r.nextInt()) % (6) + 1;
int c = Math.abs(r.nextInt()) % 4;
stars.add(new Star(W, H, S1, S2, 1, color.get(c)));
}
}
void GenerateAliens() {
for (int i = 0; i < numOfAliensPerColumn; i++) {
for (int j = 0; j < numOfAliensPerRow; j++) {
aliens.add(new Alien(initialX + j * (AliensWidth + marginH), initialY + i * (AliensHeight + marginV),
AliensWidth, AliensHeight, speedAliens, aliensColor, AliensLives, totalMovesVertical));
}
}
}
void shoot() {
long timeNow = System.currentTimeMillis();
long time = timeNow - TimeShoot;
if (time < 0 || time > ShootingRateShip) {
audiosShoot.add(new Audio(1));
TimeShoot = timeNow;
ship.shoot(bullets, speedBullets, bulletColor);
}
}
void aliensShoot() {
long timeNow = System.currentTimeMillis();
long time = timeNow - TimeStart;
if (time < 0 || time > ShootingRate) {
TimeStart = timeNow;
if (aliens.size() > 0) {
int aux = Math.abs(r.nextInt()) % (aliens.size());
Alien al = aliens.get(aux);
bulletsAliens.add(
new Bullet(al.x + al.width, al.y + ship.height / 2, 8, 4, speedBulletsAlien, (Color.ORANGE)));
}
}
}
void deleteExplosion() {
List<Explosion> found = new ArrayList<Explosion>();
for (Explosion explosion : explosions) {
if (explosion.over()) {
found.add(explosion);
}
}
explosions.removeAll(found);
}
void randomMove() {
long timeNow = System.currentTimeMillis();
long time = timeNow - TimeStart;
if (time < 0 || time > 200) {
shoot();
TimeStart = timeNow;
randZERO_ONE = Math.abs(r.nextInt()) % 2 - 1;
if (randZERO_ONE == 0) {
randZERO_ONE = 1;
}
}
aliensShoot();
moveShip(randZERO_ONE);
checkaudios();
moves();
impacts();
}
void frame() {
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void restart() {
ship.numOfGuns = 1;
ship.lives = 3;
AliensLives = 1;
score = 0;
NewMaximum = false;
level = 0;
bulletPower = 1;
timeDelayBullet = 50;
speedShip = 4;
speedBullets = 10;
speedBulletsAlien = 5;
ShootingRate = 800;
aliens.clear();
bullets.clear();
bulletsAliens.clear();
ForwardAnimation();
GenerateWall();
GenerateAliens();
}
void ForwardAnimation() {
int initialSpeed = stars.get(0).speed;
for (Star s : stars) {
s.speed = 30;
}
long InitTime = System.currentTimeMillis();
long now = System.currentTimeMillis();
while (now - InitTime < 2500) {
now = System.currentTimeMillis();
moveStars();
moveBullets();
moveWall();
repaintAnimation();
f.repaint();
frame();
}
for (Star s : stars) {
s.speed = initialSpeed;
}
bullets.clear();
walls.clear();
}
void run() {
initialize();
GenerateAliens();
GenerateWall();
GenerateStars();
while (!gameStart) {
ship.numOfGuns = 2;
rePaintStart();
f.repaint();
randomMove();
frame();
}
while (gameStart) {
restart();
while (!gameOver()) {
if (aliens.size() == 0) {
level++;
bullets.clear();
bulletsAliens.clear();
ForwardAnimation();
updateVarsOnLevelChange();
GenerateAliens();
GenerateWall();
}
impacts();
aliensShoot();
moves();
rePaint();
f.repaint();
frame();
}
while (!gameStart) {
rePaintEND();
f.repaint();
frame();
}
}
}
// Moves
void moveShip(int k) {
ship.move(k);
if (ship.IsOutOfRange(f.WIDTH, f.HEIGHT)) {
ship.move(-k);
}
}
void moves() {
moveAliens();
moveBullets();
moveStars();
}
// IMPACTS
void impacts() {
wallImpacts();
impactAlien();
ship.impactShip(bulletsAliens, audiosLoseLive);
impactBullets();
deleteExplosion();
}
// PAINTING
void rePaintStart() {
g.setFont(f.BigFont);
g.setColor(backgroundColor);
g.fillRect(0, 0, f.WIDTH, f.HEIGHT);
for (Star star : stars) {
star.paintStar(g);
}
ship.paintShip(g);
for (Bullet bullet : bullets) {
bullet.paint(g);
}
for (Bullet bullet : bulletsAliens) {
bullet.paint(g);
}
for (Alien alien : aliens) {
alien.paintAlien(g);
}
for (Wall w : walls) {
w.paint(g);
}
for (Explosion explosion : explosions) {
explosion.paint(g);
}
g.setColor(new Color(66, 233, 244));
g.drawString("SPACE INVADERS", f.WIDTH / 2 - 350, f.HEIGHT / 2);
g.setColor(new Color(235, 223, 100));
g.drawString("START", Sx, Sy + 100);
}
void rePaintEND() {
g.setFont(f.BigFont);
g.setColor(backgroundColor);
g.fillRect(0, 0, f.WIDTH, f.HEIGHT);
for (Star star : stars) {
star.paintStar(g);
}
g.setColor(textColor);
ship.paintShip(g);
for (int i = 0; i < bullets.size(); i++)
bullets.get(i).paint(g);
for (int i = 0; i < aliens.size(); i++)
aliens.get(i).paintAlien(g);
g.setColor(new Color(66, 233, 244));
g.drawString("SPACE INVADERS", f.WIDTH / 2 - 380, f.HEIGHT / 2 - 100);
g.setColor(new Color(235, 223, 100));
g.drawString("GAME OVER ", f.WIDTH / 2 - 250, f.HEIGHT / 2);
if (NewMaximum) {
g.setFont(f.smallFont);
g.setColor(new Color(83, 83, 241));
g.drawString("NEW MAX SCORE: " + MAXSCORE.score, f.WIDTH / 2 - 250 + 100, f.HEIGHT / 2 + 100);
}
// g.fillRect(Sx, Sy, SW, SH);
g.setFont(f.MediumFont);
g.setColor(new Color(235, 223, 100));
g.drawString("PLAY AGAIN", Sx, Sy + 100);
if (showInfo) {
g.setFont(f.smallFont);
g.setColor(textColor);
int x = f.WIDTH - 150;
g.drawString("SCORE: " + score, x, 400);
g.drawString("LEVEL: " + (level + 1), x, 450);
g.drawString("LIVES : " + ship.lives, x, 500);
g.setFont(f.TinyFont);
g.drawString("MAX SCORE: " + MAXSCORE.score, x, 550);
}
}
void rePaint() {
g.setFont(f.smallFont);
g.setColor(backgroundColor);
g.fillRect(0, 0, f.WIDTH, f.HEIGHT);
for (Star star : stars) {
star.paintStar(g);
}
g.setColor(new Color(66, 233, 244));
g.drawString("SPACE INVADERS", 550, 100);
if (showInfo) {
g.setColor(textColor);
int x = f.WIDTH - 150;
g.drawString("SCORE: " + score, x, 400);
g.drawString("LEVEL: " + (level + 1), x, 450);
g.drawString("LIVES : " + ship.lives, x, 500);
g.setFont(f.TinyFont);
g.drawString("max score: " + MAXSCORE.score, x, 550);
}
ship.paintShip(g);
for (Bullet bullet : bullets) {
bullet.paint(g);
}
for (Bullet bullet : bulletsAliens) {
bullet.paint(g);
}
for (Alien alien : aliens) {
alien.paintAlien(g);
}
for (Wall wall : walls) {
wall.paint(g);
}
for (Explosion explosion : explosions) {
explosion.paint(g);
}
}
void repaintAnimation() {
g.setFont(f.smallFont);
g.setColor(backgroundColor);
g.fillRect(0, 0, f.WIDTH, f.HEIGHT);
for (Star star : stars) {
star.paintStar(g);
}
g.setColor(new Color(66, 233, 244));
g.drawString("SPACE INVADERS", 550, 100);
g.setFont(f.BigFont);
g.setColor(new Color(235, 223, 100));
g.drawString("LEVEL " + (level + 1), f.WIDTH / 2 - 200, f.HEIGHT / 2);
if (showInfo) {
g.setFont(f.smallFont);
g.setColor(textColor);
int x = f.WIDTH - 150;
g.drawString("SCORE: " + score, x, 400);
g.drawString("LEVEL: " + (level + 1), x, 450);
g.drawString("LIVES : " + ship.lives, x, 500);
g.setFont(f.TinyFont);
g.drawString("MAX SCORE: " + MAXSCORE.score, x, 550);
}
ship.paintShip(g);
for (Bullet bullet : bullets) {
bullet.paint(g);
}
for (Bullet bullet : bulletsAliens) {
bullet.paint(g);
}
for (Wall wall : walls) {
wall.paint(g);
}
}
void checkaudios() {
List<Audio> found = new ArrayList<Audio>();
for (Audio audio : audiosShoot) {
if (audio.close()) {
found.add(audio);
}
}
audiosShoot.removeAll(found);
found.clear();
for (Audio audio : audiosExplosion) {
if (audio.close()) {
found.add(audio);
}
}
audiosExplosion.removeAll(found);
found.clear();
for (Audio audio : audiosLoseLive) {
if (audio.close()) {
found.add(audio);
}
}
audiosLoseLive.removeAll(found);
}
void moveAliens() {
for (int i = 0; i < aliens.size(); i++) {
if (aliens.get(i).moves > totalMovesVertical) {
aliens.get(i).moves = 0;
aliens.get(i).direction *= -1;
aliens.get(i).move(0);
}
aliens.get(i).moveVertical();
if (aliens.get(i).x < X + 90) {
aliens.remove(i);
loseLive();
}
}
}
void loseLive() {
ship.lives--;
audiosLoseLive.add(new Audio(3));
}
void moveBullets() {
List<Bullet> OUT = new ArrayList<Bullet>();
for (Bullet bullet : bullets) {
bullet.move(1);
if (bullet.IsOutOfRange(f.WIDTH, f.HEIGHT)) {
OUT.add(bullet);
}
}
bullets.removeAll(OUT);
OUT.clear();
for (Bullet bullet : bulletsAliens) {
bullet.move(-1);
if (bullet.IsOutOfRange(f.WIDTH, f.HEIGHT)) {
OUT.add(bullet);
}
}
bulletsAliens.removeAll(OUT);
}
void moveStars() {
for (Star s : stars) {
s.move(f.WIDTH);
}
}
void moveWall() {
for (Wall wall : walls) {
wall.move(0);
}
}
void explosion(int x, int y, boolean audio) {
explosions.add(new Explosion(x, y, 30, 30, 0, Color.RED, System.currentTimeMillis()));
if (audio) {
audiosExplosion.add(new Audio(2));
}
}
void wallImpacts() {
for (int i = 0; i < walls.size(); i++) {
if (walls.get(i).bricks.size() > 0) {
List<Brick> foundBrick = new ArrayList<Brick>();
for (Brick brick : walls.get(i).bricks) {
if (brick.impact(bullets)) {
foundBrick.add(brick);
explosion(brick.x, brick.y, false);
} else if (brick.impact(bulletsAliens)) {
foundBrick.add(brick);
explosion(brick.x, brick.y, false);
}
}
walls.get(i).bricks.removeAll(foundBrick);
}
}
}
void impactAlien() {
if (aliens.size() > 0) {
List<Alien> foundB = new ArrayList<Alien>();
for (Alien alien : aliens) {
if (alien.impactAlien(bullets)) {
foundB.add(alien);
score++;
explosion(alien.x + (alien.width / 2), alien.y + (alien.height / 2), true);
}
}
aliens.removeAll(foundB);
}
}
void impactBullets() {
List<Bullet> foundB = new ArrayList<Bullet>();
for (Bullet bullet : bullets) {
if (bullet.impact(bulletsAliens)) {
explosions.add(new Explosion(bullet.x, bullet.y, 10, 10, 0, Color.RED, System.currentTimeMillis()));
foundB.add(bullet);
}
}
bullets.removeAll(foundB);
}
}