-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatriots-7.java
More file actions
1081 lines (970 loc) · 41.1 KB
/
Patriots-7.java
File metadata and controls
1081 lines (970 loc) · 41.1 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Sudipti Dantuluri
* 4.12.2022
* Patriots.java
* Final game project.*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
import javax.swing.Timer;
//Main class
public class Patriots extends JFrame {
//Main method
Practice pc = new Practice();
public static void main(String[] args) {
Patriots p = new Patriots();
}
//Main constructor
public Patriots() {
super("Patriots");
setSize( 900, 900);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(400,50);
setResizable(true);
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
//Class for the Menu button includes the "exit" button
class exitAction implements ActionListener {
//Closes the frame when performed
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitAction());
setContentPane( pc ); // OR frame.getContentPane().add(p_in);
setVisible(true);
}
//Practice class contains all the panels, cardlayout, and declared variables
class Practice extends JPanel {
int[] xp = {0,0,0,0,0,0,0,0,0,0};
int[] yp = {0,0,0,0,0,0,0,0,0,0};
int[] xp2 = {0,0,0,0,0,0,0,0,0,0};
int[] yp2 = {0,0,0,0,0,0,0,0,0,0};
int key = 0;
boolean key_flag = false;
boolean spacebar_key_flag = false;
JPanel tPanel;
int team1Num = 0;
int team2Num = 0;
private JButton startButton, buttonPlay, introButton;
private JTextArea t1, howToPlayDescription;
private JCheckBox checkBox1, checkBox2, checkBox3;
private JTextArea introDescription;
CardLayout cardLayout1 = new CardLayout();
Instant time_now = Instant.now();
Instant start_time = Instant.now();
Instant timesoldier_on_ground = Instant.now();
Instant robot_on_move = Instant.now();
Instant timerobot_on_ground = Instant.now();
int robot_top_level = 50;
int soldier_top_level = 50;
int ground_level = 500;
int robot_height = ground_level;
int soldier_height = ground_level;
boolean robot_goup = true;
boolean solddier_goup = true;
int robot_bullets = 10;
int soldier_bullets = 10;
Instant robot_bullets_time = Instant.now();
Instant soldier_bullets_time = Instant.now();
boolean gameove = false;
int tm = 0;
Instant robot_shield_time = Instant.now();
Instant equip_time = Instant.now();
int equip_count = 0;
int equip_points = 30;
int points = 0;
//Practice constructor calls mainMenu method
public Practice() {
mainMenu();
}
//Method has the JButtons and set Listeners and added panels
public void mainMenu() {
mainPanel mp = new mainPanel();
GamePanel gp = new GamePanel();
GamePanel2 gp2 = new GamePanel2();
GamePanel3 gp3 = new GamePanel3();
GamePanelLevel2 level2 = new GamePanelLevel2();
//antecedentIntroPanel beforePanel = new antecedentIntroPanel();
gp.addKeyListener(new MKeyListener());
level2.addKeyListener(new MKeyListener_level2());
gp.setFocusable(true);
level2.setFocusable(true);
mp.setFocusable(true);
gp.requestFocusInWindow();
level2.requestFocusInWindow();
JButton shield = new JButton("Equip");
shield.addActionListener(new sbuttonListener());
gp.add(shield);
shield.setBounds(0, 100, 50, 100);
JButton btn = new JButton("button");
btn.addActionListener(new sbuttonListener());
gp.add(btn);
btn.setBounds(0, 100, 50, 100);
JButton btn2 = new JButton("button2");
btn2.addActionListener(new sbuttonListener());
gp.add(btn2);
btn2.setBounds(0, 100, 50, 100);
Settings setting = new Settings();
Instructions instruction = new Instructions();
setLayout(cardLayout1);
add(mp , "Start Screen");
add(gp, "Game Panel");
add(setting, "Settings Panel");
add(instruction, "Instructions Panel");
add(gp2, "Game Panel 2");
add(gp3, "Game Panel 3");
add(level2, "Level2");
//add(beforePanel, "beforePlay");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(mp.getX(),
mp.getY()), "cursor2.png");
mp.setCursor (c);
}
/*
//Class for panel before game
class antecedentIntroPanel extends JPanel {
Image backgroundGradient = new ImageIcon("gradient.jpg").getImage(); //Source: https://www.sliit.lk/minimalizm-gradient-background/
//Constructor for the class
antecedentIntroPanel() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(getX(),
getY()), "cursor2.png");
setCursor (c);
JTextArea spiel = new JTextArea();
spiel.setText("\n\nRemember to shoot the bot by pressing the 'w' key and moving left and right with the 's' and 'd' keys. The bot has a shield that can it can activate and prevents you from shooting. You both have ten lives. Use your tokens ever half a minute to activate your shield. Good luck.");
spiel.setFont(new Font("BaccaratUpright", Font.PLAIN, 26));
spiel.setForeground(new Color(153,0,0));
add(spiel);
JButton toPlay = new JButton("Start");
toPlay.setFont(new Font("Anklepants", Font.BOLD, 30));
toPlay.setForeground(Color.MAGENTA);
toPlay.setBounds(300, 500, 300, 100);
toPlay.addActionListener(new sbuttonListener());
//Mouse listener for the instructions button; gets the foreground color
toPlay.addMouseListener(new MouseAdapter() {
Color color = toPlay.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
color = toPlay.getForeground();
toPlay.setForeground(Color.ORANGE);
toPlay.setBounds(279, 480, 340, 135);
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
toPlay.setForeground(color);
toPlay.setBounds(300, 500, 300, 100);
}
});
add(toPlay);
}
//Graphics method for the main panel has the background image
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundGradient, 0, 0, 900,900, null);
}
}
*/
//Class for the main page containing all the graphics and content
class mainPanel extends JPanel {
Image startPageBackground;
Icon design2;
//The main panel method's constructor containing the main JButtons and their set listeners
mainPanel() {
startPageBackground = new ImageIcon("galaxy.gif").getImage(); //Source: https://giphy.com/gifs/trippy-weird-psychedelic-3ov9k1173PdfJWRsoE
setLayout(null);
JLabel header = new JLabel("<html>" + "Patriots" + "<br>" + "</html>");
header.setFont(new Font("Osake", Font.BOLD, 50));
header.setHorizontalAlignment(JLabel.CENTER);
header.setForeground(Color.WHITE);
add(header);
JButton startButton = new JButton("Play");
startButton.setLayout(null);
//startButton.setOpaque(false);
startButton.setFont(new Font("Anklepants", Font.BOLD, 30)); //Airacobra Alt
startButton.setBounds(300, 200, 300, 100);
startButton.setForeground(Color.GRAY);
startButton.addActionListener(new sbuttonListener());
//Mouse listener for the start button; gets the foreground color
startButton.addMouseListener(new MouseAdapter() {
Color color = startButton.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
//startButton.setOpaque(true);
color = startButton.getForeground();
startButton.setForeground(new Color(204,0,0));
startButton.setBounds(279, 180, 340, 135);
//startButton.setBackground(new Color(217,217,214));
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
startButton.setForeground(color);
startButton.setBounds(300, 200, 300, 100);
//startButton.setOpaque(false);
}
});
add(startButton);
JButton settingsButton = new JButton("Settings");
settingsButton.setFont(new Font("Anklepants", Font.BOLD, 30));
settingsButton.setForeground(Color.GRAY);
add(settingsButton);
settingsButton.setBounds(300, 350, 300, 100);
settingsButton.addActionListener(new sbuttonListener());
//Mouse listener for the settings button; gets the foreground color
settingsButton.addMouseListener(new MouseAdapter() {
Color color = settingsButton.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
color = settingsButton.getForeground();
settingsButton.setForeground(new Color(204,0,0));
settingsButton.setBounds(279, 330, 340, 135);
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
settingsButton.setForeground(color);
settingsButton.setBounds(300, 350, 300, 100);
}
});
add(settingsButton);
JButton instructionsButton = new JButton("Instructions");
instructionsButton.setFont(new Font("Anklepants", Font.BOLD, 30));
instructionsButton.setForeground(Color.GRAY);
add(instructionsButton);
instructionsButton.setBounds(300, 500, 300, 100);
instructionsButton.addActionListener(new sbuttonListener());
//Mouse listener for the instructions button; gets the foreground color
instructionsButton.addMouseListener(new MouseAdapter() {
Color color = instructionsButton.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
color = instructionsButton.getForeground();
instructionsButton.setForeground(new Color(204,0,0));
instructionsButton.setBounds(279, 480, 340, 135);
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
instructionsButton.setForeground(color);
instructionsButton.setBounds(300, 500, 300, 100);
}
});
add(instructionsButton);
}
//Graphics method for the main panel has the background image
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(startPageBackground, 0, 0, 900,900, null);
JButton instructionsButton = new JButton("Instructions");
instructionsButton.setIcon(design2);
repaint();
}
}
//Class for the game panel includes all the content for the game
class GamePanel extends JPanel {
int speed = 3;
Image soldier1reg;
Image soldier1reg_flat;
Image back;
Image robot1;
Image soldier1og;
Image coin;
Image userLife;
Image botLife;
Image shields;
Image shields2;
Image bullet;
Image bullet2;
//Constructor for the game panel class includes time and actions
GamePanel() {
soldier1reg = new ImageIcon("soldier1reg.png").getImage();
soldier1reg_flat = new ImageIcon("soldier1reg_flat.png").getImage();
back = new ImageIcon("firstGame.jpg").getImage();
robot1 = new ImageIcon("robot.png").getImage();
soldier1og = new ImageIcon("soldier1og.png").getImage();
coin = new ImageIcon("token4.gif").getImage(); //Source: https://pt.picmix.com/stamp/gold-coin-gif-1456107
userLife = new ImageIcon("heart.png").getImage(); //Source: https://pluspng.com/heart-png-hd-transparent-background-7155.html
botLife = new ImageIcon("heart.png").getImage(); //Source: https://pluspng.com/heart-png-hd-transparent-background-7155.html
shields = new ImageIcon("shield.png").getImage(); //Source: https://www.etsy.com/listing/827669417/medieval-knight-black-heater-shield
shields2 = new ImageIcon("shield2.png").getImage(); //Source: https://www.etsy.com/listing/827669417/medieval-knight-black-heater-shield
bullet = new ImageIcon("bullet.png").getImage();
bullet2 = new ImageIcon("bullet2.png").getImage();
}
//Get robot height
public int get_robot_height(int height) {
//for (int i=0; i<100000; i++) System.nanoTime();
long delta = Duration.between(timerobot_on_ground, Instant.now()).toMillis();
if (height >= ground_level && delta < 5000 + (int)(Math.random()*10000)) {
robot_on_move = Instant.now();
return height;
}
long delta2 = Duration.between(robot_on_move, Instant.now()).toNanos();
if (delta2 > 5000) {
robot_on_move = Instant.now();
if (robot_goup == true) {
if (height-- < robot_top_level) {
robot_goup = false;
}
} else {
if (height++ >= ground_level) {
robot_goup = true;
timerobot_on_ground = Instant.now();
}
}
}
return height;
}
//Get soldier height
public int get_soldier_height(int height) {
if (spacebar_key_flag == false) {
timesoldier_on_ground = Instant.now();
return height;
}
long delta = Duration.between(timesoldier_on_ground, Instant.now()).toNanos();
if (delta > 5000) {
timesoldier_on_ground = Instant.now();
if (solddier_goup == true) {
if (height-- <= soldier_top_level) {
solddier_goup = false;
}
} else {
if (height++ >= ground_level) {
solddier_goup = true;
spacebar_key_flag = false;
}
}
}
return height;
}
//Get shield
public boolean turn_on_robot_shield() {
if ((int)Duration.between(robot_shield_time, Instant.now()).getSeconds() > 5) {
if ((int)Duration.between(robot_shield_time, Instant.now()).getSeconds() > 9) {
robot_shield_time = Instant.now();
}
return true;
} else {
return false;
}
}
//Game panel's graphic method includes the background, characters, additional images, and sets the key's listeners and movement for the characters
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!gameove) {
tm = (int)Duration.between(start_time, Instant.now()).getSeconds();
}
//for (int i=0; i<50000; i++) System.nanoTime();
g.drawImage(back, 0, 0, 900,900, null);
robot_height = get_robot_height(robot_height);
g.drawImage(robot1, 700, robot_height, 120,330, null);
soldier_height = get_soldier_height(soldier_height);
long delta = Duration.between(time_now, Instant.now()).toMillis();
if (key == 'w' && delta < 2000) {
g.drawImage(soldier1og, 100, soldier_height, 205,350, null);
} else {
g.drawImage(soldier1reg, 100, soldier_height, 130,350, null);
}
g.drawImage(coin, 15, 20, 50,50, null);
g.drawImage(userLife, 200, soldier_height-71, 50,50, null);
g.drawImage(botLife, 800, robot_height-75, 50,50, null);
setFont(new Font("Airacobra", Font.BOLD, 30));
if (tm >= equip_count*equip_points) {
points = tm - equip_count * equip_points;
}
g.drawString(String.valueOf(points),70,57);
if (soldier_bullets > 0) {
if ((int)Duration.between(soldier_bullets_time, Instant.now()).toMillis() > 4000) {
g.drawString(String.valueOf(soldier_bullets),750,robot_height-40);
}
} else {
g.drawString("You win the game",750,robot_height-40);
pc.cardLayout1.show(pc, "Game Panel 2");
//add(secondlevel, "Game Panel 2");
if (!gameove) {
gameove = true;
g.setColor(Color.RED);
for (int i=0; i<10000; i++) {
Toolkit.getDefaultToolkit().beep();
}
JButton continues = new JButton("Continue");
add(continues);
continues.addActionListener(new sbuttonListener());
}
}
if (robot_bullets > 0) {
if ((int)Duration.between(robot_bullets_time, Instant.now()).toMillis() > 900) {
g.drawString(String.valueOf(robot_bullets),150,soldier_height-36);
}
} else {
g.drawString("You lost the game",150,soldier_height-36);
pc.cardLayout1.show(pc, "Game Panel 3");
//add(secondlevel, "Game Panel 2");
if (!gameove) {
gameove = true;
g.setColor(Color.RED);
for (int i=0; i<10000; i++) {
Toolkit.getDefaultToolkit().beep();
}
}
}
grabFocus();
for(int i=0; i<xp.length; i++) {
if (xp[i] != 0) {
// add one position for every refresh
if (xp[i]++ > 60000) {
xp[i] = 0;
}
}
if (xp2[i] != 0) {
// add one position for every refresh
if (xp2[i]++ > 60000) {
xp2[i] = 0;
}
}
}
for(int i=0; i<xp.length; i++) {
if (xp[i] == 5000 ) {
soldier_bullets--;
System.out.println("soldier_bullets " + soldier_bullets);
}
}
for(int i=0; i<xp2.length; i++) {
if (xp2[i] == 7000 ) {
robot_bullets--;
System.out.println("robot_bullets " + robot_bullets);
}
}
boolean rsflag = turn_on_robot_shield();
if (rsflag == false) {
if (key == 'w') {
if (key_flag == true) {
key_flag = false;
for(int i=0; i<xp.length; i++) {
if (xp[i] == 0) {
xp[i] = 900;
yp[i] = soldier_height + 50;
if (Math.abs(soldier_height - robot_height) < 100) {
//soldier_bullets--;
soldier_bullets_time = Instant.now();
System.out.println("soldier_bullets " + soldier_bullets);
}
break;
}
}
}
}
}
for(int i=0; i<xp2.length; i++) {
int r = (int)(Math.random()*100000);
if (r == 1) {
if (xp2[i] == 0) {
xp2[i] = 5100;
yp2[i] = robot_height;
//if (Math.abs(soldier_height - robot_height) < 100) {
if ((int)Duration.between(equip_time, Instant.now()).getSeconds() > 5) {
if (Math.abs(soldier_height - robot_height) < 100) {
//robot_bullets--;
robot_bullets_time = Instant.now();
System.out.println("robot_bullets " + robot_bullets);
}
//}
}
break;
}
}
}
if (tm > 30) {
if ((int)Duration.between(equip_time, Instant.now()).getSeconds() <5) {
g.drawImage(shields, 240, 440, 100,200, null);
}
}
if (rsflag == true) {
g.drawImage(shields2, 540, 440, 100,200, null);
}
for (int w=0; w<xp.length; w++) {
if (xp[w] !=0) {
g.drawImage(bullet, xp[w]/speed, yp[w], 35,25, null);
}
}
// try from the enemy
for (int w=0; w<xp2.length; w++) {
if (xp2[w] !=0) {
g.drawImage(bullet2, (7200-xp2[w])/speed, yp2[w], 35,25, null);
}
}
repaint();
/*if (gameove==false) {
CardLayout cl2 = new CardLayout();
setLayout(cl2);
JLabel nextLevel = new JLabel("Level 2");
add(nextLevel);
nextLevel.setFont(new Font("Airacobra", Font.BOLD, 30));
nextLevel.setBounds(50, 50, 50, 50);
//nextLevel.addActionListener(new sbuttonListener2());
}
if (gameove==true){
CardLayout cl2 = new CardLayout();
setLayout(cl2);
JButton nextLevel = new JButton("Level 2");
add(nextLevel);
nextLevel.setFont(new Font("Airacobra", Font.BOLD, 30));
nextLevel.setBounds(50, 50, 50, 50);
//nextLevel.addActionListener(new sbuttonListener2());
}*/
}
}
//Class for the panel leading to the second level
class GamePanel2 extends JPanel {
Image peep;
//Constructor for class contains text and buttons in the panel
GamePanel2() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(getX(),
getY()), "cursor2.png");
setCursor (c);
setLayout(new BorderLayout(50,50));
JButton secondlevelBtn = new JButton("Second level");
secondlevelBtn.setForeground(new Color(152,29,151));
secondlevelBtn.setFont(new Font("Anklepants", Font.BOLD, 30));
secondlevelBtn.setBounds(300, 350, 300, 100);
secondlevelBtn.addActionListener(new sbuttonListener());
add(secondlevelBtn, BorderLayout.SOUTH);
JLabel winHeader = new JLabel("<html>" + "You won" + "<br>" + "</html>");
winHeader.setFont(new Font("BaccaratUprightWide", Font.BOLD, 55));
winHeader.setHorizontalAlignment(JLabel.CENTER);
add(winHeader, BorderLayout.NORTH);
peep = new ImageIcon("peep.png").getImage(); //Source: https://stickers.cloud/en/pack/pepe-29
}
//Graphics for the second level's panel include the happy peepo image and draw string
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(peep, 270, 330, 300,300, null);
setFont(new Font("BaccaratUpright", Font.BOLD, 55));
setForeground(new Color(63,42,20));
g.drawString("Level 2: Peepo",270,240);
setBackground(Color.GRAY);
}
}
//Class for the panel saying user lost
class GamePanel3 extends JPanel {
//Constructor for class contains text and buttons in the panel
GamePanel3() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(getX(),
getY()), "cursor2.png");
setCursor (c);
setLayout(new BorderLayout(50,50));
JLabel winHeader = new JLabel("<html>" + "Game over" + "<br>" + "</html>");
winHeader.setFont(new Font("BaccaratUprightWide", Font.BOLD, 55));
winHeader.setHorizontalAlignment(JLabel.CENTER);
add(winHeader, BorderLayout.NORTH);
JButton home = new JButton("Home");
home.setFont(new Font("Anklepants", Font.BOLD, 30));
home.setForeground(Color.MAGENTA);
home.setBounds(300, 200, 300, 100);
home.addActionListener(new sbuttonListener());
//Mouse listener for the settings button; gets the foreground color
home.addMouseListener(new MouseAdapter() {
Color color = home.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
color = home.getForeground();
home.setForeground(Color.ORANGE);
home.setBounds(279, 180, 340, 135);
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
home.setForeground(color);
home.setBounds(300, 200, 300, 100);
}
});
add(home, BorderLayout.SOUTH);
JButton quit = new JButton("Quit");
quit.setFont(new Font("Anklepants", Font.BOLD, 30));
quit.setForeground(Color.MAGENTA);
quit.setBounds(300, 500, 300, 100);
quit.addActionListener(new sbuttonListener());
//Mouse listener for the instructions button; gets the foreground color
quit.addMouseListener(new MouseAdapter() {
Color color = quit.getForeground();
//When mouse enters, the button changes color
public void mouseEntered(MouseEvent me) {
color = quit.getForeground();
quit.setForeground(Color.ORANGE);
quit.setBounds(279, 480, 340, 135);
}
//When the mouse exits, the mouse returns to the original color
public void mouseExited(MouseEvent me) {
quit.setForeground(color);
quit.setBounds(300, 500, 300, 100);
}
});
add(quit, BorderLayout.SOUTH);
}
//Graphics for the panel include the sad peepo image
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image sadPeep = new ImageIcon("sadPeepo.png").getImage(); //Source: https://www.pngmart.com/image/160807
g.drawImage(sadPeep, 300, 400, 300,300, null);
setFont(new Font("BaccaratUpright", Font.BOLD, 55));
setBackground(Color.GRAY);
}
}
//Class for the second level's panel
class GamePanelLevel2 extends JPanel {
int x=0;
int x2=0;
int enemy_x1=0;
int enemy_x2=0;
int enemy_d=1;
int enemy_d2=1;
Instant enemy_x_time = Instant.now();
Instant enemy_x_time2 = Instant.now();
Image enemy;
Image ufo;
Image ufo2;
Image level2Background;
Instant start_time = Instant.now();
Image soldier1reg;
Image soldier1reg_flat;
Image soldier1og;
Image soldier1ogLeft;
Image bullet;
Image bullet2;
int speed = 30;
//Constructor contains defining the images
GamePanelLevel2() {
soldier1reg = new ImageIcon("soldier1reg.png").getImage();
soldier1reg_flat = new ImageIcon("soldier1reg_flat.png").getImage();
enemy = new ImageIcon("peepo.gif").getImage(); //Source: https://tenor.com/view/naruto-run-peepo-pepe-frog-runners-fast-gif-17319616
ufo = new ImageIcon("UFO.png").getImage(); //Source: https://gameznet.com.au/alien-and-ufos/ufo-transparent-background/
ufo2 = new ImageIcon("UFO2.png").getImage(); //Source: https://gameznet.com.au/alien-and-ufos/ufo-transparent-background/
level2Background = new ImageIcon("twinkle.gif").getImage(); //Source: https://gifimage.net/twinkling-stars-gif-6/
soldier1og = new ImageIcon("soldier1og.png").getImage();
soldier1ogLeft = new ImageIcon("soldier1ogLeft.png").getImage();
bullet = new ImageIcon("bullet.png").getImage();
bullet2 = new ImageIcon("bullet2.png").getImage();
}
public int enemy_x() {
long delta = Duration.between(enemy_x_time, Instant.now()).toMillis();
if (delta > 100) {
enemy_x_time = Instant.now();
//enemy_speed = 1 + (int)(Math.random()*4);
//eenemy_dnemyx1 += enemy_speed;
if ((int)(Math.random()*10) == 0) {
enemy_d *= 2;
}
if (enemy_d>200) {
enemy_d = 1;
}
enemy_x1 += enemy_d;
if (enemy_x1 > 800) {
enemy_x1 = 0;
}
}
//System.out.println(enemy_x1);
return enemy_x1;
}
public int enemy_2x() {
long delta = Duration.between(enemy_x_time2, Instant.now()).toMillis();
if (delta > 100) {
enemy_x_time2 = Instant.now();
//enemy_speed = 1 + (int)(Math.random()*4);
//eenemy_dnemyx1 += enemy_speed;
if ((int)(Math.random()*10) == 0) {
enemy_d2 *= 2;
}
if (enemy_d2>200) {
enemy_d2 = 1;
}
enemy_x2 += enemy_d2;
if (enemy_x2 > 800) {
enemy_x2 = 0;
}
}
//System.out.println(enemy_x1);
return enemy_x2;
}
//Graphics for the second level's panel include the actions and characters
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(level2Background, 0, 0, 900,900, null);
Image clockTime = new ImageIcon("clock.gif").getImage();
g.drawImage(clockTime, 15, 20, 50,50, null);
setFont(new Font("Airacobra", Font.BOLD, 30));
int tk = 50;
int kl = 1;
int point = 0;
/*if (tk >= kl) {
point = tk - kl;
g.drawString(String.valueOf(point),70,57);
repaint();
}*/
//g.drawString(String.valueOf(point),70,57);
//repaint();
for (int i=50;i<=0;i--) {
g.drawString(String.valueOf(i),70,57);
}
long delta = Duration.between(time_now, Instant.now()).toMillis();
if (key == 'a' && delta < 2000) {
g.drawImage(soldier1ogLeft, 300, soldier_height+130, 80,220, null);
} else if (key == 's' && delta < 2000) {
g.drawImage(soldier1og, 300, soldier_height+130, 80,220, null);
} else {
//g.drawImage(soldier1reg, 300, soldier_height, 80,220, null);
g.drawImage(soldier1reg, 300, soldier_height+130, 80,220, null);
}
if (x++ > 800) {
x = 0;
}
if (x % 4 == 0) {
x2++;
if (x2 == 300) {
start_time = Instant.now();
}
}
if ((int)Duration.between(start_time, Instant.now()).getSeconds() <1) {
x2 = 301;
}
if (x2 > 800) {
x2 = 0;
}
setForeground(new Color(153,123,88));
if (x2 < 300) {
g.drawImage(ufo, x2, 100, 100,100, null);
} else {
g.drawImage(ufo2, x2, 100, 100,100, null);
}
g.drawImage(enemy, enemy_x(), soldier_height+240, 80,80, null);
g.drawImage(enemy, enemy_2x(), soldier_height+250, 80,80, null);
repaint();
for (int w=0; w<xp.length; w++) {
if (xp[w] !=0) {
g.drawImage(bullet, xp[w]/speed, yp[w], 35,25, null);
}
}
// try from the enemy
for (int w=0; w<xp2.length; w++) {
if (xp2[w] !=0) {
g.drawImage(bullet2, (7200-xp2[w])/speed, yp2[w], 35,25, null);
}
}
for(int i=0; i<xp.length; i++) {
if (xp[i] != 0) {
// add one position for every refresh
if (xp[i]++ > 60000) {
xp[i] = 0;
}
if (xp[i] % 100 == 0) yp[i]++;
}
if (xp2[i] != 0) {
// add one position for every refresh
if (xp2[i]++ > 60000) {
xp2[i] = 0;
}
if (xp2[i] % 100 == 0) yp2[i]++;
}
}
if (key == 'a' || key == 's') {
if (key_flag == true) {
System.out.println("============"); //"soldier_bullets " + soldier_bullets);
key_flag = false;
for(int i=0; i<xp.length; i++) {
if (xp[i] == 0) {
xp[i] = 900;
yp[i] = soldier_height + 50;
if (Math.abs(soldier_height - robot_height) < 100) {
//soldier_bullets--;
soldier_bullets_time = Instant.now();
System.out.println("soldier_bullets " + soldier_bullets);
}
break;
}
}
}
}
}
}
//Class for the settings panel includes the text and content and text on how to play
class Settings extends JPanel {
//how to play panel's constructor
Settings() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(getX(),
getY()), "cursor2.png");
setCursor (c);
JLabel settingsHeader = new JLabel("<html>" + "How To Play" + "<br>" + "</html>");
settingsHeader.setFont(new Font("Osake", Font.BOLD, 55)); //Aspastic
settingsHeader.setForeground(new Color(153,0,0));
settingsHeader.setHorizontalAlignment(JLabel.CENTER);
JTextArea settingsDescription = new JTextArea();
settingsDescription.setText("\n\nFirst level:\nSpace bar = jump up\n“w” key = shoot\n\"q\" key = quit\n\nSecond level:\nSpace bar = jump up\n“w” key = shoot\n\"q\" key = quit\n\nThird level:\n“Up” arrow = up\n“Down” arrow = down\n“Left” arrow = left\n“Right” arrow = right\n“w” key = shoot ");
settingsDescription.setFont(new Font("BaccaratUpright", Font.PLAIN, 26));
settingsDescription.setForeground(new Color(153,0,0));
settingsDescription.setPreferredSize(new Dimension (700, 700));
settingsDescription.setBounds(50, 50, 250, 150);
settingsDescription.setBackground(Color.GRAY);
settingsDescription.setEditable(false);
add(settingsHeader, BorderLayout.NORTH);
add(settingsDescription, BorderLayout.SOUTH);
setBackground(Color.GRAY);
JButton previousButton = new JButton("Back");
previousButton.setBounds(80, 400, 80, 50);
previousButton.setFont(new Font("Aspastic", Font.BOLD, 30));
add(previousButton);
previousButton.addActionListener(new sbuttonListener());
}
//Graphics for the setting's panel
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
//Class for the instructions panel includes the text and content for introducing the game
class Instructions extends JPanel {
//Instruction panel constructor
Instructions() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("cursor2.png"); //Source: https://www.pngmart.com/image/26832
Cursor c = toolkit.createCustomCursor(image , new Point(getX(),
getY()), "cursor2.png");
setCursor (c);
JLabel introHeader = new JLabel("<html>" + "Introduction" + "<br>" + "</html>");
introHeader.setFont(new Font("Osake", Font.BOLD, 50));
introHeader.setHorizontalAlignment(JLabel.CENTER);
introDescription = new JTextArea();
introDescription.setText("\n\nThe US has been invaded by bots and needs your help.\nYou have been recruited by the military. Each level,\nyour goal is to destroy the bots and then you will be promoted a new role.\nFor the first level, you will have to defeat the bot with your given weapon and watch out for your health.\nWhen your health bar reaches zero, you lose; so shoot the bot until it’s health bar zero.\nOnce you win the first level, in the\nsecond level you will be able to access more\nweapons and equipment and have more bots to shoot.\nIn the last level, you will be promoted to a navy SEAL officer, so you will have to be aware of\nhealth and oxygen levels. Swim up to the surface to\nregenerate your oxygen levels.");
introDescription.setFont(new Font("BaccaratUpright", Font.PLAIN, 26));
introDescription.setEditable(false);
introDescription.setLineWrap(true);
introDescription.setWrapStyleWord(true);
introDescription.setPreferredSize(new Dimension (700, 700));
introDescription.setBounds(50, 50, 700, 700);
introDescription.setBackground(new Color(255,102,102));
introDescription.setPreferredSize(new Dimension (700, 700));
add(introHeader);
add(introDescription);
setBackground(new Color(255,102,102));
JButton previousButton = new JButton("Back");
//previousButton.setLayout(null);
//previousButton.setOpaque(false);
previousButton.setFont(new Font("Aspastic", Font.BOLD, 30));
previousButton.setBounds(80, 400, 80, 50);
previousButton.setForeground(Color.BLACK);
previousButton.addActionListener(new sbuttonListener());
add(previousButton);
}
//Graphics for the instruction's panel
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
//Class for JButton listener. It listens to what JButton is clicked and what panel to display afterwards.
class sbuttonListener implements ActionListener {
//Action performed for JButtons method
public void actionPerformed(ActionEvent e) {
String sbutton = e.getActionCommand();
if(sbutton.equalsIgnoreCase("Start")) {
pc.cardLayout1.show(pc, "Game Panel");
} else if(sbutton.equalsIgnoreCase("Settings")) {
pc.cardLayout1.show(pc, "Settings Panel");
} else if(sbutton.equalsIgnoreCase("Instructions")) {
pc.cardLayout1.show(pc, "Instructions Panel");
} else if(sbutton.equalsIgnoreCase("Back")) {
pc.cardLayout1.show(pc, "Start Screen");