-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
661 lines (519 loc) · 13.7 KB
/
Copy pathMain.java
File metadata and controls
661 lines (519 loc) · 13.7 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
package gruppe38;
import gruppe38.Items.Bombe;
import gruppe38.Items.BombeSp2;
import gruppe38.Items.Explosion;
import gruppe38.Netzwerk.Client;
import gruppe38.Netzwerk.IP;
import gruppe38.Netzwerk.Server;
import gruppe38.Netzwerk.Stamp;
import gruppe38.Sonstiges.StdDraw;
import gruppe38.Sounds.SoundLibrary;
import gruppe38.Spieler.Spieler;
import gruppe38.Spieler.Steuerung;
import gruppe38.Spieler.SteuerungSp2;
import gruppe38.Spielfeld.Spielfeld2;
import gruppe38.Tests.Kollisionsabfrage;
import java.awt.Color;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* Hauptprogramm und Spielschleife
*
* @author Gruppe38
*
*/
public class Main {
// Initialisierung der FPS
private static long delta = 0;
private static long last = 0;
public static long fps = 0;
// Initialisierung der Varbiablen, Objekte etc.
/**
* Maximale Bombenanzahl
*/
private static int bombenanzahl = 30;
/**
* mindestens 12 Felder
*/
protected static int spielfelder = 15;
private static double spielfeldgroesse = (double) 1 / getSpielfelder();
private static Spieler sp1 = new Spieler(.5, .5, "wurst", 1, 4, "Spieler1");
private static Spieler sp2 = new Spieler(.5, .5, "wurst", 1, 4, "Spieler2");
private static Spielfeld2[][] feld = new Spielfeld2[getSpielfelder()][getSpielfelder()];
private static Color farbe[][] = new Color[getSpielfelder()][getSpielfelder()];
private static boolean bombenmalen = false;
// Spieler1
private static boolean left = false;
private static boolean right = false;
private static boolean up = false;
private static boolean down = false;
private static boolean space = false;
// Spieler2
private static boolean left2 = false;
private static boolean right2 = false;
private static boolean up2 = false;
private static boolean down2 = false;
private static boolean space2 = false;
private static boolean spiel_start = false;
private static boolean menu_start = true;
private static boolean netzwerk_spiel = false;
public static boolean netzwerk_localPlayer = false;
private static boolean netzwerk_remotePlayer = false;
private static boolean server_started = false;
public static boolean runserver = false;
public static boolean client_mode = false;
public static boolean is_client = false;
private static int[] ip = new int[5];
public static InetAddress address;
public static DatagramSocket socketcli;
public static DatagramPacket packetcli;
public static byte[] sendcli = new byte[1024*4];
private static int[] bild_drehen = new int[10];
private static double[] bild_x = new double[10];
private static double[] bild_y = new double[10];
private static double[] bild_vx = new double[10];
private static double[] bild_vy = new double[10];
// initialisierung der Width und Height des Bildes der Explosionsgrafik,
// auch zur Berechnung der Kollision ben�tigt
private static double w1[] = new double[getBombenanzahl()];
private static double w2[] = new double[getBombenanzahl()];
private static double w3[] = new double[getBombenanzahl()];
private static double w4[] = new double[getBombenanzahl()];
private static double h1[] = new double[getBombenanzahl()];
private static double h2[] = new double[getBombenanzahl()];
private static double h3[] = new double[getBombenanzahl()];
private static double h4[] = new double[getBombenanzahl()];
private static double hdraw[] = new double[getBombenanzahl()];
private static int bombencounter = 0;// z�hlt die anzahl der bomben
private static Bombe[] bombe = new Bombe[getBombenanzahl()];
private static BombeSp2[] bombe2 = new BombeSp2[getBombenanzahl()];
private static double bombe_x;
private static double bombe_y;
private static boolean bombencheck;
private static int x_feld = 0;
private static int y_feld = 0;
private static boolean multi = false;
private static int explosionscounter = 0;// z�hlt die anzahl der
// explosionen
private static Explosion[] explosion = new Explosion[getBombenanzahl()];
public static SoundLibrary soundlib;
/**
* random Zahl zwischen "von" und "bis"
*
* @param von
* @param bis
* @return Zufallszahl zwischen "von" und "bis"
*/
public static int randomnumber(int von, int bis) {
int a = 0;
double x = Math.random() * bis + von;
if (x > bis)
x = bis;
a = (int) x;
return a;
}
/**
* Methode zur Betragserrechnung, zwecks einfacher positiver Differenzen
* zweier Distanzen
*
*/
public static double Betrag(double wert1, double wert2) {
double betrag = (Math.sqrt((wert1 - wert2) * (wert1 - wert2)));
return betrag;
}
/**
* Methode zur FPS berechnung
*/
private static void computeDelta() {
delta = System.nanoTime() - last;
last = System.nanoTime();
fps = ((long) 1e9) / delta;
}
/**
* Hauptprogramm
*
* @return
* @throws IOException
*
*
* @throws InterruptedException
*
**/
public static void main(String[] args) throws IOException {
// StdAudio.loop("http://www.masterpaddy.de/etc/background.wav");
soundlib = new SoundLibrary();
soundlib.loadSound("bombe", "gruppe38/Sounds/bomb.au");
soundlib.loadSound("backgroundmusic", "gruppe38/Sounds/background.mid");
soundlib.loopSound("backgroundmusic");
if (is_client == false) {
Init.init();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Spielschleife
*/
while (true) {
computeDelta();
if (isSpiel_start()) {
// FPS berechnen
if (is_client == false) {
// Interaktion, bewegung etc
Steuerung.aktionen();
if (multi) {
SteuerungSp2.aktionen();
Kollisionsabfrage.mauer(sp2);
Kollisionsabfrage.item(sp2);
}
// Kollisionsabfragen
Kollisionsabfrage.mauer(sp1);
Kollisionsabfrage.item(sp1);
}
if(server_started == true && Server.startserver == true){
Stamp stempel = Stamp.create();
Stamp.send(stempel);
}
/**
* Befehle f�r das Zeichnen des Spieles
* ============================================================
*/
// Hintergrund
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.filledSquare(.5, .5, 1);
// den Draw-Befehl f�r alle Powerups, Bomben, Spieler aufrufen
Drawing.draw();
// Gib dem Frame StdDraw den Befehl das offscreenbild zu
// zeichnen
StdDraw.onscreen.drawImage(StdDraw.offscreenImage, 0, 0, null);;
// zeichne Frame neu
}
// warte 10 millisekunden
StdDraw.show(10);
// FPS anzeigen lassen
StdDraw.offscreen.setColor(Color.BLACK);
StdDraw.offscreen.drawString("FPS:" + Long.toString(fps), 20, 10);
// Gib dem Frame StdDraw den Befehl das offscreenbild zu
// zeichnen
StdDraw.onscreen.drawImage(StdDraw.offscreenImage, 0, 0, null);;
// zeichne Frame neu
StdDraw.frame.repaint();
}
}
public static Bombe[] getBombe() {
return bombe;
}
public static void setBombe(Bombe[] bombe) {
Main.bombe = bombe;
}
public static BombeSp2[] getBombe2() {
return bombe2;
}
public static void setBombe2(BombeSp2[] bombe2) {
Main.bombe2 = bombe2;
}
public static Spielfeld2[][] getFeld() {
return feld;
}
public static void setFeld(Spielfeld2[][] feld) {
Main.feld = feld;
}
public static double getSpielfeldgroesse() {
return spielfeldgroesse;
}
public static void setSpielfeldgroesse(double spielfeldgroesse) {
Main.spielfeldgroesse = spielfeldgroesse;
}
public static Spieler getSp1() {
return sp1;
}
public static void setSp1(Spieler sp1) {
Main.sp1 = sp1;
}
public static Spieler getSp2() {
return sp2;
}
public static void setSp2(Spieler sp2) {
Main.sp2 = sp2;
}
public static Color[][] getFarbe() {
return farbe;
}
public static void setFarbe(Color farbe[][]) {
Main.farbe = farbe;
}
public static boolean isBombenmalen() {
return bombenmalen;
}
public static void setBombenmalen(boolean bombenmalen) {
Main.bombenmalen = bombenmalen;
}
public static boolean isLeft() {
return left;
}
public static void setLeft(boolean left) {
Main.left = left;
}
public static boolean isRight() {
return right;
}
public static void setRight(boolean right) {
Main.right = right;
}
public static boolean isUp() {
return up;
}
public static void setUp(boolean up) {
Main.up = up;
}
public static boolean isDown() {
return down;
}
public static void setDown(boolean down) {
Main.down = down;
}
public static boolean isSpace() {
return space;
}
public static void setSpace(boolean space) {
Main.space = space;
}
public static boolean isLeft2() {
return left2;
}
public static void setLeft2(boolean left) {
Main.left2 = left;
}
public static boolean isRight2() {
return right2;
}
public static void setRight2(boolean right) {
Main.right2 = right;
}
public static boolean isUp2() {
return up2;
}
public static void setUp2(boolean up) {
Main.up2 = up;
}
public static boolean isDown2() {
return down2;
}
public static void setDown2(boolean down) {
Main.down2 = down;
}
public static void setSpace2(boolean space) {
Main.space2 = space;
}
public static boolean isSpace2() {
return space2;
}
public static boolean isMenu_start() {
return menu_start;
}
public static void setMenu_start(boolean menu_start) {
Main.menu_start = menu_start;
}
public static double[] getW1() {
return w1;
}
public static void setW1(double w1[]) {
Main.w1 = w1;
}
public static double[] getW2() {
return w2;
}
public static void setW2(double w2[]) {
Main.w2 = w2;
}
public static double[] getH1() {
return h1;
}
public static void setH1(double h1[]) {
Main.h1 = h1;
}
public static double[] getH2() {
return h2;
}
public static void setH2(double h2[]) {
Main.h2 = h2;
}
public static int getBombencounter() {
return bombencounter;
}
public static void setBombencounter(int bombencounter) {
Main.bombencounter = bombencounter;
}
public static double getBombe_x() {
return bombe_x;
}
public static void setBombe_x(double bombe_x) {
Main.bombe_x = bombe_x;
}
public static double getBombe_y() {
return bombe_y;
}
public static void setBombe_y(double bombe_y) {
Main.bombe_y = bombe_y;
}
public static boolean isBombencheck() {
return bombencheck;
}
public static void setBombencheck(boolean bombencheck) {
Main.bombencheck = bombencheck;
}
public static int getX_feld() {
return x_feld;
}
public static void setX_feld(int x_feld) {
Main.x_feld = x_feld;
}
public static int getY_feld() {
return y_feld;
}
public static void setY_feld(int y_feld) {
Main.y_feld = y_feld;
}
public static int getExplosionscounter() {
return explosionscounter;
}
public static void setExplosionscounter(int explosionscounter) {
Main.explosionscounter = explosionscounter;
}
public static Explosion[] getExplosion() {
return explosion;
}
public static void setExplosion(Explosion[] explosion) {
Main.explosion = explosion;
}
public static double[] getBild_x() {
return bild_x;
}
public static void setBild_x(double[] bild_x) {
Main.bild_x = bild_x;
}
public static double[] getBild_y() {
return bild_y;
}
public static void setBild_y(double[] bild_y) {
Main.bild_y = bild_y;
}
public static double[] getBild_vx() {
return bild_vx;
}
public static void setBild_vx(double[] bild_vx) {
Main.bild_vx = bild_vx;
}
public static double[] getBild_vy() {
return bild_vy;
}
public static void setBild_vy(double[] bild_vy) {
Main.bild_vy = bild_vy;
}
public static int getBombenanzahl() {
return bombenanzahl;
}
public static void setBombenanzahl(int bombenanzahl) {
Main.bombenanzahl = bombenanzahl;
}
public static int getSpielfelder() {
return spielfelder;
}
public static void setSpielfelder(int spielfelder) {
Main.spielfelder = spielfelder;
}
public static boolean isSpiel_start() {
return spiel_start;
}
public static void setSpiel_start(boolean spiel_start) {
Main.spiel_start = spiel_start;
}
public static double[] getW3() {
return w3;
}
public static void setW3(double w3[]) {
Main.w3 = w3;
}
public static double[] getW4() {
return w4;
}
public static void setW4(double w4[]) {
Main.w4 = w4;
}
public static double[] getH3() {
return h3;
}
public static void setH3(double h3[]) {
Main.h3 = h3;
}
public static double[] getH4() {
return h4;
}
public static void setH4(double h4[]) {
Main.h4 = h4;
}
public static double[] getHdraw() {
return hdraw;
}
public static void setHdraw(double hdraw[]) {
Main.hdraw = hdraw;
}
public static void starteNetzwerk() throws IOException {
runserver = true;
netzwerk_spiel = true;
Server serv = new Server();
serv.start();
System.out.println("Server-Thread gestartet");
client_mode = true;
//Client.registerLocalPlayer();
setIP(1, 127);
setIP(2, 0);
setIP(3, 0);
setIP(4, 1);
Client.establishConnection();
Client.connect();
netzwerk_localPlayer = true;
//Client cli = new Client(false);
//cli.start();
server_started = true;
}
public static void registerRemotePlayer() {
netzwerk_remotePlayer = true;
}
public static boolean NetzwerkStatus() {
return netzwerk_spiel;
}
public static void setNetzwerkStatus(boolean i){
netzwerk_spiel = i;
}
public static boolean netzwerk_localPlayer_status() {
// TODO Auto-generated method stub
return netzwerk_localPlayer;
}
public static boolean get_ClientMode() {
return client_mode;
}
public static void startClient() {
IP.popUp();
System.out.println("Pop-Up geschlossen");
//Client cli = new Client(true);
//cli.start();
}
public static void setMehrspieler(boolean b) {
multi = b;
}
public static boolean getMultiplayer() {
return multi;
}
public static void setIP(int i, int j){
ip[i] = j;
}
public static int[] getIP(){
return ip;
}
}