-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkIt.java
1440 lines (1211 loc) · 36.6 KB
/
ParkIt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
//Rana Al-Khulaidi
//ICS4U-03
//ParkIt game
//June 5, 2020
//This game allows the user to pick a difficulty and attempt parking a car in a given spot while not exceeding the time and gas limit
//imports
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
import java.awt.Rectangle.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
//this is the main class which loads all the levels depending on difficulty and allows the user to play them in order
public class ParkIt extends JFrame implements ActionListener{
private javax.swing.Timer myTimer;
private GamePanel game1,game2,game3,game;
//arrayLists
private ArrayList<Image> pics=new ArrayList<Image>();
private ArrayList<HousesBlock> block=new ArrayList<HousesBlock>();
private ArrayList<Boulevard> boulevard=new ArrayList<Boulevard>();
private ArrayList<StreetRect> rects=new ArrayList<StreetRect>();
private ArrayList<Car> cars=new ArrayList<Car>();
private ArrayList<TrafficBarrier> barrier=new ArrayList<TrafficBarrier>();
private Image road,heartPic,gameOver,winImage;
private UserCar userCarLevel;
private ParkingSpot parking;
private String level;
private String difficulty,lev1,lev2,lev3;
private MP3 pop;
//this method loads and adds all the basic images and loads level 1
public ParkIt() {
super("PARKIT :)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,650);
myTimer = new javax.swing.Timer(40, this);
//sound from Zapsplat.com
pop=new MP3("pop.mp3");
//allows user to pick difficulty of levels at start of game
pickDifficulty();
try {
road = ImageIO.read(new File("road.png"));
heartPic=ImageIO.read(new File("heart.png"));
gameOver=ImageIO.read(new File("gameOver.png"));
winImage=ImageIO.read(new File("winBg.png"));
}
catch (IOException e) {
}
road=road.getScaledInstance(900,650, Image.SCALE_DEFAULT);
pics.add(road);
pics.add(heartPic);
pics.add(gameOver);
pics.add(winImage);
load(lev1);
add(game);
setResizable(false);
setVisible(true);
}
//this method loads the level file in the parameter and adds all the level components to the proper arrayList
public void load(String fileName){
//this clears out all the arrayLists and variables
if(game!=null){
remove(game);
}
if(cars.size()>0){
cars.clear();
}
if(boulevard.size()>0){
boulevard.clear();
}
if(rects.size()>0){
rects.clear();
}
if(block.size()>0){
block.clear();
}
if(barrier.size()>0){
barrier.clear();
}
//this scans the file and adds the components to the proper arrayList
try{
Scanner inFile = new Scanner(new BufferedReader(new FileReader(fileName)));
//gets level number
level=inFile.nextLine();
//uses the integer given in the file before the objects in order to properly add the correct amount of objects
int boulevNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<boulevNum;n++){
boulevard.add(new Boulevard(inFile.nextLine()));
}
int rectNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<rectNum;n++){
rects.add(new StreetRect(inFile.nextLine()));
}
int carNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<carNum;n++){
cars.add(new Car(inFile.nextLine()));
}
int barrierNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<barrierNum;n++){
barrier.add(new TrafficBarrier(inFile.nextLine()));
}
int blockNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<blockNum;n++){
block.add(new HousesBlock(inFile.nextLine()));
}
int parkNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<parkNum;n++){
parking=new ParkingSpot(inFile.nextLine());
}
int userNum=Integer.parseInt(inFile.nextLine());
for(int n=0;n<userNum;n++){
userCarLevel=new UserCar(inFile.nextLine());
}
inFile.close();
}
catch(IOException ex){
System.out.println("ex");
}
//sets the gamePanel variable as the level that was loaded using the arrayLists that were filled
game = new GamePanel(this,pics,block,boulevard,rects,cars,userCarLevel,barrier,parking,level);
//adds the panel to the JFrame
add(game);
validate();
}
//this method creates an Option Pane that allows the user to pick the difficulty of the levels
public void pickDifficulty(){
String options[] = {"Easy","Medium","Hard"};
int answer = JOptionPane.showOptionDialog(null,"How Difficult?", "Difficulty?", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
//sets level variables to name of file based on choice of difficulty
if (answer == JOptionPane.YES_OPTION){
pop.play();
difficulty="easy";
lev1="level1E.txt";
lev2="level2E.txt";
lev3="level3E.txt";
}
else if (answer == JOptionPane.NO_OPTION){
pop.play();
difficulty="medium";
lev1="level1M.txt";
lev2="level2M.txt";
lev3="level3M.txt";
}
else{
pop.play();
difficulty="hard";
lev1="level1H.txt";
lev2="level2H.txt";
lev3="level3H.txt";
}
}
//this method runs the game
public void actionPerformed(ActionEvent evt){
if(game!= null && game.ready==true ){
game.move();
game.repaint();
//restarts game from level 1 if all lives are lost
if(game.getRestart()){
load(lev1);
}
//proceeds to next level if user parks successfully
else if(game.getNextLev()&&game.getLev().equals("1")){
load(lev2);
game.setNextLev(false);
}
else if(game.getNextLev()&&game.getLev().equals("2")){
load(lev3);
game.setNextLev(false);
}
else if(game.getNextLev()){
game.setWin(true);
}
}
}
public void start(){
myTimer.start();
}
public static void main(String[] arguments) {
Menu frame = new Menu();
}
}
//this class was copied from the provided program on Edsby
class MP3 {
private String filename;
private Player player;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}
public void close() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
}
//this class creates a start screen for the game and allows the user to start the game by pressing on a button
class Menu extends JFrame{
private JLayeredPane layeredPane=new JLayeredPane();
private MP3 pop;
//this method loads all the files and sets the variables
public Menu() {
super("ParkIT");
setSize(900,650);
ImageIcon backPic = new ImageIcon("backgr.png");
ImageIcon startPic = new ImageIcon("startPic.png");
//sound from Zapsplat.com
pop=new MP3("pop.mp3");
JLabel back = new JLabel(backPic);
back.setBounds(0, 0,backPic.getIconWidth(),backPic.getIconHeight());
layeredPane.add(back,1);
//creates a button to start
JButton startBtn = new JButton(startPic);
startBtn.addActionListener(new ClickStart());
startBtn.setBounds(300,350,startPic.getIconWidth(),startPic.getIconHeight());
layeredPane.add(startBtn,2);
setContentPane(layeredPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
//this method displays the instruction screen when the user clicks start
class ClickStart implements ActionListener{
@Override
public void actionPerformed(ActionEvent evt){
//plays sound when button is clicked
pop.play();
setVisible(false);
Instructions iFrame=new Instructions();
}
}
}
//this class creates an instruction screen to show user how to play
class Instructions extends JFrame{
private JLayeredPane layeredPane=new JLayeredPane();
//sound from Zapsplat.com
private MP3 pop;
//this method loods the images and creates a button to start the game
public Instructions() {
super("ParkIT");
setSize(900,650);
ImageIcon backPic = new ImageIcon("instructions.png");
ImageIcon startPic = new ImageIcon("startPic1.png");
pop=new MP3("pop.mp3");
JLabel back = new JLabel(backPic);
back.setBounds(0, 0,backPic.getIconWidth(),backPic.getIconHeight());
layeredPane.add(back,1);
//creates button and adds it to panel
JButton startBtn = new JButton(startPic);
startBtn.addActionListener(new ClickStart());
startBtn.setBounds(360,520,startPic.getIconWidth(),startPic.getIconHeight());
layeredPane.add(startBtn,2);
setContentPane(layeredPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
//starts actual game when the button is pressed
class ClickStart implements ActionListener{
@Override
public void actionPerformed(ActionEvent evt){
pop.play();
setVisible(false);
ParkIt gFrame=new ParkIt();
}
}
}
//this class sets up the levels
class GamePanel extends JPanel {
//initializes variables
private boolean []keys;
public boolean ready=false;
private ParkIt mainFrame;
private UserCar userCar;
private Image road,heart,gameOverPic,winPic,instrucBg;
private int lives,carX,carY;
private String level;
private boolean gameOverScreen,gameOver,restart,startTime,startGas,nextLev,winScreen,win,instrucScreen;
private HousesBlock leftHouseBlock;
private ParkingSpot parking;
private ArrayList<TrafficBarrier> trafficBarriers;
private ArrayList<Boulevard> boulevardList;
private ArrayList<StreetRect> streetLines;
private ArrayList<Car> cars;
private ArrayList<Image> images;
private ArrayList<HousesBlock> blocks;
private Font fontSys=null;
//starting amounts
private double time=21;
private double gas=100;
//amounts lost per each frame
private double loseGas=0.5;
//base time and gas
private final double timeBasic=21;
private final double gasBasic=100;
//sounds from Zapsplat.com
private MP3 bell,crash,pop;
//this method sets all the variables
public GamePanel(ParkIt pGame,ArrayList<Image> pics,ArrayList<HousesBlock> block,ArrayList<Boulevard>boulevards
,ArrayList<StreetRect>roadLines,ArrayList<Car>nonUserCars,UserCar playerCar,ArrayList<TrafficBarrier>barriers,
ParkingSpot park,String levelNum){
fontSys = new Font("Arial",Font.PLAIN,23);
keys = new boolean[KeyEvent.KEY_LAST+1];
addKeyListener(new moveListener());
bell=new MP3("bell.mp3");
crash=new MP3("crash.mp3");
pop=new MP3("pop.mp3");
images=pics;
mainFrame = pGame;
//arrayLists
userCar=playerCar;
trafficBarriers=barriers;
boulevardList=boulevards;
streetLines=roadLines;
cars=nonUserCars;
parking=park;
blocks=block;
blocks=block;
road=pics.get(0);
heart=pics.get(1);
gameOverPic=pics.get(2);
winPic=pics.get(3);
lives=3;
carX=userCar.getPX();
carY=userCar.getPY();
gameOverScreen=false;
gameOver=false;
restart=false;
level=levelNum;
startTime=true;
startGas=false;
nextLev=false;
//changes based on whether you win the screen was displayed
winScreen=false;
win=false;
}
//this methods delays the game for a given length
public static void delay (long len){
try {
Thread.sleep (len);
}
catch (InterruptedException ex) {
}
}
//this method allows game to begin
public void addNotify() {
super.addNotify();
requestFocus();
ready = true;
mainFrame.start();
}
//this method displays all the components of the level
public void paintComponent(Graphics g){
g.drawImage(road,0,0,this);
//goes through each arrayList and displays the images
if(leftHouseBlock!=null){
leftHouseBlock.draw(g);
}
for(HousesBlock b:blocks){
b.draw(g);
}
for(Boulevard b:boulevardList){
b.draw(g);
}
//sets colour for street lines
g.setColor(new Color(255,255,255));
for(StreetRect r:streetLines){
g.drawRect((int)r.getX(),(int)r.getY(),(int)r.getWidth(),(int)r.getHeight());
g.fillRect((int)r.getX(),(int)r.getY(),(int)r.getWidth(),(int)r.getHeight());
}
parking.draw(g);
userCar.draw(g);
for(Car c:cars){
c.draw(g);
}
for(TrafficBarrier t:trafficBarriers){
t.draw(g);
}
//displays information about level,gas, and time
g.setColor(new Color(209, 59,46));
g.fillRoundRect(325,10,120,40,30,20);
g.fillRoundRect(455,10,120,40,30,20);
g.fillRoundRect(585,10,120,40,30,20);
g.setFont(fontSys);
g.setColor(new Color(255, 255, 255));
g.drawString("Level",477,39);
g.drawString(level,540,39);
g.drawString("TIME",340,39);
g.drawString(intTime(),402,39);
g.drawString("GAS",598,39);
g.drawString(intGas(),652,39);
//displays hearts for the amount of lives
for(int x=0;x<lives;x++){
g.drawImage(heart,60+x*55,14,null);
}
//displays game over screen when the user loses all lives
if(gameOverScreen==false && gameOver==true){
g.drawImage(gameOverPic,0,0,null);
//asks user about playing again when this boolean is true
gameOverScreen=true;
}
//displays you win screen when user completes all levels
if(winScreen==false && win==true){
g.drawImage(winPic,0,0,null);
//asks user about playing again when this boolean is true
winScreen=true;
}
}
//this method resets the time when a life is lost
public void resetTime(){
time=timeBasic;
}
//this method resets gas when a life is lost
public void resetGas(){
gas=gasBasic;
}
//this method turns time into a string
public String intTime(){
return Integer.toString((int)(time));
}
//this method turns gas into a string
public String intGas(){
return Integer.toString((int)(gas));
}
//this method allows user to move and does game logic
public void move(){
if(keys[KeyEvent.VK_UP] ){
userCar.moveFront();
//starts reducing gas when player moves
startGas=true;
//allows user to change directions only when moving
if(keys[KeyEvent.VK_RIGHT]){
userCar.changeAngle(5);
}
if(keys[KeyEvent.VK_LEFT] ){
userCar.changeAngle(-5);
}
}
else if(keys[KeyEvent.VK_DOWN] ){
startGas=true;
userCar.moveBack();
if(keys[KeyEvent.VK_RIGHT]){
userCar.changeAngle(5);
}
if(keys[KeyEvent.VK_LEFT] ){
userCar.changeAngle(-5);
}
}
//pauses game when the user presses the space button
else if(keys[KeyEvent.VK_SPACE]) {
//stops reducing time and gas
startTime=false;
startGas=false;
//allows user to pick between continuing or exiting
String options[] = {"Play","Exit"};
int answer = JOptionPane.showOptionDialog(null,"Continue playing?", "Play?", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
keys[KeyEvent.VK_SPACE] = false;
//continues game if user chooses yes
if (answer == JOptionPane.YES_OPTION){
pop.play();
return;
}
else{
pop.play();
System.exit(0);
}
}
else{
//ensures gas isn't getting reduced without movement
startGas=false;
}
//increases speed and causes gas to reduce faster
if(keys[KeyEvent.VK_W]&&userCar.getSpeed()!=10) {
startTime=false;
startGas=false;
loseGas+=0.25;
userCar.setSpeed(userCar.getSpeed()+1);
}
//decreases speed and causes gas to reduce lower
else if(keys[KeyEvent.VK_S]&&userCar.getSpeed()!=2) {
startTime=false;
startGas=false;
loseGas-=0.25;
userCar.setSpeed(userCar.getSpeed()-1);
}
//starts reducing time
if(startTime){
timerCount();
}
//starts reducing gas
if(startGas){
gasCount();
}
//moves all the non-user cars
for(Car c:cars){
c.move();
}
//user loses life if they run out of time or gas
if(gas<=0 || time<=0){
loseLife();
}
//ensures user doesnt leave boundaries
if(userCar.getMidPosX()<0 || userCar.getMidPosX()>900){
loseLife();
}
if(userCar.getMidPosY()<0 || userCar.getMidPosY()>600){
loseLife();
}
//checks for collisions between user car and all other objects
for(TrafficBarrier t:trafficBarriers){
if(userCar.checkCollision(t.getRect())){
crash.play();
//user loses life if they crash
loseLife();
}
}
//checks for collisions between user and objects
for(Car c:cars){
if(userCar.checkCollision(c.getRect())){
crash.play();
loseLife();
}
}
for(Boulevard b:boulevardList){
if(userCar.checkCollision(b.getRect())){
crash.play();
loseLife();
}
}
for(HousesBlock b:blocks){
if(userCar.checkCollision(b.getRect())){
crash.play();
loseLife();
}
}
//checks if user successfully parked
if(checkParking()){
bell.play();
nextLev=true;
//starts next level
}
//checks if game over screen was displayed then displays an option pane for the user
if(gameOver==true && gameOverScreen==true){
//stops game
ready=false;
//allows the user to pick between restarting and exiting
String options[] = {"Play", "Exit"};
int answer = JOptionPane.showOptionDialog(null,"Do you want to play again?", "Play again?", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[1]);
if (answer == JOptionPane.YES_OPTION){
pop.play();
//restarts game
restart=true;
ready=false;
}
else{
pop.play();
System.exit(0);
}
}
//checks if you win screen was displayed then displays an option pane for the user
if(win==true && winScreen==true){
ready=false;
String options[] = {"Play", "Exit"};
int answer = JOptionPane.showOptionDialog(null,"Do you want to play again?", "Play again?", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[1]);
if (answer == JOptionPane.YES_OPTION){
pop.play();
//allows user to repick difficulty and play again
mainFrame.pickDifficulty();
restart=true;
ready=false;
}
else{
pop.play();
System.exit(0);
}
}
}
//this method reduces the time by 0.04 each time the timer restarts
public void timerCount(){
time=time-0.04;
}
public String getLev(){
return level;
}
//reduces gas by set value
public void gasCount(){
gas=gas-loseGas;
}
public boolean getNextLev(){
return nextLev;
}
public boolean getRestart(){
return restart;
}
public void setNextLev(boolean bool){
nextLev=bool;
}
public void setWin(boolean bool){
win=bool;
}
//this method checks if the user car is in the parking spot
public boolean checkParking(){
//current 4 corner points of car
double[] points=userCar.getPoints();
if(parking.checkContains(points)){
return true;
}
else{
return false;
}
}
//this method reduces lives and checks if user has lost all lives
public void loseLife(){
resetTime();
resetGas();
if(lives>1){
lives--;
}
else{
gameOver();
}
resetPos();
}
//this method restarts the position of the car when the user loses a life
public void resetPos(){
//returns car points to original points
userCar=new UserCar(Integer.toString(carX)+","+Integer.toString(carY));
delay(70);
}
public void gameOver(){
gameOver=true;
}
class moveListener implements KeyListener{
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
}
}
//this class creates Boulevard objects
class Boulevard{
private Image boulevardPic,boulevardPicSized,verticalBoulevard,horizontalBoulevard;
private Rectangle2D boulevardRect;
private int posX,posY,width,height;
private String data;
//this method takes in a string and gets all the information
public Boulevard(String data){
try {
verticalBoulevard=ImageIO.read(new File("verticalBoulevard.png"));
horizontalBoulevard=ImageIO.read(new File("horizontalBoulevard.png"));
}
catch (IOException e) {
}
String [] stats = data.split(",");
//gets image type from file
if(stats[0].equals("verticalBoulevard")){
boulevardPic=verticalBoulevard;
}
else{
boulevardPic=horizontalBoulevard;
}
//gets position
posX=Integer.parseInt(stats[1]);
posY=Integer.parseInt(stats[2]);
//rescales image based on orientation
if(stats[3].equals("true")){
boulevardPicSized=boulevardPic.getScaledInstance(70,129, Image.SCALE_DEFAULT);
}
else{
boulevardPicSized=boulevardPic.getScaledInstance(129,70, Image.SCALE_DEFAULT);
}
//gets dimensions
height=boulevardPicSized.getHeight(null);
width=boulevardPicSized.getWidth(null);
//creates rect object to track collisions
boulevardRect=new Rectangle (posX,posY,width,height);
}
public Rectangle2D getRect(){
return boulevardRect;
}
public void draw(Graphics g){
g.drawImage(boulevardPicSized,posX,posY,null);
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public int getPX(){
return posX;
}
public int getPY(){
return posY;
}
}
//this class creates a traffic barrier object
class TrafficBarrier{
private Image barrierPic,barrierPicSized,verticalBarrier,horizontalBarrier;
private Rectangle2D barrierRect;
private String data;
private boolean horizontal;
private int posX,posY,width,height;
public TrafficBarrier(String data){
try {
verticalBarrier = ImageIO.read(new File("verticalBarrier.png"));
horizontalBarrier = ImageIO.read(new File("horizontalBarrier.png"));
}
catch (IOException e) {
}
String [] stats = data.split(",");
//this sets type of object
if(stats[0].equals("verticalBarrier")){
barrierPic=verticalBarrier;
}
else{
barrierPic=horizontalBarrier;
}
//this sets position
posX=Integer.parseInt(stats[1]);
posY=Integer.parseInt(stats[2]);
//resizes based on orientation
if(stats[3].equals("true")){
barrierPicSized=barrierPic.getScaledInstance(50,14, Image.SCALE_DEFAULT);
}
else{
barrierPicSized=barrierPic.getScaledInstance(14,50, Image.SCALE_DEFAULT);
}
height=barrierPicSized.getHeight(null);
width=barrierPicSized.getWidth(null);
barrierRect=new Rectangle (posX,posY,width,height);
}
public Rectangle2D getRect(){
return barrierRect;
}
public void draw(Graphics g){
g.drawImage(barrierPicSized,posX,posY,null);
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public int getPX(){
return posX;
}
public int getPY(){
return posY;
}
}
//this class creates a houses block object
class HousesBlock{
private Image housesBlockPic,housesPicSized,leftBlockPic;
private Rectangle2D housesBlockRect;
private int posX,posY,width,height;
private String data;
public HousesBlock(String data){
try {
leftBlockPic=ImageIO.read(new File("leftBlock.png"));
}
catch (IOException e) {
}
String [] stats = data.split(",");
//sets image
if(stats[0].equals("leftBlock")){
housesBlockPic=leftBlockPic;
}
//resizes
housesPicSized=housesBlockPic.getScaledInstance(78,640, Image.SCALE_DEFAULT);
height=housesPicSized.getHeight(null);
width=housesPicSized.getWidth(null);
//sets position
posX=Integer.parseInt(stats[1]);
posY=Integer.parseInt(stats[2]);
//rect object to track collisions
housesBlockRect=new Rectangle (posX,posY,width,height);
}
public Rectangle2D getRect(){
return housesBlockRect;
}
public void draw(Graphics g){
g.drawImage(housesPicSized,posX,posY,null);
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public int getPX(){
return posX;
}
public int getPY(){
return posY;
}
}
//this class creates a car object for the non-user cars
class Car{
private BufferedImage car1,car2,car3,car4,car5,car6,car7,car8,car9,car10,car,car11;
private Image carPic,carPicSized;
private Rectangle2D carRect;
private final int SPEED=4;
private String data;