-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsolute.java
More file actions
719 lines (641 loc) · 14.9 KB
/
Absolute.java
File metadata and controls
719 lines (641 loc) · 14.9 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Absolute Space
import java.awt.*;
import java.net.*;
import java.applet.Applet;
import java.applet.AudioClip;
public class Absolute extends Applet implements Runnable
{
Dimension d;
Font largefont = new Font("Helvetica", Font.PLAIN, 24);
Font smallfont = new Font("Helvetica", Font.PLAIN, 14);
FontMetrics fmsmall, fmlarge;
Graphics goff;
Image ii;
Thread thethread;
boolean ingame=false;
int x, y, mousex, mousey, oldx, oldy, dx=0, dy=0, count, shield=0;
boolean showtitle=true;
Image ship;
Image[] fire;
int firecnt=0;
// Bullet variables
Image bullet;
int[] bx;
int[] by;
final int bmy=16, bul_xs=54, bul_ys=8;
// Meteor variables
Image meteor;
int maxmet, metcount, mtotal, mrenew, metmy;
int[] metx;
int[] mety;
int[] metf;
boolean[] metr;
final int sxmet=80, symet=84;
// These are for the star field
public int starsX[];
public int starsY[];
public Color starsC[];
public int numStars = 30;
public int speed = 6, xSize, ySize;
// Variables for big boom
Image[] boom;
int rndbx, rndby, rndcnt=777;
final int sxbom=71, sybom=100, bframes=4;
// Global Variables
int distance=0, maxdist=2000;
int slevel, blevel, difflev, bosslevel;
int smax, bmax;
int scur, bcur, renew, rcnt=0, sstretch, txtalign=100;
long score;
// Sounds
AudioClip blast, crash, kill;
// Bosses here
// Sunbird
boolean sunbird, sbefore, safter;
int sbx, sby, sbmove, maxtribe, tribe;
int[] sbfx, sbfy;
final int maxshield=9;
final int backcol=0x102040;
final int fireframe=2;
final int borderwidth=0;
final int sxsize=90, sysize=39, sxfire=11, syfire=6;
final int movex=10, movey=5;
final int scoreheight=45;
final int screendelay=300;
public String getAppletInfo()
{
return("Absolute Space - by nicks707");
}
public void init()
{
Graphics g;
int n;
d = size();
setBackground(Color.black);
g=getGraphics();
g.setFont(smallfont);
fmsmall = g.getFontMetrics();
g.setFont(largefont);
fmlarge = g.getFontMetrics();
ship = getImage(getCodeBase(), "ship.gif");
bullet = getImage(getCodeBase(), "bullet.gif");
fire = new Image[fireframe];
for (n=0; n<fireframe; n++) {
fire[n] = getImage(getCodeBase(), "fire"+n+".gif");
}
boom = new Image[bframes+1];
for (n=0; n<=bframes; n++) {
boom[n] = getImage(getCodeBase(), "boom"+n+".gif");
}
xSize = d.width - borderwidth*2;
ySize = d.height - borderwidth*2 - scoreheight;
x = (xSize - sxsize) / 2;
y = ySize - sysize - scoreheight - borderwidth;
mousex = -1;
// will be override by command line parameters
blevel = 3;
slevel = 3;
bx = new int[blevel*10];
by = new int[blevel*10];
for (n=0; n<blevel*10; n++) {
bx[n] = -1;
}
// Meteor initiliaze
meteor = getImage(getCodeBase(), "meteor.gif");
maxmet = d.height / symet + 1;
maxmet = maxmet * 10;
metx = new int[maxmet];
mety = new int[maxmet];
metf = new int[maxmet];
metr = new boolean[maxmet];
// Audio
try {
blast = getAudioClip(new URL(getDocumentBase(), "blast.au"));
crash = getAudioClip(new URL(getDocumentBase(), "collisn.au"));
kill = getAudioClip(new URL(getDocumentBase(), "mdestr.au"));
}
catch (MalformedURLException e) {}
blast.play(); blast.stop();
crash.play(); crash.stop();
kill.play(); kill.stop();
initStars();
rndcnt = 777;
// Bosses
// Sunbird
sbfx = new int[11];
sbfy = new int[11];
sbfx[0] = 10;
sbfy[0] = 0;
sbfx[1] = 15;
sbfy[1] = 10;
sbfx[2] = 0;
sbfy[2] = 10;
sbfx[3] = 3;
sbfy[3] = 15;
sbfx[4] = 17;
sbfy[4] = 15;
sbfx[5] = 20;
sbfy[5] = 20;
sbfx[6] = 23;
sbfy[6] = 15;
sbfx[7] = 37;
sbfy[7] = 15;
sbfx[8] = 40;
sbfy[8] = 10;
sbfx[9] = 25;
sbfy[9] = 10;
sbfx[10] = 30;
sbfy[10] = 0;
}
// This creates the starfield in the background
public void initStars () {
starsX = new int[numStars];
starsY = new int[numStars];
starsC = new Color[numStars];
for (int i = 0; i < numStars; i++) {
starsX[i] = (int) ((Math.random() * xSize - 1) + 1);
starsY[i] = (int) ((Math.random() * ySize - 1) + 1);
starsC[i] = NewColor();
}
}
public boolean keyDown(Event e, int key)
{
if (ingame)
{
mousex = -1;
if (key == Event.LEFT)
dx=-1;
if (key == Event.RIGHT)
dx=1;
if (key == Event.UP)
dy=-1;
if (key == Event.DOWN)
dy=1;
if (key == ' ')
if (bcur>0) FireGun();
if (key == Event.ESCAPE)
ingame=false;
}
else
{
if (key == 's' || key == 'S')
{
ingame=true;
GameStart();
}
}
return true;
}
public boolean keyUp(Event e, int key)
{
System.out.println("Key: "+key);
if (key == Event.LEFT || key == Event.RIGHT)
dx=0;
if (key == Event.UP || key == Event.DOWN)
dy=0;
return true;
}
public void paint(Graphics g)
{
String s;
Graphics gg;
if (goff==null && d.width>0 && d.height>0)
{
ii = createImage(d.width, d.height);
goff = ii.getGraphics();
}
if (goff==null || ii==null)
return;
goff.setColor(Color.black);
goff.fillRect(0, 0, d.width, d.height);
if (ingame)
PlayGame();
else
ShowIntroScreen();
g.drawImage(ii, 0, 0, this);
}
public void PlayGame()
{
NewMeteor();
MoveShip();
DrawPlayField();
// Big bosses here
if (sunbird) SunBird();
ShowScore();
distance++;
score+=100;
if (distance % maxdist == 0) {
difflev++;
if (difflev>2 & difflev<10) {
renew-=20;
bmax+=1;
smax+=1;
metmy++;
mrenew--;
}
if (difflev>3 & difflev<11) {
maxtribe++;
sbmove++;
}
if (difflev>3) {
sunbird = true;
tribe = maxtribe;
}
}
// Renew Ship Energy
rcnt++;
if (rcnt % (renew / blevel) == 0) {
bcur++;
if (bcur>bmax) bcur=bmax;
}
if (distance % 500 == 0) {
scur++;
if (scur>smax) scur=smax;
}
if (rcnt>renew) rcnt=0;
}
public void ShowIntroScreen()
{
String s;
DrawPlayField();
goff.setFont(largefont);
if (rndcnt > bframes) {
rndbx = (int) (Math.random() * (xSize - sxbom) + 1);
rndby = (int) (Math.random() * (ySize - sybom) + 1);
rndcnt = 0;
}
goff.drawImage(boom[rndcnt], rndbx, rndby, this);
rndcnt++;
for (int i=0; i<xSize/bul_xs; i++) {
goff.drawImage(bullet, i*bul_xs, 0, this);
goff.drawImage(bullet, i*bul_xs, ySize-bul_ys, this);
}
if (showtitle)
{
goff.setColor(new Color(0xff0000));
s="Absolute Space";
goff.drawString(s,(d.width-fmlarge.stringWidth(s)) / 2, (d.height-scoreheight-borderwidth)/2 - 20);
goff.setColor(new Color(0xff00ff));
s="(c)2000 by Navjot Singh";
goff.setFont(smallfont);
goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 10);
s="navjot.usit@gmail.com";
goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 30);
}
else
{
goff.setFont(smallfont);
goff.setColor(new Color(0xffff00));
s="Leftclick to start game";
goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 - 10);
goff.setColor(new Color(0x00ff00));
s="Use cursor keys move, click or press SPACE to fire";
goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 20);
goff.setFont(largefont);
goff.setColor(new Color(0xff00ff));
s="LAST SCORE: "+score;
goff.drawString(s,(d.width-fmlarge.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 120);
}
count--;
if (count<=0)
{ count=screendelay; showtitle=!showtitle; }
}
public void DrawPlayField()
{
// Show stars
moveStars();
for (int a = 0; a < numStars; a++) {
goff.setColor(starsC[a]);
goff.drawRect(starsX[a], starsY[a], 1, 1);
}
ShowMeteors();
KillEmAll();
goff.drawImage(ship, x, y, this); // paint ship
if (firecnt != 0) {
goff.drawImage(fire[firecnt-1], x+( (sxsize-sxfire) / 2 ), y+sysize, this); // engine fire
}
firecnt++;
if (firecnt > 2) firecnt=0;
Collisions();
if (shield>0) {
goff.setColor(new Color(0x00ffff));
goff.drawOval(x-shield, y-shield, sxsize+shield*2, sysize+shield*2);
shield--;
}
}
public void ShowScore()
{
String s;
int my;
sstretch = (xSize-txtalign*2)/Math.max(bmax,smax);
// Laser bar
my = d.height-scoreheight+10;
goff.setColor(new Color(0x00ff96));
goff.drawRect(txtalign, my-10, bmax*sstretch, 10);
goff.setFont(smallfont);
s="laser: "+bcur+"/"+bmax;
goff.fillRect(txtalign, my-10, bcur*sstretch, 10);
goff.drawString(s,10,my);
// Shield bar
my += 15;
goff.setColor(new Color(0x00ffff));
goff.drawRect(txtalign, my-10, smax*sstretch, 10);
goff.setFont(smallfont);
s="shield: "+scur+"/"+smax;
goff.fillRect(txtalign, my-10, scur*sstretch, 10);
goff.drawString(s,10,my);
// Score
my += 20;
goff.setColor(new Color(0xffffff));
goff.setFont(largefont);
s="score: "+score;
goff.drawString(s,10,my);
}
public void MoveShip()
{
int xx, yy;
oldx = x;
oldy = y;
xx = mousex;
if (xx>0) {
yy = mousey;
if (xx<x) dx=-1;
if (xx>x+sxsize) dx=1;
if (yy<y) dy=-1;
if (yy>y+sysize) dy=1;
if (xx>x & xx<x+sxsize & yy>y & yy<y+sysize) {
dx = 0;
dy = 0;
mousex = -1;
}
}
x+=dx*movex;
y+=dy*movey;
if (y<=borderwidth || y>=(d.height-sysize-scoreheight))
{
dy=0;
y=oldy;
}
if (x>=(d.width-borderwidth-sxsize) || x<=borderwidth)
{
dx=0;
x=oldx;
}
}
public void FireGun()
{
int n=0, f=-1;
while (n<blevel*10 && bx[n]>=0) n++;
if (n<blevel*10) f = n;
if (f>=0) {
bx[f] = x+( (sxsize-bul_xs) / 2);
by[f] = y;
bcur--;
blast.play();
}
}
public void KillEmAll()
{
int f;
for (int n=0; n<blevel*10; n++) {
if (bx[n]>0) {
by[n] -= bmy;
if ( by[n] < borderwidth | MetHit(n) | BirdHit(bx[n], by[n]) ) {
bx[n] = -1;
} else {
goff.drawImage(bullet, bx[n], by[n], this); // paint bullet
}
}
}
}
public boolean MetHit(int f)
{
for (int n=0; n<maxmet; n++) {
if (metx[n]>=0) {
if (metr[n] & bx[f]+bul_xs>metx[n] & bx[f]<metx[n]+sxmet & by[f]+bul_ys>mety[n] & by[f]<mety[n]+symet) {
DelMeteor(n);
kill.play();
return true;
}
}
}
return false;
}
public void ShowMeteors()
{
int n;
mtotal = 0;
for (n=0; n<maxmet; n++) {
if (metx[n]>=0) {
mtotal++;
mety[n] += metmy;
if (mety[n] > d.height-borderwidth-scoreheight) {
DelMeteor(n);
} else {
if (metr[n]) {
goff.drawImage(meteor, metx[n], mety[n], this); // paint meteor
} else {
goff.drawImage(boom[bframes-metf[n]], metx[n]+(sxmet-sxbom)/2, mety[n]+(symet-sybom)/2, this); // paint boom
metf[n]--;
if (metf[n]<0) DelMeteor(n);
}
}
}
}
}
public void NewMeteor()
{
int n=0, f=-1;
metcount++;
if (metcount > mrenew/metmy) {
metcount = 0;
while (n<maxmet & metx[n]>=0) n++;
if (n<maxmet) f = n;
if (f>=0) {
metx[f] = (int) (Math.random() * (xSize - sxmet) + 1);
mety[f] = borderwidth-symet;
metr[f] = true;
metf[f] = bframes;
}
}
}
// If a star in the background reaches the bottome then it will go back to the top
public void moveStars () {
for (int i = 0; i < numStars; i++) {
if (starsY[i] + 1 > ySize - (speed * 2 )) {
starsY[i] = 0;
starsX[i] = (int) ((Math.random() * xSize - 1) + 1);
starsC[i] = NewColor();
}
else {
starsY[i] += speed;
}
}
}
public void Collisions()
{
for (int n=0; n<maxmet; n++) {
if (metx[n]>=0) {
if (metr[n] & x+sxsize>metx[n] & x<metx[n]+sxmet & y+sysize>mety[n] & y<mety[n]+symet) {
HitShip();
DelMeteor(n);
}
}
}
}
public void HitShip()
{
crash.play();
shield=maxshield;
scur--;
if (scur<0) GameOver();
}
public void DelMeteor(int n)
{
if (metr[n]) {
metr[n] = false;
metf[n] = bframes;
} else {
metx[n] = -1;
metr[n] = true;
metf[n] = 0;
}
}
public Color NewColor()
{
int[] rgb;
int t;
rgb = new int[3];
for (int i=0; i<3; i++) rgb[i] = 0;
t = (int) (Math.random()*3);
rgb[t] = (int) (Math.random()*128 + 1) + 127;
return new Color(rgb[0], rgb[1], rgb[2]);
}
public void run()
{
long starttime;
Graphics g;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
g=getGraphics();
while(true)
{
starttime=System.currentTimeMillis();
try
{
paint(g);
starttime += 30;
Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
}
}
public void start()
{
if (thethread == null) {
thethread = new Thread(this);
thethread.start();
}
}
public void stop()
{
if (thethread != null) {
thethread.stop();
thethread = null;
}
}
// This class handles mouse clicking
public boolean mouseDown(Event e,int xx,int yy)
{
if (ingame) {
mousex = xx;
mousey = yy;
keyDown(e, 32);
} else {
keyDown(e, 'S');
}
return true;
}
// Game Start
public void GameStart()
{
// Set Up Ship variables
bmax = blevel*blevel;
bcur = bmax;
smax = slevel*slevel;
scur = smax;
difflev = 3;
distance=0;
score=0;
renew=250;
for (int n=0; n<maxmet; n++) {
metx[n] = -1;
metf[n] = 0;
metr[n] = true;
}
metcount=0;
metmy=2;
mrenew=60;
// Bosses init
// #1 - SunBird;
sbx = -1;
sbmove = 2;
maxtribe = 1;
sunbird=false;
sbefore = true;
safter = false;
}
// Game Over
public void GameOver()
{
ingame=false;
}
// Boss #1 - Sunbird's pack
public void SunBird()
{
int[] xcur, ycur;
xcur = new int[11];
ycur = new int[11];
if (sbx<0) {
sbx = (int) ((Math.random() * xSize - 40) + 1);
sby = -5;
sbefore = true;
safter = false;
}
sby += sbmove;
if (y+sysize/2<sby) safter = true;
goff.setColor(new Color(0xffff00));
if (sbefore & safter) {
// hit ship
goff.fillRect(0, sby+15, xSize, 2);
HitShip();
}
for (int i=0; i<11; i++) {
xcur[i] = sbfx[i] + sbx;
ycur[i] = sbfy[i] + sby;
}
goff.fillPolygon(xcur, ycur, 11);
if (sby>xSize+20) {
sbx=-1;
sbefore = true;
safter = false;
}
sbefore=false;
if (y+sysize/2>sby) sbefore = true;
}
public boolean BirdHit(int blx, int bly) {
if (sunbird) {
if (blx+bul_xs>sbx & blx<sbx+40 & bly+bul_ys>sby & bly<sby+20) {
tribe--;
if (tribe<0) sunbird=false;
sbx=-1;
sbefore = true;
safter = false;
return true;
}
}
return false;
}
}