-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPvZ.java
More file actions
1546 lines (1358 loc) · 44.4 KB
/
PvZ.java
File metadata and controls
1546 lines (1358 loc) · 44.4 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
// PvZ.java
// David Wang & Wendy Xu
// Plants Vs. Zombies is a tower defense game in which the user must plant plants on their front lawn
// in order to protect again zombies. With 18 different plants and 7 different zombies, the user must
// come up with a strategy in order to defeat the zombies successfully and successfully keep their
// brains from being eaten.
// This is the plants vs zombies game main class. It runs everything that the game needs to function.
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;
import javax.sound.sampled.AudioSystem;
public class PvZ extends JFrame implements ActionListener{
GamePanel gamepanel;
MainMenu menu;
boolean sound, music,mplaying;
AudioClip playingmusic;
javax.swing.Timer myTimer;
public PvZ(){
super("Plants Vs. Zombies");
setLayout(null);
setSize(818,647);
menu = new MainMenu(this);
gamepanel = new GamePanel(this);
gamepanel.setSize(818,647);
gamepanel.setLocation(0,0);
add(gamepanel);
setSize(818,647);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myTimer = new javax.swing.Timer(10,this);
mplaying = false;
playingmusic = Applet.newAudioClip(getClass().getResource("MP3/Music/InGame.wav")); // music while playing game
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
gamepanel.play();
music = menu.getMusic(); // music & sound must be held constand throughout the entire game and menues,
sound = menu.getSound(); // must constantly be checking if the user changed them through menu using options
}
public void startTimer(){
myTimer.start();
}
public void setMenu(){
menu.setVisible(true);
menu.playMusic();
}
public MainMenu getMenu(){
return menu;
}
public GamePanel getPanel(){
return gamepanel;
}
public boolean getMusic(){
return music;
}
public boolean getSound(){
return sound;
}
public void setMusic(boolean music){
menu.setMusic(music);
this.music = music;
}
public void setSound(boolean sound){
menu.setSound(sound);
this.sound = sound;
}
public static void main(String[] args){
PvZ PlantsVsZombies = new PvZ();
}
}
class GamePanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener{
private PvZ pvz;
private GameMap game;
private Shooter player; // in this game the user is a peashooter and can shooter bullets too
private boolean clicked,lose,gamestarted,loaded,paused,win,inalmanac,scoreAdded;
private int score;
String mode = "levels";
private int[] pos = new int[]{0,0};
private boolean[] keys;
private Rectangle2D startgame = new Rectangle2D.Double(700,500,50,50);
// rectangles for pause menu
private Rectangle pauseBackToGame = new Rectangle(245,458,330,76);
private Rectangle pauseAlmanacRect = new Rectangle(308,309,201,37);
private Rectangle pauseRestartRect = new Rectangle(308,351,201,37);
private Rectangle pauseMainRect = new Rectangle(308,393,201,37);
private Rectangle musicCheckBox = new Rectangle(480,275,34,30);
private Rectangle soundFXCheck = new Rectangle(480,244,34,30);
// rectangles for levels mode end game
private Rectangle levelsLoseWinMenu = new Rectangle(427,550,151,34);
private Rectangle levelsLoseWinAgain = new Rectangle(224,550,151,34);
// rectangles for survival mode end game
private Rectangle survivalLoseWinAgain = new Rectangle(127,550,151,34);
private Rectangle survivalLoseWinMenu = new Rectangle(330,550,151,34);
private Rectangle survivalLoseWinScore = new Rectangle(533,550,151,34);
// variables for levels & survival
private int survivalwave = 0, survivalwavecount = 0, level;
// sound variables
private boolean music,sound,mplaying; // mplaying checks if music is already playing
private AudioClip playingmusic,losemusic,pickingmusic;
public void mouseClicked(MouseEvent evt){
clicked = true;
}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){
clicked = true;
}
public void mousePressed(MouseEvent evt){
clicked = false;
System.out.println(evt.getX()+" "+evt.getY());
}
public void mouseMoved(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public void mouseDragged(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public void keyPressed(KeyEvent evt){
int i = evt.getKeyCode();
keys[i] = true;
}
public void keyTyped(KeyEvent evt){
}
public void keyReleased(KeyEvent evt){
int i = evt.getKeyCode();
keys[i] = false;
}
public GamePanel(PvZ pvz){
super();
setFocusable(true);
grabFocus();
game = new GameMap();
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
keys = new boolean[2000];
this.pvz = pvz;
player = game.getPlayer();
clicked = false;
lose = false;
win = false;
loaded = false;
gamestarted = false;
paused = false;
inalmanac = false;
scoreAdded = false;
playingmusic = Applet.newAudioClip(getClass().getResource("MP3/Music/InGame.wav"));
losemusic = Applet.newAudioClip(getClass().getResource("MP3/Sound FX/GameOver.wav"));
pickingmusic = Applet.newAudioClip(getClass().getResource("MP3/Music/ChoosingPlants.wav"));
mplaying = false;
}
public void gameStart(){
gamestarted = true;
}
public GameMap getMap(){
return game;
}
public void pause(){
paused = true;
}
public String getMode(){
return mode;
}
public void setMode(String mode){
this.mode = mode;
}
public void setLevel(int level){
this.level = level;
}
public void setInAlmanac(boolean inalmanac){
this.inalmanac = inalmanac;
}
public void setMusicLoop(){
playingmusic.loop();
mplaying = true;
}
public void resetEverything(){
// resets the game stats for starting a new game
survivalwave = 0;
survivalwavecount = 0;
score = 0;
lose = false;
loaded = false;
gamestarted = false;
paused = false;
win = false;
scoreAdded = false;
game.resetEverything();
}
public void play(){
// plays the game as well as pauses, and ends the game
music = pvz.getMusic();
sound = pvz.getSound();
game.setMusic(music);
game.setSound(sound);
if(gamestarted){
// manages the playing of the sounds here
if(music && inalmanac && !mplaying){
pickingmusic.loop();
mplaying = true;
}
if (music && !inalmanac){
pickingmusic.stop();
if (!mplaying){
playingmusic.loop();
}
}
if(music && !mplaying && !lose && !inalmanac){
playingmusic.loop();
mplaying = true;
}
if(!music){
playingmusic.stop();
pickingmusic.stop();
mplaying = false;
}
if(!lose && !win){
// if the player hasn't won or lost, game is being played
if (!paused){
if(keys[KeyEvent.VK_W]){ // move player up
player.move(Shooter.UP);
keys[KeyEvent.VK_W] = false;
}
if(keys[KeyEvent.VK_S]){ // move player down
player.move(Shooter.DOWN);
keys[KeyEvent.VK_S] = false;
}
if(keys[KeyEvent.VK_SPACE]){ // player shoots peas
game.playerShoot();
keys[87] = false;
}
if(keys[KeyEvent.VK_ESCAPE] || keys[KeyEvent.VK_P]){ // pauses game
paused = true;
game.setPaused(true);
keys[KeyEvent.VK_ESCAPE] = false;
}
// all functions needed for game to be played
game.makeSun();
game.moveSun();
game.rechargePlantCards();
game.actionCount();
game.plantsAction();
game.moveZombies();
game.checkSquash();
game.checkDancing();
game.checkFirePeas();
game.moveBullets();
game.checkBulletCollisions();
game.checkZombieCollisions();
game.checkDeaths();
game.setPics();
if(clicked){
game.clickPlant(pos[0],pos[1]); // checks if seed packet was clicked
game.clickShovel(pos[0],pos[1]); // checks if shoverl was clicked
if (pos[1] > 87 || pos[0] > 454){ // plants plants or removes plants with shovel
game.plantPlant(pos[0],pos[1]);
game.removePlant(pos[0],pos[1]);
}
game.addSun(pos); // checks if user clicked on sun
clicked = false;
}
// different game modes
if(mode.equals("survival")){
game.loadZombiesSurvival(survivalwave);
if(survivalwavecount > 6000){ // if 60 seconds passed, start a new wave of zombies
int random = (int)(Math.random()*5);
if(random == 0){
survivalwave ++;
survivalwavecount = 0;
}
}
survivalwavecount ++;
}
if(mode.equals("levels")){
if(!loaded){
game.loadZombies(level);
loaded = true;
}
game.playLevel(level);
// checks to see if the user has won
if(game.checkWin()){
win = true;
game.setWin(true);
}
}
// checks to see if the user has lost
if(game.checkLose()){
lose = true;
game.setLose(true);
playingmusic.stop();
if(sound){
losemusic.play();
}
}
}
if(paused){
// user can enter the almanac, exit the game, or restart the game from the pause menu
if(keys[KeyEvent.VK_ESCAPE] || keys[KeyEvent.VK_P]){ // unpauses game
paused = false;
game.setPaused(false);
keys[KeyEvent.VK_ESCAPE] = false;
}
if(clicked){
if(pauseBackToGame.contains(pos[0],pos[1])){ // unpauses game
paused = false;
game.setPaused(false);
}
if(pauseAlmanacRect.contains(pos[0],pos[1])){ // view almanac
mplaying = false;
playingmusic.stop();
inalmanac = true;
AlmanacMenu newalmanac = new AlmanacMenu(pvz.getMenu(),"game",pvz);
pvz.dispose();
}
if(pauseRestartRect.contains(pos[0],pos[1])){ // restart game
if(mode.equals("levels")){
mplaying = false;
playingmusic.stop();
pvz.getMenu().adventure();
pvz.dispose();
resetEverything();
}
else{
mplaying = false;
playingmusic.stop();
pvz.getMenu().survival();
pvz.dispose();
resetEverything();
}
}
if(pauseMainRect.contains(pos[0],pos[1])){ // main menu
mplaying = false;
pvz.getMenu().getMenuPanel().setInGame(false);
playingmusic.stop();
pvz.setMenu();
pvz.dispose();
resetEverything();
}
if(musicCheckBox.contains(pos[0],pos[1])){ // changing music
if (!music){
pvz.setMusic(true);
music = true;
game.setMusic(music);
clicked = false;
}
else if(music){
pvz.setMusic(false);
music = false;
mplaying = false;
game.setMusic(music);
clicked = false;
}
}
if(soundFXCheck.contains(pos[0],pos[1])){ // changing sound effects
if(!sound){
pvz.setSound(true);
sound = true;
game.setSound(sound);
clicked = false;
}
else if(sound){
pvz.setSound(false);
sound = false;
game.setSound(sound);
clicked = false;
}
}
clicked = false;
}
}
}
if(lose || win){
playingmusic.stop();
mplaying = false;
if(clicked){
if(mode.equals("levels")){
// if the user has lost, they can choose to play
// again or exit to menu
if(levelsLoseWinMenu.contains(pos[0],pos[1])){ // go back to main menu
losemusic.stop();
mplaying = false;
pvz.setMenu();
pvz.dispose();
resetEverything();
}
if(levelsLoseWinAgain.contains(pos[0],pos[1])){ // play again
losemusic.stop();
mplaying = false;
pvz.getMenu().adventure();
pvz.dispose();
resetEverything();
}
}
if(mode.equals("survival")){
score = game.getScore();
if(!scoreAdded){ // adds score to list of scores in highscores.txt
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(score);
scoreAdded = true;
try{
Scanner inFile = new Scanner(new BufferedReader(new FileReader("highscores.txt")));
while(inFile.hasNextInt()){ // takes all scores saved in textfile
scores.add(inFile.nextInt());
}
inFile.close();
Collections.sort(scores); // sorts highscores
Collections.reverse(scores); // reverses order from highest to lowest
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("highscores.txt")));
for(int i = 0; i < Math.min(scores.size(),50); i ++){ // rewrites new sorted highscores
outFile.println(scores.get(i));
}
outFile.close();
}
catch(Exception excep){
System.out.println("Oh no! "+excep);
}
}
if(survivalLoseWinAgain.contains(pos[0],pos[1])){ // play again
losemusic.stop();
mplaying = false;
pvz.getMenu().survival();
pvz.dispose();
resetEverything();
}
if(survivalLoseWinMenu.contains(pos[0],pos[1])){ // main menu
losemusic.stop();
mplaying = false;
pvz.setMenu();
pvz.getMenu().getMenuPanel().setInGame(false);
pvz.dispose();
resetEverything();
}
if(survivalLoseWinScore.contains(pos[0],pos[1])){ // view high scores
losemusic.stop();
mplaying = false;
pvz.getMenu().HighScore();
pvz.getMenu().getMenuPanel().setInGame(false);
pvz.dispose();
resetEverything();
}
}
clicked = false;
}
}
}
repaint();
}
public void paintComponent(Graphics g){
if (gamestarted){
game.draw(g,this,pos[0],pos[1]);
}
}
}
class GameMap{
// game map controls & manipulates all the variables in the game through functions, it also controls the drawing
private boolean lose,paused,win;
private boolean music,sound;
private Shooter player;
private String mode;
private Image backgroundPic,currentPlant,topShovel,usedShovel,mouseShovel;
private Image pauseTomb,pauseMenu,pauseMusic,pauseBack,pauseAlmanac,pauseRestart;
private Image musicPic,soundFXPic;
private Image levelsLosePic,levelsLoseMenuPic,levelsLoseAgainPic;
private Image levelsWinPic,levelsWinMenuPic,levelsWinAgainPic;
private Image survivalLosePic,survivalLoseMenuPic,survivalLoseAgainPic,survivalLoseScorePic;
private Image winScreen,winMenuPic;
private int plantingPlant,sunCost; // plant chosen to be planted on grid
private int sunEnergy,suncounter,spawnCount;
private int score;
private Rectangle shovel = new Rectangle(454,0,70,72);
// pause menu rectangles
private Rectangle pauseBackToGame = new Rectangle(245,458,330,76);
private Rectangle pauseAlmanacRect = new Rectangle(308,309,201,37);
private Rectangle pauseRestartRect = new Rectangle(308,351,201,37);
private Rectangle pauseMainRect = new Rectangle(308,393,201,37);
// rectangles for end game options in the levels game mode
private Rectangle levelsLoseWinMenu = new Rectangle(427,550,151,34);
private Rectangle levelsLoseWinAgain = new Rectangle(224,550,151,34);
// rectangles for end game options in the survival game mode
private Rectangle survivalLoseWinAgain = new Rectangle(127,550,151,34);
private Rectangle survivalLoseWinMenu = new Rectangle(330,550,151,34);
private Rectangle survivalLoseWinScore = new Rectangle(533,550,151,34);
private Plant[][] plants = new Plant[5][9]; // grid of plants
private PlantCard[] seeds = new PlantCard[7]; // seeds that the player will be using
private ArrayList<ArrayList<Zombie>> zombies = new ArrayList<ArrayList<Zombie>>(); // zombies on the field
private ArrayList<Zombie> zombiesWait = new ArrayList<Zombie>(); // zombies lined up to be deployed onto the field
private ArrayList<ArrayList<Bullet>> plantBullets = new ArrayList<ArrayList<Bullet>>(); // bullets in game
private ArrayList<Sun> suns = new ArrayList<Sun>(); // sun in game
private ArrayList<Integer> level1ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,2500,2500,2000,2000,2000,1500,1500,1500,1500)); // times for zombies to come in on level 1
private ArrayList<Integer> level2ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,2000,2500,2000,2000,1500,1500,1500,1500,1500,1000,1000,1000,1000,1000,750,750,750,750,750)); // times for zombies to come in on level 2
// times for zombies to come in on level 3
private ArrayList<Integer> level3ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,200,2500,2000,2000,1500,1500,1000,1000,1000,1000,750,750,750,750,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500));
private static Random random = new Random();
private static final int[][] dimensions = new int[][]{{90,78},{80,80},{298,160},{80,80},{100,100},{90,78},{80,116},{90,78},
{108,72},{250,150},{1460,80},{100,48},{80,92},{120,110},{100,100},
{140,104},{100,100},{170,78}}; // dimensions of the plant images
public GameMap(){
lose = false;
win = false;
paused = false;
mode = "levels";
plantingPlant = -1;
player = new Shooter();
backgroundPic = new ImageIcon("Images/Game/lawn.png").getImage();
sunEnergy = 500;
suncounter = 0;
score = 0;
setArrayLists();
Plant.getImages();
Zombie.getImages();
// shovel images
topShovel = new ImageIcon("Images/Game/Shovel.png").getImage();
usedShovel = new ImageIcon("Images/Game/SelectedShovel.png").getImage();
mouseShovel = new ImageIcon("Images/Game/MouseShovel.png").getImage();
// pause menu images
pauseTomb = new ImageIcon("Images/Game/PauseMenu.png").getImage();
pauseMenu = new ImageIcon("Images/Game/PauseMenu - MainMenu.png").getImage();
pauseMusic = new ImageIcon("Images/Game/PauseMenu - Music.png").getImage();
pauseBack = new ImageIcon("Images/Game/PauseMenu - Resume.png").getImage();
pauseAlmanac = new ImageIcon("Images/Game/PauseMenu - Almanac.png").getImage();
pauseRestart = new ImageIcon("Images/Game/PauseMenu - Restart.png").getImage();
musicPic = new ImageIcon("Images/Game/PauseMenu - Music.png").getImage();
soundFXPic = new ImageIcon("Images/Game/PauseMenu - SoundFX.png").getImage();
// images for losing when playing the levels mode
levelsLosePic = new ImageIcon("Images/Game/Levels/Lose.png").getImage();
levelsLoseMenuPic = new ImageIcon("Images/Game/Levels/LoseMainMenu.png").getImage();
levelsLoseAgainPic = new ImageIcon("Images/Game/Levels/LosePlayAgain.png").getImage();
// images for winning when playing the levels mode
levelsWinPic = new ImageIcon("Images/Game/Levels/Win.png").getImage();
levelsWinMenuPic = new ImageIcon("Images/Game/Levels/WinMainMenu.png").getImage();
levelsWinAgainPic = new ImageIcon("Images/Game/Levels/WinPlayAgain.png").getImage();
//images for losing when playing survival mode
survivalLosePic = new ImageIcon("Images/Game/Survival/Lose.png").getImage();
survivalLoseMenuPic = new ImageIcon("Images/Game/Survival/LoseMainMenu.png").getImage();
survivalLoseAgainPic = new ImageIcon("Images/Game/Survival/LosePlayAgain.png").getImage();
survivalLoseScorePic = new ImageIcon("Images/Game/Survival/LoseHighscores.png").getImage();
suns.add(new Sun(new double[]{500,120},230)); // there is always a single sun at the start of a game
}
public void setLose(boolean lose){
this.lose = lose;
}
public boolean getLose(){
return lose;
}
public void setWin(boolean win){
this.win = win;
}
public void setPaused(boolean paused){
this.paused = paused;
}
public void setMode(String mode){
this.mode = mode;
}
public void setMusic(boolean music){
this.music = music;
}
public void setSound(boolean sound){
this.sound = sound;
}
public int getScore(){
return score;
}
public void resetEverything(){
//resets the game and changes all the variables to their defaults
lose = false;
win = false;
paused = false;
plantingPlant = -1;
sunEnergy = 50;
suncounter = 0;
spawnCount = 0;
score = 0;
plants = new Plant[5][9];
seeds = new PlantCard[7];
zombies = new ArrayList<ArrayList<Zombie>>();
zombiesWait = new ArrayList<Zombie>();
plantBullets = new ArrayList<ArrayList<Bullet>>();
suns = new ArrayList<Sun>();
suns.add(new Sun(new double[]{500,120},230));
setArrayLists();
level1ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,2500,2500,2000,2000,2000,1500,1500,1500,1500));
level2ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,2500,2500,2500,2000,2000,2000,2000,2000,2000,2000,2000,2000,1500,2000,2500,2000,1500,1000,1000));
level3ZombieCome = new ArrayList<Integer>(Arrays.asList(2500,200,2500,2000,2000,1500,1500,1000,1000,1000,1000,750,750,750,750,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,750,750,750,750,750,500,500,500,500,500));
}
public void setArrayLists(){
//sets up all the arraylists
for(int i = 0; i < 5; i ++){
ArrayList<Zombie> tmpZ = new ArrayList<Zombie>();
ArrayList<Bullet> tmpB = new ArrayList<Bullet>();
zombies.add(tmpZ);
plantBullets.add(tmpB);
}
}
public void setSeeds(PlantCard[] seeds){
this.seeds = seeds;
}
public void addPlant(int[] pos, int plant){ // plant a plant onto the grid
plants[pos[1]][pos[0]] = new Plant(plant,pos);
for (int i=0; i<seeds.length; i++){
if (seeds[i] != null){
if (seeds[i].getType() == plantingPlant){
seeds[i].setActive(false);
}
}
}
}
public void rechargePlantCards(){
// recharges plantcards that have been used
// goes through the entire list of seeds, if they are unactive, check to see
// if they are ready to become active, if so activate them again
for(int i=0; i<seeds.length; i++){
if(seeds[i] != null){
if(!seeds[i].getActive()){
if(seeds[i].getCharge() >= seeds[i].getRecharge()){
seeds[i].setActive(true);
seeds[i].setCharge(0);
}
else{
seeds[i].addCharge();
}
}
}
}
}
public void removePlant(int[] pos){ // player uses shovel to get rid of plant on grid
plants[pos[1]][pos[0]] = null;
}
public void makeSun(){
// makes a sun on the map every 10 seconds
if(suncounter == 1000){
int pos = random.nextInt(500)+100;
int land = random.nextInt(300)+230;
suns.add(new Sun(new double[]{pos,120},land));
suncounter = 0;
}
else{
suncounter ++;
}
}
public void moveSun(){
// moves sun down the screen
for(int i = suns.size()-1; i > -1; i--){
suns.get(i).move();
if(suns.get(i).timeUp()){ // if user doesn't pick up the sun, it eventually disappears
suns.remove(i);
}
}
}
public void addSun(int[] pos){
// adds sun to sunEnergy
for(int i = suns.size()-1; i > -1; i--){
if(suns.get(i).checkCollision(pos)){
sunEnergy += 25;
suns.remove(i);
break;
}
}
}
public void plantsAction(){
// controls the actions of all the plants in the game
// peashooters are programmed to shoot, sunflowers make sun,
// cherrybombs explode, etc.
for(int i =0; i <5; i ++){
for(int j = 0; j < 9; j++){
if(plants[i][j] != null){
if(plants[i][j].isReady()){
int[] ppos = plants[i][j].getPos();
if(plants[i][j].getType() == Plant.PEASHOOTER || plants[i][j].getType() == Plant.REPEATER || plants[i][j].getType() == Plant.GATLINGPEA){
if(zombies.get(i).size() > 0){
plantBullets.get(ppos[1]).add(new Bullet(Bullet.PEASHOOTER,new double[]{ppos[0]*80+115,ppos[1]}));
}
}
if(plants[i][j].getType() == Plant.SUNFLOWER || plants[i][j].getType() == Plant.TWINSUNFLOWER){
for(int a = 0; a < plants[i][j].getSun(); a++){
int landx = random.nextInt(61)-30;
int landy = random.nextInt(31);
suns.add(new Sun(new double[]{ppos[0]*80+52+landx,ppos[1]*98+110},ppos[1]*98+120+landy));
}
}
if(plants[i][j].getType() == Plant.CHERRYBOMB && !plants[i][j].getExplode()){
for(int a = i+1; a > i-2; a--){ // damages zombies around it
if(a > -1 && a < 5){
if(zombies.get(a).size() > 0){
for(int b = zombies.get(a).size()-1; b >-1; b--){
double[] zpos = zombies.get(a).get(b).getPos();
if(zpos[0] < (ppos[0]+2)*80+35 && zpos[0] > (ppos[0]-2)*80+35){
zombies.get(a).get(b).burnt(plants[i][j].getDamage());
}
}
}
}
}
}
if(plants[i][j].getType() == Plant.JALAPENO && !plants[i][j].getExplode()){ // damages zombies in same row
for(int a = zombies.get(i).size()-1; a >-1; a--){
zombies.get(i).get(a).burnt(plants[i][j].getDamage());
}
}
if(plants[i][j].getType() == Plant.SNOWPEA){
if(zombies.get(i).size() > 0){
plantBullets.get(ppos[1]).add(new Bullet(Bullet.SNOWPEA,new double[]{ppos[0]*80+115,ppos[1]}));
}
}
if(plants[i][j].getType() == Plant.PIKAPEA){
if(zombies.get(i).size() > 0){
plantBullets.get(ppos[1]).add(new Bullet(Bullet.PIKAPEA,new double[]{ppos[0]*80+115,ppos[1]}));
}
}
if(plants[i][j].getType() == Plant.FIREPEA){
if(zombies.get(i).size() > 0){
plantBullets.get(ppos[1]).add(new Bullet(Bullet.FIREPEA,new double[]{ppos[0]*80+115,ppos[1]}));
}
}
if(plants[i][j].getType() == Plant.THREEPEATER){
//for the threepeater if there is a zombie on the lane or on any of the two lanes adjacent to it,
//it will fire one pea down each lane
boolean shoot = false;
for(int a = i+1; a > i-2; a--){ // checks lanes
if(a > -1 && a < 5 && zombies.get(a).size() > 0){
for(int b = zombies.get(a).size()-1; b > -1; b--){
double[] zpos = zombies.get(a).get(b).getPos();
if(zpos[0] >= ppos[0]*80+115){
shoot = true;
}
}
}
}
if(shoot){ // if there is even one zombie, shoots down all 3 rows
for(int a = i+1; a > i-2; a--){
if(a > -1 && a < 5){
plantBullets.get(a).add(new Bullet(Bullet.PEASHOOTER,new double[]{ppos[0]*80+115,a}));
}
}
}
}
if(plants[i][j].getType() == Plant.BONKCHOY){
int[] pos = plants[i][j].getPos();
for(Zombie zombie:zombies.get(i)){
for(int a = (pos[0]-1)*80+75; a < (pos[0]+1)*80+75; a++){
if(zombie.checkCollision(new double[]{a,pos[1]*98+129})){ // checks from left to right, zombies to left of bonk
// choy have priority
zombie.getHit(plants[i][j].getDamage(),Plant.PEASHOOTER); // treated as normal damage, like peashooter
if(a < pos[0]*80+75){
plants[i][j].punch("left");
break;
}
else if(a >= pos[0]*80+75){
plants[i][j].punch("right");
break;
}
}
}
}
}
}
}
}
}
}
public void actionCount(){ // counter for when plants do their actions
for(int i =0; i <5; i ++){
for(int j = 0; j < 9; j++){
if(plants[i][j] != null){
plants[i][j].actionCounter();
}
}
}
player.actionCounter();
}
public void checkFirePeas(){
// turns normal peas that pass through the torchwood
// into fire peas and snowpeas into normal peas
for(int i = 0; i <5; i ++){
for(int j = 0; j < 9; j++){
if(plants[i][j] != null && plants[i][j].getType() == Plant.TORCHWOOD){
for(Bullet bullet:plantBullets.get(i)){
double[] bpos = bullet.getPos();
if(bpos[0] > j*80+35 && bpos[0] < (j+1)*80+35){
bullet.torched(j);
}
}
}
}
}
}
public void checkSquash(){
// squash checks to see if there are zombies to the left or right one tile away and jumps on them and crushes them
for(int i = 0; i <5; i ++){
for(int j = 0; j < 9; j++){
if(plants[i][j] != null && plants[i][j].getType() == Plant.SQUASH && !plants[i][j].getSquash()){
for(Zombie zombie:zombies.get(i)){
int[] ppos = plants[i][j].getPos();
for(int a = (ppos[0]-1)*80+75; a < (ppos[0]+1)*80+75; a++){
if(zombie.checkCollision(new double[]{a,ppos[1]*98+80})){ // checks from left to right, zombies to left have priority
zombie.getHit(plants[i][j].getDamage(),plants[i][j].getType());
if(a < ppos[0]*80+75){
plants[i][j].squashed("left");
break;
}
else if(a >= ppos[0]*80+75){
plants[i][j].squashed("right");
break;
}
}
}
}
}
}
}
}
public void checkDancing(){
// makes dancing zombies spawn other dancing zombies
for(int i = 4; i > -1; i --){
for(int j = zombies.get(i).size()-1; j > -1; j--){
if(zombies.get(i).get(j).getType() == Zombie.DANCING){
System.out.println(zombies.get(i).get(j).getSpawn()+","+zombies.get(i).get(j).getSpawned());
if(zombies.get(i).get(j).getSpawn() && !zombies.get(i).get(j).getSpawned()){ // original dancing zombie can only spawn once
double[] zpos = zombies.get(i).get(j).getPos();
zombies.get(i).get(j).setSpawned(true);
if(zpos[1] - 1 > -1){
Zombie dancing = new Zombie(Zombie.DANCING,new double[]{zpos[0],zpos[1]-1}); // dancing zombies spawned can't spawn their own
dancing.setSpawned(true); // set true as if it already spawned so it can't spawn anymore
zombies.get((int)zpos[1]-1).add(dancing);
}
if(zpos[1] + 1 < 5){
Zombie dancing = new Zombie(Zombie.DANCING,new double[]{zpos[0],zpos[1]+1});
dancing.setSpawned(true);
zombies.get((int)zpos[1]+1).add(dancing);
}
}
else{
zombies.get(i).get(j).spawnCount(); // counts when original dancing zombie will spawn zombies
}
}
}
}
}
public void setPics(){
// sets all images of everything in game
player.setPic();
// plant pics
for(int i =0; i <5; i ++){
for(int j = 0; j < 9; j++){
if(plants[i][j] != null){
plants[i][j].setPic();
if(plants[i][j].getType() == Plant.POTATOMINE && plants[i][j].getExplode()){
plants[i][j].explodedTimer();
}
}
}
}
// zombie pics
for(int i = 4; i > -1; i--){
for(int j = zombies.get(i).size()-1; j > -1; j--){
zombies.get(i).get(j).setPic();
}
}
}
public void moveZombies(){
// move zombies down the lawn towards the house
for(int i = 0; i < 5; i ++){
for(int j = 0; j < zombies.get(i).size(); j++){