-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestGame.java
1621 lines (1511 loc) · 74.3 KB
/
TestGame.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
import javafx.animation.KeyFrame;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBar;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javax.swing.*;
import javafx.scene.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.EventHandler;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Scanner;
import javafx.scene.control.Button;
import javafx.util.Duration;
public class TestGame extends Application implements Map{
Operation operation = new Operation();
int a = 1;//1春,2冬
boolean b = false;
int level = 1;
int heroDir = 2;//1,2,3,4
String gameMusicString = TestGame.class.getResource("Song/2.mp3").toString();
MediaPlayer gameMusic = new MediaPlayer(new Media(gameMusicString));
String winMusicString = TestGame.class.getResource("Song/win.mp3").toString();
MediaPlayer winMusic = new MediaPlayer(new Media(winMusicString));
Timeline monMoveAni;
ImageView showAfter;
ImageView show = new ImageView();
Timeline refresh;
ImageView mshow = new ImageView();
ImageView mshow2 = new ImageView();
ImageView mshow3 = new ImageView();
int isThemeChanged = 0;
Timeline keyboardEnter;
Timeline mM;
KeyCode lastOption = KeyCode.D;
//PathTransition heroMovePT;
boolean canSave = true;
//Pane pMove = new Pane();
//StackPane mazeAndOther = new StackPane();
//GridPane maze = new GridPane();
//BorderPane mazepane = new BorderPane();
//MyMenuBar myMenuBar = new MyMenuBar(this);
@Override
public void start(Stage primaryStage) throws Exception {
String startMusicString = TestGame.class.getResource("Song/1.mp3").toString();
MediaPlayer startmusic = new MediaPlayer(new Media(startMusicString));
//开始界面gif
primaryStage.setTitle("MazeGame");
StackPane startpane = new StackPane();
//Pane startpane = new Pane();
Scene sceneStart = new Scene(startpane,960,768);
Image startImg = new Image("Img/start.png");
startpane.getChildren().add(new ImageView(startImg));
//开始界面按钮
Image story1 = new Image("Img/storymode-1.png");
Image sandbox1 = new Image("Img/sandbox-1.png");
Button story = new Button("",new ImageView("Img/storymode-1.png"));
Button sandbox = new Button("",new ImageView("Img/sandbox-1.png"));
HBox hBox1 = new HBox(214);
hBox1.setPadding(new Insets(470,107,343,107));
hBox1.getChildren().addAll(story,sandbox);
hBox1.setAlignment(Pos.CENTER);
startpane.getChildren().addAll(hBox1);
primaryStage.setScene(sceneStart);
primaryStage.show();
startmusic.play();
story.setOnMousePressed(event -> {
canSave = true;
StackPane pane = new StackPane();
pane.getChildren().add(new ImageView(startImg));
Scene sceneChooseTheme = new Scene(pane,960,768);
HBox hBoxCT = new HBox(214);
hBoxCT.setPadding(new Insets(470,107,343,107));
Button spring = new Button("",new ImageView("Img/spring.png"));
Button winter = new Button("",new ImageView("Img/winter.png"));
//Button returnbt = new Button("",new ImageView("return.png"));
hBoxCT.getChildren().addAll(spring,winter);
pane.getChildren().add(hBoxCT);
primaryStage.setScene(sceneChooseTheme);
primaryStage.show();
spring.setOnMousePressed(event1 -> {
canSave = true;
startmusic.pause();
//playGameMusic(1);
durGame(primaryStage,1,1,1);
//mazeGame(primaryStage,1,1,1);
});
winter.setOnMousePressed(event1 -> {
canSave = true;
a = 2;
startmusic.pause();
//playGameMusic(1);
durGame(primaryStage,1,1,1);
//mazeGame(primaryStage,1,1,1);
});
});
sandbox.setOnMousePressed(event -> {
canSave = false;
StackPane pane2 = new StackPane();
pane2.getChildren().add(new ImageView(startImg));
Scene sceneChooseLevel = new Scene(pane2,960,768);
VBox vBoxCL = new VBox(50);
vBoxCL.setPadding(new Insets(200));
Button level1 = new Button("第一关");
Button level2 = new Button("第二关");
Button level3 = new Button("第三关");
Button level4 = new Button("第四关");
vBoxCL.getChildren().addAll(level1,level2,level3,level4);
vBoxCL.setAlignment(Pos.CENTER);
pane2.getChildren().add(vBoxCL);
primaryStage.setScene(sceneChooseLevel);
primaryStage.show();
level1.setOnMousePressed(event1 -> {
canSave = false;
startmusic.pause();
mazeGame(primaryStage,1,2,1);
});
level2.setOnMousePressed(event1 -> {
canSave = false;
startmusic.pause();
mazeGame(primaryStage,2,2,1);
});
level3.setOnMousePressed(event1 -> {
canSave = false;
startmusic.pause();
mazeGame(primaryStage,3,2,1);
});
level4.setOnMousePressed(event1 -> {
canSave = false;
startmusic.pause();
mazeGame(primaryStage,4,2,1);
});
});
}
public void durGame(Stage primaryStage,int no,int storyOrSand,int DurOrNot){
Timeline showDur1 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/durup.png"));
BorderPane mazepane1 = new BorderPane();
GridPane maze1 = new GridPane();
StackPane titlePane1 = new StackPane();
Image title1 = new Image("Img/ghp.png");
if(a == 2) title1 = new Image("Img/shp.png");
titlePane1.getChildren().add(new ImageView(title1));
Image[] phpTitle1 = new Image[14];
for(int i = 0; i < 14; i++){
phpTitle1[i] = new Image("Img/php-"+i+".png");
}
titlePane1.getChildren().add(new ImageView(phpTitle1[operation.php]));
mazepane1.setTop(durup);
mazepane1.setCenter(titlePane1);
mazepane1.setBottom(maze1);
print(a,maze1,no,1);
print(a,titlePane1,no);
Image maze11 = new Image("Img/maze1.gif");
Scene maze1S = new Scene(mazepane1);
mazepane1.getChildren().add(new ImageView(maze11));
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,1,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,1,1,0);
});
}));
Timeline showDur2 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/durup.png"));
BorderPane mazepane1 = new BorderPane();
GridPane maze1 = new GridPane();
StackPane titlePane1 = new StackPane();
Image title1 = new Image("Img/ghp.png");
if(a == 2) title1 = new Image("Img/shp.png");
titlePane1.getChildren().add(new ImageView(title1));
Image[] phpTitle1 = new Image[14];
for(int i = 0; i < 14; i++){
phpTitle1[i] = new Image("Img/php-"+i+".png");
}
titlePane1.getChildren().add(new ImageView(phpTitle1[operation.php]));
mazepane1.setTop(durup);
mazepane1.setCenter(titlePane1);
mazepane1.setBottom(maze1);
print(a,maze1,no,1);
print(a,titlePane1,no);
Image maze11 = new Image("Img/maze2.gif");
Scene maze1S = new Scene(mazepane1);
mazepane1.getChildren().add(new ImageView(maze11));
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,2,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,2,1,0);
});
}));
Timeline showDur3 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/durup.png"));
BorderPane mazepane1 = new BorderPane();
GridPane maze1 = new GridPane();
StackPane titlePane1 = new StackPane();
Image title1 = new Image("Img/ghp.png");
if(a == 2) title1 = new Image("Img/shp.png");
titlePane1.getChildren().add(new ImageView(title1));
Image[] phpTitle1 = new Image[14];
for(int i = 0; i < 14; i++){
phpTitle1[i] = new Image("Img/php-"+i+".png");
}
titlePane1.getChildren().add(new ImageView(phpTitle1[operation.php]));
mazepane1.setTop(durup);
mazepane1.setCenter(titlePane1);
mazepane1.setBottom(maze1);
print(a,maze1,no,1);
print(a,titlePane1,no);
Image maze11 = new Image("Img/maze3.gif");
Scene maze1S = new Scene(mazepane1);
mazepane1.getChildren().add(new ImageView(maze11));
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,3,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,3,1,0);
});
}));
Timeline showDur4 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/durup.png"));
BorderPane mazepane1 = new BorderPane();
GridPane maze1 = new GridPane();
StackPane titlePane1 = new StackPane();
Image title1 = new Image("Img/ghp.png");
if(a == 2) title1 = new Image("Img/shp.png");
titlePane1.getChildren().add(new ImageView(title1));
Image[] phpTitle1 = new Image[14];
for(int i = 0; i < 14; i++){
phpTitle1[i] = new Image("Img/php-"+i+".png");
}
titlePane1.getChildren().add(new ImageView(phpTitle1[operation.php]));
mazepane1.setTop(durup);
mazepane1.setCenter(titlePane1);
mazepane1.setBottom(maze1);
print(a,maze1,no,1);
print(a,titlePane1,no);
Image maze11 = new Image("Img/maze4.gif");
Scene maze1S = new Scene(mazepane1);
mazepane1.getChildren().add(new ImageView(maze11));
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
System.out.println("pointed");
mazeGame(primaryStage,4,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,4,1,0);
});
}));
/*Timeline showDur1 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/dur1.png"));
if(a == 2) durup = new ImageView(new Image("Img/sdur1.png"));
ImageView maze11 = new ImageView(new Image("Img/maze1.gif"));
StackPane durPane = new StackPane();
durPane.getChildren().addAll(durup,maze11);
Scene maze1S = new Scene(durPane);
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,1,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,1,1,0);
});
}));
Timeline showDur2 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/dur2.png"));
if(a == 2) durup = new ImageView(new Image("Img/sdur2.png"));
ImageView maze11 = new ImageView(new Image("Img/maze2.gif"));
StackPane durPane = new StackPane();
durPane.getChildren().addAll(durup,maze11);
Scene maze1S = new Scene(durPane);
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,2,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,2,1,0);
});
}));
Timeline showDur3 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/dur3.png"));
if(a == 2) durup = new ImageView(new Image("Img/sdur3.png"));
ImageView maze11 = new ImageView(new Image("Img/maze3.gif"));
StackPane durPane = new StackPane();
durPane.getChildren().addAll(durup,maze11);
Scene maze1S = new Scene(durPane);
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,3,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,3,1,0);
});
}));
Timeline showDur4 = new Timeline(new KeyFrame(Duration.seconds(1),event ->{
ImageView durup = new ImageView(new Image("Img/dur3.png"));
if(a == 2) durup = new ImageView(new Image("Img/sdur3.png"));
ImageView maze11 = new ImageView(new Image("Img/maze4.gif"));
StackPane durPane = new StackPane();
durPane.getChildren().addAll(durup,maze11);
Scene maze1S = new Scene(durPane);
primaryStage.setScene(maze1S);
primaryStage.show();
maze1S.setOnMousePressed(event1 -> {
mazeGame(primaryStage,4,1,0);
});
maze1S.setOnKeyPressed(event1 -> {
mazeGame(primaryStage,4,1,0);
});
}));*/
if(no == 1)showDur1.play();
else if(no == 2)showDur2.play();
else if(no == 3)showDur3.play();
else if(no == 4)showDur4.play();
}
public void mazeGame(Stage primaryStage,int no,int storyOrSand,int DurOrNot) {
gameMusic.play();
gameMusic.setCycleCount(Timeline.INDEFINITE);
//迷宫界面
MyMenuBar myMenuBar = new MyMenuBar(this);
myMenuBar.setStage(primaryStage);
BorderPane mazepane = new BorderPane();
StackPane mazeAndOther = new StackPane();
GridPane maze = new GridPane();
Pane pMove = new Pane();
Pane mMove = new Pane();
Pane mMove2 = new Pane();
Pane mMove3 = new Pane();
mazeAndOther.getChildren().add(maze);//mazeAndOther = maze + mMove + pMove
mazeAndOther.getChildren().add(pMove);
mazeAndOther.getChildren().addAll(mMove,mMove2,mMove3);
//血条界面
StackPane titlePane = new StackPane();
Image title = new Image("Img/ghp.png");
if (a == 2) title = new Image("Img/shp.png");
titlePane.getChildren().add(new ImageView(title));
Image[] phpTitle = new Image[14];
for (int i = 0; i < 14; i++) {
phpTitle[i] = new Image("Img/php-" + i + ".png");
}
titlePane.getChildren().add(new ImageView(phpTitle[operation.php]));
//全部游戏界面上中下
mazepane.setTop(myMenuBar);
mazepane.setCenter(titlePane);
mazepane.setBottom(mazeAndOther);
//打印开场
printMap(maze,no);
printP(pMove,KeyCode.L);
//printM(mMove,mMove2,mMove3,no);
print(a, titlePane, no);
Scene mazeScene = new Scene(mazepane);///960,768
primaryStage.setScene(mazeScene);
primaryStage.show();
mazeScene.setOnKeyPressed((KeyEvent event) -> {
if(event.getCode() == KeyCode.W||event.getCode() == KeyCode.A||event.getCode() == KeyCode.S||event.getCode() == KeyCode.D)
lastOption = event.getCode();
if (event.getCode() == KeyCode.H) {
showHelpScene(a);
}
if (event.getCode() == KeyCode.C) {
showScoreScene();
}
if (event.getCode() == KeyCode.K) {
playKillMusic();
}
if (event.getCode() == KeyCode.P) {
playPickMusic();
}
operation.heroMove(event.getCode(), no);
if (operation.pPos2[0] != pPos[0] || operation.pPos2[1] != pPos[1]) {
playWalkMusic();
}
printMap(maze,no);
if(event.getCode() == KeyCode.K){
printPK(pMove,lastOption);
}else {
printP(pMove,event.getCode());
}
print(a, titlePane, no);
if (operation.isKilled) {
if (!this.b) {
showDieScene(mazepane, primaryStage);
}
}
if (pPos[0] == 13 && pPos[1] == 19) {
level++;
refresh.stop();
if (storyOrSand == 1) {
operation.ifContinue[no] = true;
for (int i = 0; i < operation.treasure2.length; i++) {
for (int j = 0; j < operation.treasure2[0].length; j++) {
operation.treasure[i][j] = operation.treasure2[i][j];
}
}
for (int i = 0; i < operation.monster2.length; i++) {
for (int j = 0; j < operation.monster2[0].length; j++) {
operation.monster[i][j] = operation.monster2[i][j];
}
}
for (int i = 0; i < operation.addHp.length; i++) {
for (int j = 0; j < operation.addHp[0].length; j++) {
operation.addHp[i][j] = operation.addHp2[i][j];
}
}
pPos[0] = 1;
pPos[1] = 0;
operation.size = 0;
operation.record[0][0] = 1;
operation.record[0][1] = 0;
this.b = false;
if (no < 4) {
durGame(primaryStage, no + 1, 1, 1);
} else if (no == 4 || level == 5) {
gameMusic.stop();
showFinishSceneStory(primaryStage);
}
} else if (storyOrSand == 2) {
showFinishSceneSandbox(primaryStage);
}
}
});
monMoveAni = new Timeline(new KeyFrame(Duration.seconds(0.7), event -> {
if (operation.isKilled) {
if (!this.b) {
showDieScene(mazepane, primaryStage);
}
}
if (!operation.isKilled) {
if(operation.monster[0][0] < 0) mazeAndOther.getChildren().removeAll(mMove);
if(operation.monster[1][0] < 0) mazeAndOther.getChildren().removeAll(mMove2);
if(operation.monster[2][0] < 0) mazeAndOther.getChildren().removeAll(mMove3);
operation.monsterMove(pPos, no);
printM(mMove,mMove2,mMove3,no);
print(a, titlePane, no);
}
}));
if(no != 4){
monMoveAni.stop();
}
if(no == 4){
monMoveAni.setCycleCount(Timeline.INDEFINITE);
monMoveAni.play();
}
refresh = new Timeline(new KeyFrame(Duration.seconds(0.1),event -> {
if(isThemeChanged != 0){
printMap(maze,no);
print(a,maze,no,3);
if(no == 3) printM(mMove,mMove2,mMove3,no);
print(a,titlePane,no);
isThemeChanged = 0;
}
}));
refresh.setCycleCount(Timeline.INDEFINITE);
refresh.play();
}
public void showFinishSceneSandbox(Stage primaryStage){
gameMusic.stop();
winMusic.play();
winMusic.setCycleCount(Timeline.INDEFINITE);
StackPane finishPane = new StackPane(new ImageView("/Img/beijing.jpg"));
Scene finishScene = new Scene(finishPane);
VBox vBox = new VBox(200);
Text text = new Text("You Win!");
text.setFont(Font.font("宋体",FontWeight.BOLD,70));
Button button = new Button("结束");
vBox.getChildren().addAll(text,button);
vBox.setAlignment(Pos.CENTER);
finishPane.getChildren().add(vBox);
primaryStage.setScene(finishScene);
primaryStage.show();
button.setOnMousePressed(event -> System.exit(0));
}
public void showFinishSceneStory(Stage primaryStage){
b = true;
StackPane backPane = new StackPane(new ImageView("/Img/beijing.jpg"));
VBox vBoxName = new VBox(40);
vBoxName.setPadding(new Insets(150,200,200,200));
Text score = new Text(""+(int)((((3*operation.treasurecount)+(10*operation.monstercount)+(3*operation.php)+(7*operation.dps))/(0.2*(operation.stepcount+5)))*100)+"!");
score.setFont(Font.font("宋体",FontWeight.BOLD,70));
Text yourName = new Text("你的名字");
yourName.setFont(Font.font("宋体",FontWeight.BOLD,30));
TextField nameTF = new TextField();
//nameTF.setPrefColumnCount(1);
Button button = new Button("确认");
vBoxName.getChildren().addAll(score,yourName,nameTF,button);
vBoxName.setAlignment(Pos.CENTER);
backPane.getChildren().add(vBoxName);
Scene finishSceneStory = new Scene(backPane);
primaryStage.setScene(finishSceneStory);
primaryStage.show();
button.setOnMousePressed(event -> {
operation.name = nameTF.getText();
if(nameTF.getText().length() == 0){
operation.name = "游客";
}
try {
showRankScene(primaryStage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
});
}
public void playKillMusic(){
String killMusicString = TestGame.class.getResource("Song/attack.mp3").toString();
MediaPlayer killMusic = new MediaPlayer(new Media(killMusicString));
killMusic.play();
}
public void playWalkMusic(){
String walkMusicString = TestGame.class.getResource("Song/gfoot.mp3").toString();
if(a == 2)walkMusicString = TestGame.class.getResource("Song/sfoot.mp3").toString();
MediaPlayer walkMusic = new MediaPlayer(new Media(walkMusicString));
walkMusic.play();
}
public void playPickMusic(){
String pickMusicString = TestGame.class.getResource("Song/pick.mp3").toString();
MediaPlayer pickMusic = new MediaPlayer(new Media(pickMusicString));
pickMusic.play();
}
public void playFailMusic(){
String failMusicString = TestGame.class.getResource("Song/fail.mp3").toString();
MediaPlayer failMusic = new MediaPlayer(new Media(failMusicString));
failMusic.play();
}
public void showDieScene(BorderPane mazepane,Stage primaryStage){
playFailMusic();
this.b = true;
StackPane diePane = new StackPane();
diePane.getChildren().add(mazepane);
diePane.getChildren().add(new ImageView("Img/die2.png"));
//HBox hBox = new HBox(50);
VBox vBox = new VBox(300);
vBox.setAlignment(Pos.CENTER);
Text text = new Text(" ");
//hBox.setPadding(new Insets(500));
Button button = new Button("结束游戏");
vBox.getChildren().addAll(text,button);
diePane.getChildren().add(vBox);
Scene dieScene = new Scene(diePane);
primaryStage.setScene(dieScene);
primaryStage.show();
button.setOnMousePressed(event -> System.exit(0));
}
public void showHelpScene(int a){
//帮助界面
StackPane helpPane = new StackPane();
Scene helpScene = new Scene(helpPane);
Image help = new Image("Img/ghelp.png");
if(a == 2) help = new Image("Img/shelp.png");
helpPane.getChildren().add(new ImageView(help));
Stage helpStage = new Stage();
helpStage.setScene(helpScene);
helpStage.setTitle("帮助界面");
helpStage.show();
helpScene.setOnMousePressed(event -> {
helpStage.close();
});
}
public void showScoreScene(){
//得分界面
StackPane scorePane = new StackPane();
Scene scoreScene = new Scene(scorePane);
Image setting = new Image("Img/background.png");
scorePane.getChildren().add(new ImageView(setting));
Text score = new Text(20,20,"当前拾取宝物 "+operation.treasurecount+"\n\n当前杀死追兵 "+operation.monstercount+"\n\n当前步数 "+operation.stepcount+"\n\n当前得分 "+(int)((((3*operation.treasurecount)+(10*operation.monstercount)+(3*operation.php)+(7*operation.dps))/(0.2*(operation.stepcount+5)))*100));
scorePane.getChildren().add(score);
score.setFont(Font.font("宋体",FontWeight.BOLD,20));
Stage scoreStage = new Stage();
scoreStage.setTitle("得分界面");
scoreStage.setScene(scoreScene);
scoreStage.show();
scoreScene.setOnMousePressed(event -> {
scoreStage.close();
});
}
public void showRankScene(Stage primaryStage) throws FileNotFoundException {
java.io.File readnumber = new java.io.File("number.txt");
Scanner readnumberS = new Scanner(readnumber);
int n = readnumberS.nextInt();
readnumberS.close();
PrintWriter readnumberP = new PrintWriter(readnumber);
readnumberP.print(n+1);
readnumberP.close();
java.io.File nameNoRepeat = new java.io.File("scores.txt");
Scanner nameNoRep = null;
try {
nameNoRep = new Scanner(nameNoRepeat);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//String[] name0 = new String[1];
while (nameNoRep.hasNext()) {
String name0 = nameNoRep.next();
while (name0.equals(operation.name)) {
//JOptionPane.showMessageDialog(null,"用户名已存在!");
operation.name += ""+(n+1);
Text nameAlready = new Text("用户名已存在!");
nameAlready.setFont(Font.font("宋体",15));
//vBoxName.getChildren().add(nameAlready);
}
while ("".equals(operation.name)){
Text nameNoVoid = new Text("用户名不可为空!");
nameNoVoid.setFont(Font.font("宋体",15));
//vBoxName.getChildren().add(nameNoVoid);
}
int rubbish = nameNoRep.nextInt();
operation.playNumber++;
}
nameNoRep.close();
Scanner nameRecord = null;
try {
nameRecord = new Scanner(nameNoRepeat);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] namePre = new String[operation.playNumber + 1];//用户名数组
for (int i = 0; i < operation.playNumber; i++) {
namePre[i] = nameRecord.next();
int rubbish = nameRecord.nextInt();
}
nameRecord.close();
namePre[operation.playNumber] = operation.name;
Scanner scoresPrevious = null;
try {
scoresPrevious = new Scanner(nameNoRepeat);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int[] scorePrevious = new int[operation.playNumber + 1];//分数数组
for (int i = 0; i < operation.playNumber; i++) {
String rubbish = scoresPrevious.next();
scorePrevious[i] = scoresPrevious.nextInt();
}
scoresPrevious.close();
scorePrevious[operation.playNumber] = (int)((((3*operation.treasurecount)+(10*operation.monstercount)+(3*operation.php)+(7*operation.dps))/(0.2*(operation.stepcount+5)))*100);
//写入名字和分数
BufferedWriter scores = null;
try {
scores = new BufferedWriter(new FileWriter(new File("scores.txt"), true));
} catch (IOException e) {
e.printStackTrace();
}
try {
scores.append(operation.name + " ");
} catch (IOException e) {
e.printStackTrace();
}
try {
scores.append((int)((((3*operation.treasurecount)+(10*operation.monstercount)+(3*operation.php)+(7*operation.dps))/(0.2*(operation.stepcount+5)))*100) + " ");
} catch (IOException e) {
e.printStackTrace();
}
try {
scores.close();
} catch (IOException e) {
e.printStackTrace();
}
int[] scorePaixu = new int[operation.playNumber + 1];
System.arraycopy(scorePrevious, 0, scorePaixu, 0, operation.playNumber);
scorePaixu[operation.playNumber] = (int)((((3*operation.treasurecount)+(10*operation.monstercount)+(3*operation.php)+(7*operation.dps))/(0.2*(operation.stepcount+5)))*100);
Arrays.sort(scorePaixu);
int[] scorePaixu2 = new int[scorePaixu.length]; //分数从大到小
for (int j = 0; j < scorePaixu.length; j++) {
scorePaixu2[j] = scorePaixu[scorePaixu.length - j - 1];
}
int rank = 1;
int x = 0;
Text paihangbang = new Text("排行榜");
Text content = new Text();
String content2 = "";
//System.out.println("\n\n 排行榜 \n");
for (int k = 0; k < operation.playNumber + 1; k++) {//scorePaixu2[]
if (k + 1 > 10) break;//////////
if (k >= 1 && scorePaixu2[k] == scorePaixu2[k - 1]) continue;
int num = 0;
for (int j = 0; j < operation.playNumber + 1; j++) {//scorePrevious[]
if (scorePrevious[j] == scorePaixu2[k]) {
num = j;
String namename = String.format("%10s",namePre[num]);
content2 += "第"+( k + 1)+"名: "+scorePrevious[num]+" "+namename+"\n";
// content[x++].setText("第"+( k + 1)+"名: "+namePre[num]+" "+scorePrevious[num]+"\n");
}
}
}
content.setText(content2);
for (int k = 0; k < operation.playNumber + 1; k++) {
if (k >= 1 && scorePaixu2[k] == scorePaixu2[k - 1]) continue;
if (scorePrevious[operation.playNumber] == scorePaixu2[k]) rank = k + 1;
}
Text ifShangbang = new Text();
if (rank <= 10) {
ifShangbang.setText("恭喜你!你上榜了");
//playWinMusic();
winMusic.play();
winMusic.setCycleCount(Timeline.INDEFINITE);
}
else {
ifShangbang.setText("很遗憾,你没有上榜!");
playFailMusic();
}
Text yourRank = new Text("你的排名: " + rank);
Button exit = new Button("结束游戏");
Button reborn = new Button("再来一局");
HBox hBox = new HBox(50);
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(exit,reborn);
VBox vBox2 = new VBox(20);
vBox2.setAlignment(Pos.CENTER);
vBox2.getChildren().addAll(ifShangbang,paihangbang,content,yourRank,hBox);
StackPane backPane2 = new StackPane(new ImageView("/Img/beijing.jpg"));
backPane2.getChildren().add(vBox2);
Scene paihangbangScene = new Scene(backPane2);
primaryStage.setScene(paihangbangScene);
primaryStage.show();
exit.setOnMousePressed(event1 -> System.exit(0));
reborn.setOnMousePressed(event -> {
clear();
//primaryStage.close();
winMusic.stop();
monMoveAni.stop();
durGame(primaryStage,1,1,1);
});
}
public void clear(){
//operation.ifContinue[no] = true;
for (int i = 0; i < operation.treasure2.length; i++) {
for (int j = 0; j < operation.treasure2[0].length; j++) {
operation.treasure[i][j] = operation.treasure2[i][j];
}
}
for (int i = 0; i < operation.monster2.length; i++) {
for (int j = 0; j < operation.monster2[0].length; j++) {
operation.monster[i][j] = operation.monster2[i][j];
}
}
for (int i = 0; i < operation.addHp.length; i++){
for(int j = 0; j < operation.addHp[0].length; j++){
operation.addHp[i][j] = operation.addHp2[i][j];
}
}
operation.treasurecount = 0;
operation.monstercount = 0;
operation.stepcount = 0;
operation.php = 2;
operation.dps = 1;
pPos[0] = 1;
pPos[1] = 0;
operation.size = 0;
operation.isKilled = false;
operation.record[0][0] = 1;
operation.record[0][1] = 0;
this.b = false;
}
public void printPK(Pane pMove,KeyCode x) {
pMove.getChildren().removeAll(showAfter, show);
show = new ImageView();
pMove.getChildren().add(show);
Image[][] move = new Image[4][4];
for (int j = 0; j < 4; j++) {
if (j == 0) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/melee-0-" + (i + 1) + ".png");
}
}
if (j == 1) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/melee-1-" + (i + 1) + ".png");
}
}
if (j == 2) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/melee-2-" + (i + 1) + ".png");
}
}
if (j == 3) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/melee-3-" + (i + 1) + ".png");
}
}
}
if (x == KeyCode.W) showAfter = new ImageView(new Image("Img/hero-back-1.png"));
if (x == KeyCode.D) showAfter = new ImageView(new Image("Img/hero-right-1.png"));
if (x == KeyCode.S) showAfter = new ImageView(new Image("Img/hero-face-1.png"));
if (x == KeyCode.A) showAfter = new ImageView(new Image("Img/hero-left-1.png"));
showAfter.setX(pPos[1] * 48);
showAfter.setY(pPos[0] * 48);
show.setX(operation.pPos2[1] * 48);
show.setY(operation.pPos2[0] * 48);
Timeline heroAni = new Timeline(new KeyFrame(Duration.millis(40), event -> {
if (x == KeyCode.W) {//上
if (show.getImage() == move[0][0]) {
show.setImage(move[0][1]);
} else if (show.getImage() == move[0][1]) {
show.setImage(move[0][2]);
} else if (show.getImage() == move[0][2]) {
show.setImage(move[0][3]);
} else {
show.setImage(move[0][0]);
}
} else if (x == KeyCode.D) {//右
if (show.getImage() == move[1][0]) {
show.setImage(move[1][1]);
} else if (show.getImage() == move[1][1]) {
show.setImage(move[1][2]);
} else if (show.getImage() == move[1][2]) {
show.setImage(move[1][3]);
} else {
show.setImage(move[1][0]);
}
} else if (x == KeyCode.S) {//下
if (show.getImage() == move[2][0]) {
show.setImage(move[2][1]);
} else if (show.getImage() == move[2][1]) {
show.setImage(move[2][2]);
} else if (show.getImage() == move[2][2]) {
show.setImage(move[2][3]);
} else {
show.setImage(move[2][0]);
}
} else if (x == KeyCode.A) { //左
if (show.getImage() == move[3][0]) {
show.setImage(move[3][1]);
} else if (show.getImage() == move[3][1]) {
show.setImage(move[3][2]);
} else if (show.getImage() == move[3][2]) {
show.setImage(move[3][3]);
} else {
show.setImage(move[3][0]);
}
}
}));
heroAni.setOnFinished(event -> {
pMove.getChildren().removeAll(show);
try {
pMove.getChildren().add(showAfter);
} catch (IllegalArgumentException e) {
}
});
heroAni.setCycleCount(4);
heroAni.play();
}
public void printP(Pane pMove,KeyCode x){
//pMove = new Pane();
pMove.getChildren().removeAll(showAfter,show);
show = new ImageView();
pMove.getChildren().add(show);
Image[][] move = new Image[4][4];
for (int j = 0; j < 4; j++) {
if (j == 0) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/hero-back-" + (i + 1) + ".png");
}
}
if (j == 1) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/hero-right-" + (i + 1) + ".png");
}
}
if (j == 2) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/hero-face-" + (i + 1) + ".png");
}
}
if (j == 3) {
for (int i = 0; i < 4; i++) {
move[j][i] = new Image("Img/hero-left-" + (i + 1) + ".png");
}
}
}
showAfter = new ImageView(move[2][0]);
if (x == KeyCode.W) {
showAfter.setImage(move[0][0]);