文章目录
一、项目效果演示
1.1 游戏主界面
1.2 音乐选择界面
1.3 游戏界面
二、项目结构
2.1 src
2.2 images
2.3 Mstw
2.4 music
三、Code
3.1 Main.java
packageMstw;//用来执行整个程序的主类publicclassMain{publicstaticfinalint screen_width =1920;//设置宽度publicstaticfinalint screen_height =1080;//设置高度publicstaticfinalint sleep_time =10;//设置下落间隔时间publicstaticfinalint reach_time =2;//用来调整的时间publicstaticint note_speed =3;//设置方块下落速度publicstaticint scout;//游戏中的得分数//执行整个程序的主函数publicstaticvoidmain(String[] args){newMstw();}}
3.2 Beat.java
packageMstw;publicclassBeat{privateint time;privateString noteName;publicintgetTime(){return time;}publicvoidsetTime(int time){this.time = time;}publicStringgetNoteName(){return noteName;}publicvoidsetNoteName(String noteName){this.noteName = noteName;}publicBeat(int time,String noteName){super();this.time = time;this.noteName = noteName;}}
3.3 Game.java
packageMstw;importjavax.swing.*;importjava.awt.*;importjava.util.ArrayList;publicclassGameextendsThread{privateImage gameInfoImage =newImageIcon(Main.class.getResource("../images/gameInfo.png")).getImage();privateImage judgementLineImage =newImageIcon(Main.class.getResource("../images/judgementLine.png")).getImage();privateImage noteRouteLineImage =newImageIcon(Main.class.getResource("../images/noteRouteLine.png")).getImage();//每个按键控制一个区域privateImage noteRouteSImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteDImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteFImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteSpaceImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteJImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteKImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage noteRouteLImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();privateImage blueFlareImage;privateImage judgeImage;privateString titleName;//歌曲名称privateString difficulty;//难度privateString musicTitle;//歌曲MP3privateMusic gameMusic;ArrayList<Note> noteList =newArrayList<Note>();publicGame(String titleName,String difficulty,String musicTitle){this.titleName = titleName;this.difficulty = difficulty;this.musicTitle = musicTitle;
gameMusic =newMusic(this.musicTitle,false);}publicvoidscreenDraw(Graphics2D g){
g.drawImage(gameInfoImage,0,1020,null);
g.drawImage(judgementLineImage,0,920,null);//方块判定界面//方块下落路径和间隙
g.drawImage(noteRouteLineImage,419,0,null);
g.drawImage(noteRouteSImage,423,0,null);
g.drawImage(noteRouteLineImage,573,0,null);
g.drawImage(noteRouteDImage,577,0,null);
g.drawImage(noteRouteLineImage,727,0,null);
g.drawImage(noteRouteFImage,731,0,null);
g.drawImage(noteRouteLineImage,881,0,null);
g.drawImage(noteRouteSpaceImage,885,0,null);
g.drawImage(noteRouteLineImage,1035,0,null);
g.drawImage(noteRouteJImage,1039,0,null);
g.drawImage(noteRouteLineImage,1189,0,null);
g.drawImage(noteRouteKImage,1193,0,null);
g.drawImage(noteRouteLineImage,1343,0,null);
g.drawImage(noteRouteLImage,1347,0,null);
g.drawImage(noteRouteLineImage,1497,0,null);for(int i =0; i < noteList.size(); i++){Note note = noteList.get(i);if(note.getY()>960){
judgeImage =newImageIcon(Main.class.getResource("../images/judgeMiss.png")).getImage();}if(!note.isProceeded()){
noteList.remove(i);
i--;}else{
note.screenDraw(g);}}//设置白色字体
g.setColor(Color.white);//让字体平滑
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(newFont("Arial",Font.BOLD,30));
g.drawString(titleName,30,1065);
g.drawString(difficulty,1800,1065);//设置字母位置和颜色
g.setFont(newFont("Arial",Font.PLAIN,30));
g.setColor(Color.GREEN);
g.drawString("S",498,950);
g.drawString("D",652,950);
g.drawString("F",806,950);
g.drawString("Space",920,950);
g.drawString("J",1110,950);
g.drawString("K",1268,950);
g.drawString("L",1422,950);
g.setColor(Color.LIGHT_GRAY);
g.setFont(newFont("Elephant",Font.BOLD,30));//显示当前得分
g.drawString(" "+Main.scout,920,1065);if(Main.scout >=1000){Main.note_speed =6;}
g.drawImage(blueFlareImage,360,500,null);
g.drawImage(judgeImage,780,730,null);}//按下键盘上相应键的行为的函数,按下和松开//SpublicvoidpressS(){judge("S");
noteRouteSImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseS(){
noteRouteSImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//DpublicvoidpressD(){judge("D");
noteRouteDImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseD(){
noteRouteDImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//FpublicvoidpressF(){judge("F");
noteRouteFImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseF(){
noteRouteFImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//SpacepublicvoidpressSpace(){judge("Space");
noteRouteSpaceImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseSpace(){
noteRouteSpaceImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//JpublicvoidpressJ(){judge("J");
noteRouteJImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseJ(){
noteRouteJImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//KpublicvoidpressK(){judge("K");
noteRouteKImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseK(){
noteRouteKImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}//LpublicvoidpressL(){judge("L");
noteRouteLImage =newImageIcon(Main.class.getResource("../images/noteRoutePressed.png")).getImage();newMusic("PressedKeyboardMusic.mp3",false).start();}publicvoidreleaseL(){
noteRouteLImage =newImageIcon(Main.class.getResource("../images/noteRoute.png")).getImage();}@Overridepublicvoidrun(){dropNotes(this.titleName);}publicvoidclose(){
gameMusic.close();this.interrupt();}//下落方块publicvoiddropNotes(String titleName){// noteList.add(new Note(425, 600));// noteList.add(new Note(579, 555));// noteList.add(new Note(733, 500));// noteList.add(new Note(887, 340));// noteList.add(new Note(1041, 480));// noteList.add(new Note(1195, 650));// noteList.add(new Note(1349, 340));Beat[] beats =null;//根据选择的歌曲和难度制定节拍if(titleName.equals("Celebrate - Joakim Karud")&& difficulty.equals("Easy")){int startTime =2750-Main.reach_time *1000;//设置起始时间int gap =150;//用来制定节拍的间隔
beats =newBeat[]{//节拍学习过程newBeat(startTime,"S"),newBeat(startTime + gap *2,"D"),newBeat(startTime + gap *4,"S"),newBeat(startTime + gap *6,"D"),newBeat(startTime + gap *8,"S"),newBeat(startTime + gap *10,"D"),newBeat(startTime + gap *12-80,"S"),newBeat(startTime + gap *14-130,"D"),newBeat(startTime + gap *15,"F"),newBeat(startTime + gap *18,"J"),newBeat(startTime + gap *20-80,"K"),newBeat(startTime + gap *22-130,"L"),newBeat(startTime + gap *23,"J"),newBeat(startTime + gap *24,"K"),newBeat(startTime + gap *25,"J"),newBeat(startTime + gap *26,"K"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *28,"Space"),// new Beat(startTime + gap * 36, "D"),// new Beat(startTime + gap * 38, "S"),// new Beat(startTime + gap * 40, "D"),// new Beat(startTime + gap * 42, "S"),// new Beat(startTime + gap * 44, "D"),// new Beat(startTime + gap * 46, "S"),// new Beat(startTime + gap * 48, "D"),// new Beat(startTime + gap * 50, "F"),// new Beat(startTime + gap * 52, "J"),// new Beat(startTime + gap * 54, "K"),// new Beat(startTime + gap * 56, "L"),// new Beat(startTime + gap * 58, "J"),// new Beat(startTime + gap * 60, "K"),// new Beat(startTime + gap * 62, "J"),// new Beat(startTime + gap * 64, "K"),// new Beat(startTime + gap * 66, "L"),// new Beat(startTime + gap * 68, "Space"),// new Beat(startTime + gap * 70, "D"),// new Beat(startTime + gap * 72, "S"),// new Beat(startTime + gap * 74, "D"),// new Beat(startTime + gap * 76, "S"),// new Beat(startTime + gap * 78, "D"),// new Beat(startTime + gap * 80, "S"),// new Beat(startTime + gap * 82, "D"),// new Beat(startTime + gap * 84, "F"),// new Beat(startTime + gap * 86, "J"),newBeat(startTime + gap *88,"S"),newBeat(startTime + gap *90,"D"),newBeat(startTime + gap *92,"S"),newBeat(startTime + gap *94,"D"),newBeat(startTime + gap *96,"J"),newBeat(startTime + gap *98,"K"),newBeat(startTime + gap *100,"J"),newBeat(startTime + gap *102,"K"),newBeat(startTime + gap *104,"D"),newBeat(startTime + gap *106,"F"),newBeat(startTime + gap *108,"L"),newBeat(startTime + gap *110,"Space"),newBeat(startTime + gap *112,"S"),newBeat(startTime + gap *114,"J"),newBeat(startTime + gap *116,"K"),newBeat(startTime + gap *118,"L"),newBeat(startTime + gap *120,"J"),newBeat(startTime + gap *122,"K"),newBeat(startTime + gap *124,"J"),newBeat(startTime + gap *126,"K"),newBeat(startTime + gap *128,"L"),newBeat(startTime + gap *130,"Space"),newBeat(startTime + gap *132,"D"),newBeat(startTime + gap *134,"S"),newBeat(startTime + gap *136,"D"),newBeat(startTime + gap *138,"S"),newBeat(startTime + gap *140,"D"),newBeat(startTime + gap *142,"S"),newBeat(startTime + gap *144,"K"),newBeat(startTime + gap *146,"L"),newBeat(startTime + gap *148,"J"),newBeat(startTime + gap *150,"K"),newBeat(startTime + gap *152,"J"),newBeat(startTime + gap *154,"K"),newBeat(startTime + gap *156,"L"),newBeat(startTime + gap *158,"Space"),newBeat(startTime + gap *160,"S"),newBeat(startTime + gap *162,"D"),newBeat(startTime + gap *164,"F"),newBeat(startTime + gap *166,"J"),newBeat(startTime + gap *168,"K"),newBeat(startTime + gap *170,"L"),newBeat(startTime + gap *172,"S"),newBeat(startTime + gap *174,"D"),newBeat(startTime + gap *176,"S"),newBeat(startTime + gap *178,"J"),newBeat(startTime + gap *180,"K"),newBeat(startTime + gap *182,"J"),newBeat(startTime + gap *184,"D"),newBeat(startTime + gap *186,"F"),newBeat(startTime + gap *188,"D"),newBeat(startTime + gap *190,"K"),newBeat(startTime + gap *192,"L"),newBeat(startTime + gap *194,"K"),newBeat(startTime + gap *196,"Space"),newBeat(startTime + gap *198,"S"),newBeat(startTime + gap *200,"D"),newBeat(startTime + gap *202,"F"),newBeat(startTime + gap *204,"D"),newBeat(startTime + gap *206,"S"),newBeat(startTime + gap *208,"K"),newBeat(startTime + gap *210,"L"),newBeat(startTime + gap *212,"J"),newBeat(startTime + gap *214,"K"),newBeat(startTime + gap *216,"J"),newBeat(startTime + gap *218,"K"),newBeat(startTime + gap *220,"L"),newBeat(startTime + gap *222,"Space"),newBeat(startTime + gap *224,"D"),newBeat(startTime + gap *226,"S"),newBeat(startTime + gap *228,"D"),newBeat(startTime + gap *230,"S"),newBeat(startTime + gap *232,"D"),newBeat(startTime + gap *234,"S"),newBeat(startTime + gap *236,"K"),newBeat(startTime + gap *238,"L"),newBeat(startTime + gap *240,"J"),newBeat(startTime + gap *242,"K"),newBeat(startTime + gap *244,"J"),newBeat(startTime + gap *246,"K"),newBeat(startTime + gap *248,"L"),newBeat(startTime + gap *250,"Space"),newBeat(startTime + gap *252,"S"),newBeat(startTime + gap *254,"D"),newBeat(startTime + gap *256,"S"),newBeat(startTime + gap *258,"J"),newBeat(startTime + gap *260,"K"),newBeat(startTime + gap *262,"J"),newBeat(startTime + gap *264,"Space"),newBeat(startTime + gap *266,"S"),newBeat(startTime + gap *268,"D"),newBeat(startTime + gap *270,"S"),newBeat(startTime + gap *272,"D"),newBeat(startTime + gap *274,"S"),newBeat(startTime + gap *276,"D"),newBeat(startTime + gap *278-80,"S"),newBeat(startTime + gap *280-130,"D"),newBeat(startTime + gap *281,"F"),newBeat(startTime + gap *284,"J"),newBeat(startTime + gap *286-80,"K"),newBeat(startTime + gap *288-130,"L"),newBeat(startTime + gap *289,"J"),newBeat(startTime + gap *290,"K"),newBeat(startTime + gap *291,"J"),newBeat(startTime + gap *292,"K"),newBeat(startTime + gap *293,"L"),newBeat(startTime + gap *294,"Space"),newBeat(startTime + gap *296,"D"),newBeat(startTime + gap *298,"S"),newBeat(startTime + gap *300,"K"),newBeat(startTime + gap *302,"L"),newBeat(startTime + gap *304,"J"),newBeat(startTime + gap *306,"K"),newBeat(startTime + gap *308,"J"),newBeat(startTime + gap *310,"K"),newBeat(startTime + gap *312,"L"),newBeat(startTime + gap *314,"Space"),newBeat(startTime + gap *316,"D"),newBeat(startTime + gap *318,"S"),newBeat(startTime + gap *320,"D"),newBeat(startTime + gap *322,"S"),newBeat(startTime + gap *324,"D"),newBeat(startTime + gap *326,"S"),newBeat(startTime + gap *328,"K"),newBeat(startTime + gap *330,"L"),newBeat(startTime + gap *332,"J"),newBeat(startTime + gap *334,"K"),newBeat(startTime + gap *336,"J"),newBeat(startTime + gap *338,"K"),newBeat(startTime + gap *340,"L"),newBeat(startTime + gap *342,"Space"),newBeat(startTime + gap *344,"S"),newBeat(startTime + gap *346,"D"),newBeat(startTime + gap *348,"S"),newBeat(startTime + gap *350,"J"),newBeat(startTime + gap *352,"K"),newBeat(startTime + gap *354,"J"),newBeat(startTime + gap *356,"Space"),};}elseif(titleName.equals("Celebrate - Joakim Karud")&& difficulty.equals("Hard")){int startTime =2750-Main.reach_time *1000;int gap =125;//用来制定节拍的间隔Main.note_speed =4;
beats =newBeat[]{//节拍学习过程newBeat(startTime,"S"),newBeat(startTime,"J"),newBeat(startTime + gap *2,"D"),newBeat(startTime + gap *2,"K"),newBeat(startTime + gap *4,"S"),newBeat(startTime + gap *4,"J"),newBeat(startTime + gap *6,"D"),newBeat(startTime + gap *6,"K"),newBeat(startTime + gap *8,"S"),newBeat(startTime + gap *8,"J"),newBeat(startTime + gap *10,"D"),newBeat(startTime + gap *10,"K"),newBeat(startTime + gap *12-80,"S"),newBeat(startTime + gap *12-80,"J"),newBeat(startTime + gap *14-130,"D"),newBeat(startTime + gap *14-130,"K"),newBeat(startTime + gap *15,"F"),newBeat(startTime + gap *15,"L"),newBeat(startTime + gap *18,"J"),newBeat(startTime + gap *18,"S"),newBeat(startTime + gap *20-80,"K"),newBeat(startTime + gap *20-80,"D"),newBeat(startTime + gap *22-130,"L"),newBeat(startTime + gap *22-130,"F"),newBeat(startTime + gap *23,"J"),newBeat(startTime + gap *23,"F"),newBeat(startTime + gap *24,"K"),newBeat(startTime + gap *24,"D"),newBeat(startTime + gap *25,"J"),newBeat(startTime + gap *25,"F"),newBeat(startTime + gap *26,"K"),newBeat(startTime + gap *26,"D"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *28,"Space"),newBeat(startTime + gap *36,"D"),newBeat(startTime + gap *38,"S"),newBeat(startTime + gap *40,"D"),newBeat(startTime + gap *42,"S"),newBeat(startTime + gap *44,"D"),newBeat(startTime + gap *46,"S"),newBeat(startTime + gap *48,"D"),newBeat(startTime + gap *50,"F"),newBeat(startTime + gap *52,"J"),newBeat(startTime + gap *54,"K"),newBeat(startTime + gap *56,"L"),newBeat(startTime + gap *58,"J"),newBeat(startTime + gap *60,"K"),newBeat(startTime + gap *62,"J"),newBeat(startTime + gap *64,"K"),newBeat(startTime + gap *66,"L"),newBeat(startTime + gap *68,"Space"),// new Beat(startTime + gap * 70, "D"),// new Beat(startTime + gap * 72, "S"),// new Beat(startTime + gap * 74, "D"),// new Beat(startTime + gap * 76, "S"),// new Beat(startTime + gap * 78, "D"),// new Beat(startTime + gap * 80, "S"),// new Beat(startTime + gap * 82, "D"),// new Beat(startTime + gap * 84, "F"),// new Beat(startTime + gap * 86, "J"),newBeat(startTime + gap *88,"S"),newBeat(startTime + gap *89,"D"),newBeat(startTime + gap *90,"S"),newBeat(startTime + gap *91,"D"),newBeat(startTime + gap *92,"J"),newBeat(startTime + gap *93,"K"),newBeat(startTime + gap *94,"J"),newBeat(startTime + gap *95,"K"),newBeat(startTime + gap *96,"D"),newBeat(startTime + gap *97,"F"),newBeat(startTime + gap *98,"L"),newBeat(startTime + gap *99,"Space"),newBeat(startTime + gap *100,"S"),newBeat(startTime + gap *101,"J"),newBeat(startTime + gap *102,"S"),newBeat(startTime + gap *103,"D"),newBeat(startTime + gap *104,"S"),newBeat(startTime + gap *105,"D"),newBeat(startTime + gap *106,"J"),newBeat(startTime + gap *107,"K"),newBeat(startTime + gap *108,"J"),newBeat(startTime + gap *109,"K"),newBeat(startTime + gap *110,"D"),newBeat(startTime + gap *111,"F"),newBeat(startTime + gap *112,"L"),newBeat(startTime + gap *114,"Space"),newBeat(startTime + gap *114,"F"),newBeat(startTime + gap *114,"J"),newBeat(startTime + gap *116,"K"),newBeat(startTime + gap *118,"L"),newBeat(startTime + gap *120,"J"),newBeat(startTime + gap *122,"K"),newBeat(startTime + gap *124,"J"),newBeat(startTime + gap *126,"K"),newBeat(startTime + gap *128,"L"),newBeat(startTime + gap *130,"Space"),newBeat(startTime + gap *132,"D"),newBeat(startTime + gap *132,"K"),newBeat(startTime + gap *134,"S"),newBeat(startTime + gap *134,"L"),newBeat(startTime + gap *136,"D"),newBeat(startTime + gap *136,"K"),newBeat(startTime + gap *138,"S"),newBeat(startTime + gap *138,"L"),newBeat(startTime + gap *140,"D"),newBeat(startTime + gap *140,"K"),newBeat(startTime + gap *142,"S"),newBeat(startTime + gap *142,"L"),newBeat(startTime + gap *144,"K"),newBeat(startTime + gap *144,"D"),newBeat(startTime + gap *146,"S"),newBeat(startTime + gap *146,"L"),newBeat(startTime + gap *148,"F"),newBeat(startTime + gap *148,"J"),newBeat(startTime + gap *150,"D"),newBeat(startTime + gap *150,"K"),newBeat(startTime + gap *152,"F"),newBeat(startTime + gap *152,"J"),newBeat(startTime + gap *154,"D"),newBeat(startTime + gap *154,"K"),newBeat(startTime + gap *156,"F"),newBeat(startTime + gap *156,"L"),newBeat(startTime + gap *158,"Space"),newBeat(startTime + gap *160,"S"),newBeat(startTime + gap *160,"L"),newBeat(startTime + gap *162,"D"),newBeat(startTime + gap *162,"K"),newBeat(startTime + gap *164,"F"),newBeat(startTime + gap *164,"J"),newBeat(startTime + gap *166,"J"),newBeat(startTime + gap *166,"F"),newBeat(startTime + gap *168,"K"),newBeat(startTime + gap *168,"D"),newBeat(startTime + gap *170,"L"),newBeat(startTime + gap *170,"S"),newBeat(startTime + gap *172,"S"),newBeat(startTime + gap *174,"D"),newBeat(startTime + gap *176,"S"),newBeat(startTime + gap *178,"J"),newBeat(startTime + gap *180,"K"),newBeat(startTime + gap *182,"J"),newBeat(startTime + gap *184,"D"),newBeat(startTime + gap *186,"F"),newBeat(startTime + gap *188,"D"),newBeat(startTime + gap *190,"K"),newBeat(startTime + gap *192,"L"),newBeat(startTime + gap *194,"K"),newBeat(startTime + gap *196,"Space"),newBeat(startTime + gap *196,"D"),newBeat(startTime + gap *196,"F"),newBeat(startTime + gap *196,"J"),newBeat(startTime + gap *196,"K"),newBeat(startTime + gap *198,"S"),newBeat(startTime + gap *200,"D"),newBeat(startTime + gap *202,"F"),newBeat(startTime + gap *204,"D"),newBeat(startTime + gap *206,"S"),newBeat(startTime + gap *208,"K"),newBeat(startTime + gap *210,"L"),newBeat(startTime + gap *212,"J"),newBeat(startTime + gap *214,"K"),newBeat(startTime + gap *216,"J"),newBeat(startTime + gap *218,"K"),newBeat(startTime + gap *220,"L"),newBeat(startTime + gap *222,"Space"),newBeat(startTime + gap *224,"D"),newBeat(startTime + gap *224,"K"),newBeat(startTime + gap *226,"S"),newBeat(startTime + gap *226,"L"),newBeat(startTime + gap *228,"D"),newBeat(startTime + gap *228,"K"),newBeat(startTime + gap *230,"S"),newBeat(startTime + gap *230,"L"),newBeat(startTime + gap *232,"D"),newBeat(startTime + gap *232,"K"),newBeat(startTime + gap *234,"S"),newBeat(startTime + gap *234,"L"),newBeat(startTime + gap *236,"K"),newBeat(startTime + gap *236,"D"),newBeat(startTime + gap *238,"L"),newBeat(startTime + gap *238,"S"),newBeat(startTime + gap *240,"J"),newBeat(startTime + gap *240,"F"),newBeat(startTime + gap *242,"K"),newBeat(startTime + gap *242,"D"),newBeat(startTime + gap *244,"J"),newBeat(startTime + gap *244,"F"),newBeat(startTime + gap *246,"K"),newBeat(startTime + gap *246,"D"),newBeat(startTime + gap *248,"L"),newBeat(startTime + gap *248,"F"),newBeat(startTime + gap *250,"Space"),newBeat(startTime + gap *252,"S"),newBeat(startTime + gap *254,"D"),newBeat(startTime + gap *256,"S"),newBeat(startTime + gap *258,"J"),newBeat(startTime + gap *260,"K"),newBeat(startTime + gap *262,"J"),newBeat(startTime + gap *264,"Space"),newBeat(startTime + gap *266,"S"),newBeat(startTime + gap *268,"D"),newBeat(startTime + gap *270,"S"),newBeat(startTime + gap *272,"D"),newBeat(startTime + gap *274,"S"),newBeat(startTime + gap *276,"D"),newBeat(startTime + gap *278-80,"S"),newBeat(startTime + gap *280-130,"D"),newBeat(startTime + gap *281,"F"),newBeat(startTime + gap *284,"J"),newBeat(startTime + gap *286-80,"K"),newBeat(startTime + gap *288-130,"L"),newBeat(startTime + gap *289,"J"),newBeat(startTime + gap *290,"K"),newBeat(startTime + gap *291,"J"),newBeat(startTime + gap *292,"K"),newBeat(startTime + gap *293,"L"),newBeat(startTime + gap *294,"Space"),newBeat(startTime + gap *296,"D"),newBeat(startTime + gap *298,"S"),newBeat(startTime + gap *300,"K"),newBeat(startTime + gap *302,"L"),newBeat(startTime + gap *304,"J"),newBeat(startTime + gap *306,"K"),newBeat(startTime + gap *308,"J"),newBeat(startTime + gap *310,"K"),newBeat(startTime + gap *312,"L"),newBeat(startTime + gap *314,"Space"),newBeat(startTime + gap *316,"D"),newBeat(startTime + gap *316,"K"),newBeat(startTime + gap *318,"S"),newBeat(startTime + gap *318,"L"),newBeat(startTime + gap *320,"D"),newBeat(startTime + gap *320,"K"),newBeat(startTime + gap *322,"S"),newBeat(startTime + gap *322,"L"),newBeat(startTime + gap *324,"D"),newBeat(startTime + gap *324,"K"),newBeat(startTime + gap *326,"S"),newBeat(startTime + gap *326,"L"),newBeat(startTime + gap *328,"K"),newBeat(startTime + gap *328,"D"),newBeat(startTime + gap *330,"L"),newBeat(startTime + gap *330,"S"),newBeat(startTime + gap *332,"J"),newBeat(startTime + gap *332,"F"),newBeat(startTime + gap *334,"K"),newBeat(startTime + gap *334,"D"),newBeat(startTime + gap *336,"J"),newBeat(startTime + gap *336,"F"),newBeat(startTime + gap *338,"K"),newBeat(startTime + gap *338,"D"),newBeat(startTime + gap *340,"L"),newBeat(startTime + gap *340,"S"),newBeat(startTime + gap *342,"Space"),newBeat(startTime + gap *344,"S"),newBeat(startTime + gap *346,"D"),newBeat(startTime + gap *348,"S"),newBeat(startTime + gap *350,"J"),newBeat(startTime + gap *352,"K"),newBeat(startTime + gap *354,"J"),newBeat(startTime + gap *356,"Space"),newBeat(startTime + gap *356,"S"),newBeat(startTime + gap *356,"D"),newBeat(startTime + gap *356,"F"),newBeat(startTime + gap *356,"J"),newBeat(startTime + gap *356,"K"),newBeat(startTime + gap *356,"L"),};}elseif(titleName.equals("Future Funk - Joakim Karud")&& difficulty.equals("Easy")){int startTime =2750-Main.reach_time *1000;//设置起始时间int gap =150;//用来制定节拍的间隔
beats =newBeat[]{//节拍学习过程newBeat(startTime,"S"),newBeat(startTime + gap *2,"D"),newBeat(startTime + gap *4,"S"),newBeat(startTime + gap *6,"D"),newBeat(startTime + gap *8,"S"),newBeat(startTime + gap *10,"D"),newBeat(startTime + gap *12-80,"S"),newBeat(startTime + gap *14-130,"D"),newBeat(startTime + gap *15,"F"),newBeat(startTime + gap *18,"J"),newBeat(startTime + gap *20-80,"K"),newBeat(startTime + gap *22-130,"L"),newBeat(startTime + gap *23,"J"),newBeat(startTime + gap *24,"K"),newBeat(startTime + gap *25,"J"),newBeat(startTime + gap *26,"K"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *28,"Space"),newBeat(startTime + gap *88,"S"),newBeat(startTime + gap *90,"D"),newBeat(startTime + gap *92,"S"),newBeat(startTime + gap *94,"D"),newBeat(startTime + gap *96,"J"),newBeat(startTime + gap *98,"K"),newBeat(startTime + gap *100,"J"),newBeat(startTime + gap *102,"K"),newBeat(startTime + gap *104,"D"),newBeat(startTime + gap *106,"F"),newBeat(startTime + gap *108,"L"),newBeat(startTime + gap *110,"Space"),newBeat(startTime + gap *112,"S"),newBeat(startTime + gap *114,"J"),newBeat(startTime + gap *116,"K"),newBeat(startTime + gap *118,"L"),newBeat(startTime + gap *120,"J"),newBeat(startTime + gap *122,"K"),newBeat(startTime + gap *124,"J"),newBeat(startTime + gap *126,"K"),newBeat(startTime + gap *128,"L"),newBeat(startTime + gap *130,"Space"),newBeat(startTime + gap *132,"D"),newBeat(startTime + gap *134,"S"),newBeat(startTime + gap *136,"D"),newBeat(startTime + gap *138,"S"),newBeat(startTime + gap *140,"D"),newBeat(startTime + gap *142,"S"),newBeat(startTime + gap *144,"K"),newBeat(startTime + gap *146,"L"),newBeat(startTime + gap *148,"J"),newBeat(startTime + gap *150,"K"),newBeat(startTime + gap *152,"J"),newBeat(startTime + gap *154,"K"),newBeat(startTime + gap *156,"L"),newBeat(startTime + gap *158,"Space"),newBeat(startTime + gap *160,"S"),newBeat(startTime + gap *162,"D"),newBeat(startTime + gap *164,"F"),newBeat(startTime + gap *166,"J"),newBeat(startTime + gap *168,"K"),newBeat(startTime + gap *170,"L"),newBeat(startTime + gap *172,"S"),newBeat(startTime + gap *174,"D"),newBeat(startTime + gap *176,"S"),newBeat(startTime + gap *178,"J"),newBeat(startTime + gap *180,"K"),newBeat(startTime + gap *182,"J"),newBeat(startTime + gap *184,"D"),newBeat(startTime + gap *186,"F"),newBeat(startTime + gap *188,"D"),newBeat(startTime + gap *190,"K"),newBeat(startTime + gap *192,"L"),newBeat(startTime + gap *194,"K"),newBeat(startTime + gap *196,"Space"),newBeat(startTime + gap *198,"S"),newBeat(startTime + gap *200,"D"),newBeat(startTime + gap *202,"F"),newBeat(startTime + gap *204,"D"),newBeat(startTime + gap *206,"S"),newBeat(startTime + gap *208,"K"),newBeat(startTime + gap *210,"L"),newBeat(startTime + gap *212,"J"),newBeat(startTime + gap *214,"K"),newBeat(startTime + gap *216,"J"),newBeat(startTime + gap *218,"K"),newBeat(startTime + gap *220,"L"),newBeat(startTime + gap *222,"Space"),newBeat(startTime + gap *224,"D"),newBeat(startTime + gap *226,"S"),newBeat(startTime + gap *228,"D"),newBeat(startTime + gap *230,"S"),newBeat(startTime + gap *232,"D"),newBeat(startTime + gap *234,"S"),newBeat(startTime + gap *236,"K"),newBeat(startTime + gap *238,"L"),newBeat(startTime + gap *240,"J"),newBeat(startTime + gap *242,"K"),newBeat(startTime + gap *244,"J"),newBeat(startTime + gap *246,"K"),newBeat(startTime + gap *248,"L"),newBeat(startTime + gap *250,"Space"),newBeat(startTime + gap *252,"S"),newBeat(startTime + gap *254,"D"),newBeat(startTime + gap *256,"S"),newBeat(startTime + gap *258,"J"),newBeat(startTime + gap *260,"K"),newBeat(startTime + gap *262,"J"),newBeat(startTime + gap *264,"Space"),newBeat(startTime + gap *266,"S"),newBeat(startTime + gap *268,"D"),newBeat(startTime + gap *270,"S"),newBeat(startTime + gap *272,"D"),newBeat(startTime + gap *274,"S"),newBeat(startTime + gap *276,"D"),newBeat(startTime + gap *278-80,"S"),newBeat(startTime + gap *280-130,"D"),newBeat(startTime + gap *281,"F"),newBeat(startTime + gap *284,"J"),newBeat(startTime + gap *286-80,"K"),newBeat(startTime + gap *288-130,"L"),newBeat(startTime + gap *289,"J"),newBeat(startTime + gap *290,"K"),newBeat(startTime + gap *291,"J"),newBeat(startTime + gap *292,"K"),newBeat(startTime + gap *293,"L"),newBeat(startTime + gap *294,"Space"),};}elseif(titleName.equals("Future Funk - Joakim Karud")&& difficulty.equals("Hard")){int startTime =2750-Main.reach_time *1000;//设置起始时间int gap =150;//用来制定节拍的间隔
beats =newBeat[]{//节拍学习过程newBeat(startTime,"S"),newBeat(startTime,"J"),newBeat(startTime + gap *2,"D"),newBeat(startTime + gap *2,"K"),newBeat(startTime + gap *4,"S"),newBeat(startTime + gap *4,"J"),newBeat(startTime + gap *6,"D"),newBeat(startTime + gap *6,"K"),newBeat(startTime + gap *8,"S"),newBeat(startTime + gap *8,"J"),newBeat(startTime + gap *10,"D"),newBeat(startTime + gap *10,"K"),newBeat(startTime + gap *12-80,"S"),newBeat(startTime + gap *12-80,"J"),newBeat(startTime + gap *14-130,"D"),newBeat(startTime + gap *14-130,"K"),newBeat(startTime + gap *15,"F"),newBeat(startTime + gap *15,"L"),newBeat(startTime + gap *18,"J"),newBeat(startTime + gap *18,"S"),newBeat(startTime + gap *20-80,"K"),newBeat(startTime + gap *20-80,"D"),newBeat(startTime + gap *22-130,"L"),newBeat(startTime + gap *22-130,"F"),newBeat(startTime + gap *23,"J"),newBeat(startTime + gap *23,"F"),newBeat(startTime + gap *24,"K"),newBeat(startTime + gap *24,"D"),newBeat(startTime + gap *25,"J"),newBeat(startTime + gap *25,"F"),newBeat(startTime + gap *26,"K"),newBeat(startTime + gap *26,"D"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *27,"L"),newBeat(startTime + gap *28,"Space"),newBeat(startTime + gap *36,"D"),newBeat(startTime + gap *38,"S"),newBeat(startTime + gap *40,"D"),newBeat(startTime + gap *42,"S"),newBeat(startTime + gap *44,"D"),newBeat(startTime + gap *46,"S"),newBeat(startTime + gap *48,"D"),newBeat(startTime + gap *50,"F"),newBeat(startTime + gap *52,"J"),newBeat(startTime + gap *54,"K"),newBeat(startTime + gap *56,"L"),newBeat(startTime + gap *58,"J"),newBeat(startTime + gap *60,"K"),newBeat(startTime + gap *62,"J"),newBeat(startTime + gap *64,"K"),newBeat(startTime + gap *66,"L"),newBeat(startTime + gap *68,"Space"),newBeat(startTime + gap *88,"S"),newBeat(startTime + gap *89,"D"),newBeat(startTime + gap *90,"S"),newBeat(startTime + gap *91,"D"),newBeat(startTime + gap *92,"J"),newBeat(startTime + gap *93,"K"),newBeat(startTime + gap *94,"J"),newBeat(startTime + gap *95,"K"),newBeat(startTime + gap *96,"D"),newBeat(startTime + gap *97,"F"),newBeat(startTime + gap *98,"L"),newBeat(startTime + gap *99,"Space"),newBeat(startTime + gap *100,"S"),newBeat(startTime + gap *101,"J"),newBeat(startTime + gap *102,"S"),newBeat(startTime + gap *103,"D"),newBeat(startTime + gap *104,"S"),newBeat(startTime + gap *105,"D"),newBeat(startTime + gap *106,"J"),newBeat(startTime + gap *107,"K"),newBeat(startTime + gap *108,"J"),newBeat(startTime + gap *109,"K"),newBeat(startTime + gap *110,"D"),newBeat(startTime + gap *111,"F"),newBeat(startTime + gap *112,"L"),newBeat(startTime + gap *114,"Space"),newBeat(startTime + gap *114,"F"),newBeat(startTime + gap *114,"J"),newBeat(startTime + gap *116,"K"),newBeat(startTime + gap *118,"L"),newBeat(startTime + gap *120,"J"),newBeat(startTime + gap *122,"K"),newBeat(startTime + gap *124,"J"),newBeat(startTime + gap *126,"K"),newBeat(startTime + gap *128,"L"),newBeat(startTime + gap *130,"Space"),newBeat(startTime + gap *132,"D"),newBeat(startTime + gap *132,"K"),newBeat(startTime + gap *134,"S"),newBeat(startTime + gap *134,"L"),newBeat(startTime + gap *136,"D"),newBeat(startTime + gap *136,"K"),newBeat(startTime + gap *138,"S"),newBeat(startTime + gap *138,"L"),newBeat(startTime + gap *140,"D"),newBeat(startTime + gap *140,"K"),newBeat(startTime + gap *142,"S"),newBeat(startTime + gap *142,"L"),newBeat(startTime + gap *144,"K"),newBeat(startTime + gap *144,"D"),newBeat(startTime + gap *146,"S"),newBeat(startTime + gap *146,"L"),newBeat(startTime + gap *148,"F"),newBeat(startTime + gap *148,"J"),newBeat(startTime + gap *150,"D"),newBeat(startTime + gap *150,"K"),newBeat(startTime + gap *152,"F"),newBeat(startTime + gap *152,"J"),newBeat(startTime + gap *154,"D"),newBeat(startTime + gap *154,"K"),newBeat(startTime + gap *156,"F"),newBeat(startTime + gap *156,"L"),newBeat(startTime + gap *158,"Space"),newBeat(startTime + gap *160,"S"),newBeat(startTime + gap *160,"L"),newBeat(startTime + gap *162,"D"),newBeat(startTime + gap *162,"K"),newBeat(startTime + gap *164,"F"),newBeat(startTime + gap *164,"J"),newBeat(startTime + gap *166,"J"),newBeat(startTime + gap *166,"F"),newBeat(startTime + gap *168,"K"),newBeat(startTime + gap *168,"D"),newBeat(startTime + gap *170,"L"),newBeat(startTime + gap *170,"S"),newBeat(startTime + gap *172,"S"),newBeat(startTime + gap *174,"D"),newBeat(startTime + gap *176,"S"),newBeat(startTime + gap *178,"J"),newBeat(startTime + gap *180,"K"),newBeat(startTime + gap *182,"J"),newBeat(startTime + gap *184,"D"),newBeat(startTime + gap *186,"F"),newBeat(startTime + gap *188,"D"),newBeat(startTime + gap *190,"K"),newBeat(startTime + gap *192,"L"),newBeat(startTime + gap *194,"K"),newBeat(startTime + gap *196,"Space"),newBeat(startTime + gap *196,"D"),newBeat(startTime + gap *196,"F"),newBeat(startTime + gap *196,"J"),newBeat(startTime + gap *196,"K"),newBeat(startTime + gap *198,"S"),newBeat(startTime + gap *200,"D"),newBeat(startTime + gap *202,"F"),newBeat(startTime + gap *204,"D"),newBeat(startTime + gap *206,"S"),newBeat(startTime + gap *208,"K"),newBeat(startTime + gap *210,"L"),newBeat(startTime + gap *212,"J"),newBeat(startTime + gap *214,"K"),newBeat(startTime + gap *216,"J"),newBeat(startTime + gap *218,"K"),newBeat(startTime + gap *220,"L"),newBeat(startTime + gap *222,"Space"),newBeat(startTime + gap *224,"D"),newBeat(startTime + gap *224,"K"),newBeat(startTime + gap *226,"S"),newBeat(startTime + gap *226,"L"),newBeat(startTime + gap *228,"D"),newBeat(startTime + gap *228,"K"),newBeat(startTime + gap *230,"S"),newBeat(startTime + gap *230,"L"),newBeat(startTime + gap *232,"D"),newBeat(startTime + gap *232,"K"),newBeat(startTime + gap *234,"S"),newBeat(startTime + gap *234,"L"),newBeat(startTime + gap *236,"K"),newBeat(startTime + gap *236,"D"),newBeat(startTime + gap *238,"L"),newBeat(startTime + gap *238,"S"),newBeat(startTime + gap *240,"J"),newBeat(startTime + gap *240,"F"),newBeat(startTime + gap *242,"K"),newBeat(startTime + gap *242,"D"),newBeat(startTime + gap *244,"J"),newBeat(startTime + gap *244,"F"),newBeat(startTime + gap *246,"K"),newBeat(startTime + gap *246,"D"),newBeat(startTime + gap *248,"L"),newBeat(startTime + gap *248,"F"),newBeat(startTime + gap *250,"Space"),newBeat(startTime + gap *252,"S"),newBeat(startTime + gap *254,"D"),newBeat(startTime + gap *256,"S"),newBeat(startTime + gap *258,"J"),newBeat(startTime + gap *260,"K"),newBeat(startTime + gap *262,"J"),newBeat(startTime + gap *264,"Space"),newBeat(startTime + gap *266,"S"),newBeat(startTime + gap *268,"D"),newBeat(startTime + gap *270,"S"),newBeat(startTime + gap *272,"D"),newBeat(startTime + gap *274,"S"),newBeat(startTime + gap *276,"D"),newBeat(startTime + gap *278-80,"S"),newBeat(startTime + gap *280-130,"D"),newBeat(startTime + gap *281,"F"),newBeat(startTime + gap *284,"J"),newBeat(startTime + gap *286-80,"K"),newBeat(startTime + gap *288-130,"L"),newBeat(startTime + gap *289,"J"),newBeat(startTime + gap *290,"K"),newBeat(startTime + gap *291,"J"),newBeat(startTime + gap *292,"K"),newBeat(startTime + gap *293,"L"),newBeat(startTime + gap *294,"Space"),};}
gameMusic.start();int i =0;while(i < beats.length &&!isInterrupted()){boolean dropped =false;if(beats[i].getTime()<= gameMusic.getTime()){Note note =newNote(beats[i].getNoteName());
note.start();
noteList.add(note);
i++;
dropped =true;}if(!dropped ){try{Thread.sleep(5);}catch(Exception e){
e.printStackTrace();}}}}//判定方块函数publicvoidjudge(String input){for(int i =0; i < noteList.size(); i++){Note note = noteList.get(i);if(input.equals(note.getNoteType())){judgeEvent(note.judge());break;}}}//判断事件,判断显示等级publicvoidjudgeEvent(String judge){if(!judge.equals("None")){
blueFlareImage =newImageIcon(Main.class.getResource("../images/blueFlare.png")).getImage();}if(judge.equals("Miss")){
judgeImage =newImageIcon(Main.class.getResource("../images/judgeMiss.png")).getImage();}elseif(judge.equals("Late")){
judgeImage =newImageIcon(Main.class.getResource("../images/judgeLate.png")).getImage();}elseif(judge.equals("Early")){
judgeImage =newImageIcon(Main.class.getResource("../images/judgeEarly.png")).getImage();}elseif(judge.equals("Good")){
judgeImage =newImageIcon(Main.class.getResource("../images/judgeGood.png")).getImage();}elseif(judge.equals("Prefect")){
judgeImage =newImageIcon(Main.class.getResource("../images/judgePrefect.png")).getImage();}}}
3.4 KeyListener_.java
packageMstw;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;publicclassKeyListener_extendsKeyAdapter{//按下键盘@OverridepublicvoidkeyPressed(KeyEvent e){if(Mstw.game ==null){return;}elseif(e.getKeyCode()==KeyEvent.VK_S){Mstw.game.pressS();}elseif(e.getKeyCode()==KeyEvent.VK_D){Mstw.game.pressD();}elseif(e.getKeyCode()==KeyEvent.VK_F){Mstw.game.pressF();}elseif(e.getKeyCode()==KeyEvent.VK_SPACE){Mstw.game.pressSpace();}elseif(e.getKeyCode()==KeyEvent.VK_J){Mstw.game.pressJ();}elseif(e.getKeyCode()==KeyEvent.VK_K){Mstw.game.pressK();}elseif(e.getKeyCode()==KeyEvent.VK_L){Mstw.game.pressL();}}//释放键盘@OverridepublicvoidkeyReleased(KeyEvent e){if(Mstw.game ==null){return;}elseif(e.getKeyCode()==KeyEvent.VK_S){Mstw.game.releaseS();}elseif(e.getKeyCode()==KeyEvent.VK_D){Mstw.game.releaseD();}elseif(e.getKeyCode()==KeyEvent.VK_F){Mstw.game.releaseF();}elseif(e.getKeyCode()==KeyEvent.VK_SPACE){Mstw.game.releaseSpace();}elseif(e.getKeyCode()==KeyEvent.VK_J){Mstw.game.releaseJ();}elseif(e.getKeyCode()==KeyEvent.VK_K){Mstw.game.releaseK();}elseif(e.getKeyCode()==KeyEvent.VK_L){Mstw.game.releaseL();}}}
3.5 Mstw.java
packageMstw;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.util.ArrayList;publicclassMstwextendsJFrame{privateImage screenImage;privateGraphics screenGraphic;privateint mouseX,mouseY;privateboolean isGameScreen =false;//从路径中获取图片//背景图片privateImageBackground=newImageIcon(Main.class.getResource("../images/background.jpg")).getImage();//上边界privateJLabel menuBar =newJLabel(newImageIcon(Main.class.getResource("../images/menuBar.png")));//制作按钮基础图标和鼠标访问图标privateImageIcon exitButtonBasicImage =newImageIcon(Main.class.getResource("../images/exitButtonBasic.png"));privateImageIcon exitButtonEnteredImage =newImageIcon(Main.class.getResource("../images/exitButtonEntered.png"));privateImageIcon startButtonBasicImage =newImageIcon(Main.class.getResource("../images/startButtonBasic.png"));privateImageIcon startButtonEnteredImage =newImageIcon(Main.class.getResource("../images/startButtonEntered.png"));privateImageIcon quitButtonBasicImage =newImageIcon(Main.class.getResource("../images/quitButtonBasic.png"));privateImageIcon quitButtonEnteredImage =newImageIcon(Main.class.getResource("../images/quitButtonEntered.png"));privateImageIcon easyButtonBasicImage =newImageIcon(Main.class.getResource("../images/easyButtonBasic.png"));privateImageIcon easyButtonEnteredImage =newImageIcon(Main.class.getResource("../images/easyButtonEntered.png"));privateImageIcon hardButtonBasicImage =newImageIcon((Main.class.getResource("../images/hardButtonBasic.png")));privateImageIcon hardButtonEnteredImage =newImageIcon((Main.class.getResource("../images/hardButtonEntered.png")));privateImageIcon backButtonBasicImage =newImageIcon(Main.class.getResource("../images/backButtonBasic.png"));privateImageIcon backButtonEnteredImage =newImageIcon(Main.class.getResource("../images/backButtonEntered.png"));privateImageIcon leftSelectedButtonBasicImage =newImageIcon(Main.class.getResource("../images/leftSelectedButtonBasic.png"));privateImageIcon leftSelectedButtonEnteredImage =newImageIcon(Main.class.getResource("../images/leftSelectedButtonEntered.png"));privateImageIcon rightSelectedButtonBasicImage =newImageIcon(Main.class.getResource("../images/rightSelectedButtonBasic.png"));privateImageIcon rightSelectedButtonEnteredImage =newImageIcon(Main.class.getResource("../images/rightSelectedButtonEntered.png"));//设置按钮privateJButton exitButton =newJButton(exitButtonBasicImage);privateJButton startButton =newJButton(startButtonBasicImage);privateJButton quitButton =newJButton(quitButtonBasicImage);privateJButton easyButton =newJButton(easyButtonBasicImage);privateJButton hardButton =newJButton(hardButtonBasicImage);privateJButton backButton =newJButton(backButtonBasicImage);privateJButton leftSelectedButton =newJButton(leftSelectedButtonBasicImage);privateJButton rightSelectedButton =newJButton(rightSelectedButtonBasicImage);privateboolean isMainScreen =false;//创建数组列表来保存可选择的不同歌曲ArrayList<Track> trackList =newArrayList<Track>();//音乐对应标题图片privateImage titleImage;//选择不同音乐开始的不同封面privateImage selectedImage;//所选择的bgmprivateMusic selectedMusic;privateint nowSelected =0;//定义Music变量introMusic,传入首界面bgmMusic introMusic =newMusic("bgm.mp3",true);publicstaticGame game;//构造方法publicMstw(){//将不同歌曲的属性加入到音乐列表中,并且便于组织
trackList.add(newTrack("Celebrate TitleImage.png","Celebrate startImage.png","gameBackground.jpg","Celebrate1.mp3","Celebrate1.mp3","Celebrate - Joakim Karud"));
trackList.add(newTrack("Future Funk TitleImage.png","Future Funk startImage.png","gameBackground.jpg","Future Funk1.mp3","Future Funk1.mp3","Future Funk - Joakim Karud"));//设置GUI界面setFocusable(true);//设置屏幕焦点,以便于监听键盘setUndecorated(true);setTitle("Melodic sound travels the world");//设置界面标题setSize(Main.screen_width,Main.screen_height);//设置界面大小setResizable(true);//设置窗口可以由用户自由调节大小setLocationRelativeTo(null);//设置窗口相对于指定组件的位置,null为屏幕中央setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置界面执行程序可关闭setVisible(true);//设置界面可见setBackground(newColor(0,0,0,0));//设置背景颜色setLayout(null);//设置布局为空//执行线程,播放音乐
introMusic.start();//设置右上角退出按钮
quitButton.setBounds(1850,0,30,30);//设置按钮位置
quitButton.setBorderPainted(false);
quitButton.setContentAreaFilled(false);
quitButton.setFocusPainted(false);
quitButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
quitButton.setIcon(quitButtonBasicImage);
quitButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
quitButton.setIcon(quitButtonEnteredImage);
quitButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();try{Thread.sleep(1000);}catch(InterruptedException ex){
ex.printStackTrace();}System.exit(0);}});add(quitButton);//设置上边界,并且可以移动它
menuBar.setBounds(0,0,1920,30);
menuBar.addMouseListener(newMouseAdapter(){@OverridepublicvoidmousePressed(MouseEvent e){
mouseX = e.getX();
mouseY = e.getY();}});
menuBar.addMouseMotionListener(newMouseMotionAdapter(){@OverridepublicvoidmouseDragged(MouseEvent e){int x = e.getXOnScreen();int y = e.getYOnScreen();setLocation(x - mouseX, y - mouseY);}});add(menuBar);//设置离开EXIT按钮
exitButton.setBounds(1100,780,169,99);
exitButton.setBorderPainted(false);
exitButton.setContentAreaFilled(false);
exitButton.setFocusPainted(false);
exitButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
exitButton.setIcon(exitButtonBasicImage);
exitButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
exitButton.setIcon(exitButtonEnteredImage);
exitButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();//设置按钮反应时间try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}System.exit(0);}});add(exitButton);//设置开始Start按钮
startButton.setBounds(650,780,169,99);
startButton.setBorderPainted(false);
startButton.setContentAreaFilled(false);
startButton.setFocusPainted(false);
startButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
startButton.setIcon(startButtonBasicImage);
startButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
startButton.setIcon(startButtonEnteredImage);
startButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();//设置按钮反应时间try{Thread.sleep(500);}catch(InterruptedException ex){
ex.printStackTrace();}//调用相应函数enterMain();}});add(startButton);//设置左选择按钮
leftSelectedButton.setVisible(false);
leftSelectedButton.setBounds(350,450,100,100);
leftSelectedButton.setBorderPainted(false);
leftSelectedButton.setContentAreaFilled(false);
leftSelectedButton.setFocusPainted(false);
leftSelectedButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
leftSelectedButton.setIcon(leftSelectedButtonBasicImage);
leftSelectedButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
leftSelectedButton.setIcon(leftSelectedButtonEnteredImage);
leftSelectedButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}selectLift();//按左选按钮调用selectLift函数}});add(leftSelectedButton);//设置右选择按钮
rightSelectedButton.setVisible(false);
rightSelectedButton.setBounds(1470,450,100,100);
rightSelectedButton.setBorderPainted(false);
rightSelectedButton.setContentAreaFilled(false);
rightSelectedButton.setFocusPainted(false);
rightSelectedButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
rightSelectedButton.setIcon(rightSelectedButtonBasicImage);
rightSelectedButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
rightSelectedButton.setIcon(rightSelectedButtonEnteredImage);
rightSelectedButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}selectRight();//按右选按钮调用selectRight函数}});add(rightSelectedButton);//设置简单按钮
easyButton.setVisible(false);
easyButton.setBounds(550,800,300,150);//设置按钮位置
easyButton.setBorderPainted(false);
easyButton.setContentAreaFilled(false);
easyButton.setFocusPainted(false);
easyButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
easyButton.setIcon(easyButtonBasicImage);
easyButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
easyButton.setIcon(easyButtonEnteredImage);
easyButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();Main.scout =0;try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}//开始游戏,难度为EasygameStart(nowSelected,"Easy");}});add(easyButton);//设置困难按钮
hardButton.setVisible(false);
hardButton.setBounds(1070,800,300,150);//设置按钮位置
hardButton.setBorderPainted(false);
hardButton.setContentAreaFilled(false);
hardButton.setFocusPainted(false);
hardButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
hardButton.setIcon(hardButtonBasicImage);
hardButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
hardButton.setIcon(hardButtonEnteredImage);
hardButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();Main.scout =0;try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}//开始游戏,难度为HardgameStart(nowSelected,"Hard");}});add(hardButton);//设置返回按钮
backButton.setBounds(20,50,100,100);
backButton.setVisible(false);
backButton.setBorderPainted(false);
backButton.setContentAreaFilled(false);
backButton.setFocusPainted(false);
backButton.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseExited(MouseEvent e){
backButton.setIcon(backButtonBasicImage);
backButton.setCursor(newCursor(Cursor.DEFAULT_CURSOR));}//鼠标访问@OverridepublicvoidmouseEntered(MouseEvent e){
backButton.setIcon(backButtonEnteredImage);
backButton.setCursor(newCursor(Cursor.HAND_CURSOR));Music buttonEnteredMusic =newMusic("buttonEnteredMusic.mp3",false);
buttonEnteredMusic.start();}//鼠标点击@OverridepublicvoidmousePressed(MouseEvent e){Music buttonEnteredMusic =newMusic("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();//设置按钮反应时间try{Thread.sleep(200);}catch(InterruptedException ex){
ex.printStackTrace();}//返回上一个界面backMain();//返回界面函数}});add(backButton);}//重写Graphics类的paint方法进行绘图publicvoidpaint(Graphics g){//创建一幅用于双缓冲,可以在屏幕外绘制的图像
screenImage =createImage(Main.screen_width,Main.screen_height);//得到一个Graphics对象
screenGraphic = screenImage.getGraphics();//调用screenDraw方法进行绘画screenDraw((Graphics2D) screenGraphic);//Graphics对象g调用drawImage方法,要加载的图像为screenImage,绘制图像矩阵左上角的位置为(0,0),容器为空
g.drawImage(screenImage,0,0,null);}publicvoidscreenDraw(Graphics2D g){//Graphics对象g调用drawImage方法,要加载的图像为introBackground,绘制图像矩阵左上角的位置为(0,0),容器为空
g.drawImage(Background,0,0,null);//如果是游戏选择界面的话就绘制图片和标题if(isMainScreen){
g.drawImage(selectedImage,710,250,null);
g.drawImage(titleImage,710,150,null);}//如果进入到游戏屏幕,才能实现游戏功能if(isGameScreen){
game.screenDraw(g);}paintComponents(g);try{Thread.sleep(5);}catch(Exception e){
e.printStackTrace();}//重绘图像this.repaint();}//根据当前所选择的音乐变换bgm和图片publicvoidselectTrack(int nowSelected){if(selectedMusic !=null){
selectedMusic.close();}
titleImage =newImageIcon(Main.class.getResource("../images/"+trackList.get(nowSelected).getTitleImage())).getImage();
selectedImage =newImageIcon(Main.class.getResource("../images/"+trackList.get(nowSelected).getStartImage())).getImage();
selectedMusic =newMusic(trackList.get(nowSelected).getStartMusic(),true);
selectedMusic.start();}//向左选择的函数publicvoidselectLift(){//如果是第一首再按左就跳到最后一首if(nowSelected ==0){
nowSelected = trackList.size()-1;}else{
nowSelected --;}selectTrack(nowSelected);//调用方法实现歌曲和图片的切换}//向右选择的函数publicvoidselectRight(){//如果是最后一首再按左就跳到第一首if(nowSelected == trackList.size()-1){
nowSelected =0;}else{
nowSelected ++;}selectTrack(nowSelected);//调用方法实现歌曲和图片的切换}//开始游戏执行的函数,参数为选择的难度publicvoidgameStart(int nowSelected,String difficulty){if(selectedMusic !=null){
selectedMusic.close();}
easyButton.setVisible(false);
hardButton.setVisible(false);
backButton.setVisible(true);
leftSelectedButton.setVisible(false);
rightSelectedButton.setVisible(false);Background=newImageIcon(Main.class.getResource("../images/gameBackground.jpg")).getImage();
isGameScreen =true;
isMainScreen =false;//接收键盘事件addKeyListener(newKeyListener_());//对于不同歌曲传入不同参数
game =newGame(trackList.get(nowSelected).getTitleName(), difficulty, trackList.get(nowSelected).getGameMusic());
game.start();//始终捕捉键盘焦点setFocusable(true);}//点击返回按钮返回上一个界面的函数publicvoidbackMain(){
easyButton.setVisible(true);
hardButton.setVisible(true);
backButton.setVisible(false);
leftSelectedButton.setVisible(true);
rightSelectedButton.setVisible(true);Background=newImageIcon(Main.class.getResource("../images/introBackground.jpg")).getImage();
isMainScreen =true;//返回的第二个界面是游戏选择界面selectTrack(nowSelected);//第二个界面不是正式游戏界面
isGameScreen =false;//关闭游戏中的音乐
game.close();}publicvoidenterMain(){//点击开始按钮后隐藏start和exit按钮
startButton.setVisible(false);
exitButton.setVisible(false);//点击开始按钮后显示easy和hard按钮和左右选择按钮
easyButton.setVisible(true);
hardButton.setVisible(true);
leftSelectedButton.setVisible(true);
rightSelectedButton.setVisible(true);Background=newImageIcon(Main.class.getResource("../images/introBackground.jpg")).getImage();//进入到游戏主界面(选择界面)
isMainScreen =true;//点击开始按钮关闭第一个页面的bgm
introMusic.close();//默认点击开始首先进入第一首歌selectTrack(0);}}
3.6 Music.java
packageMstw;importjavazoom.jl.player.Player;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;publicclassMusicextendsThread{privatePlayer player;privateboolean isLoop;privateFile file;privateFileInputStream fis;privateBufferedInputStream bis;//重载构造函数,传入参数为名字和是否循环publicMusic(String name,boolean isLoop){try{this.isLoop = isLoop;//创建一个File对象
file =newFile(Main.class.getResource("../music/"+name).toURI());//创建一个输入流
fis =newFileInputStream(file);//创建一个缓冲流
bis =newBufferedInputStream(fis);//创建播放器对象,将文件的缓冲输入流传入进去
player =newPlayer(bis);}catch(Exception e){
e.printStackTrace();}}publicintgetTime(){if(player ==null)return0;return player.getPosition();}publicvoidclose(){
isLoop =false;
player.close();this.interrupt();}@Overridepublicvoidrun(){try{do{
player.play();//用播放方法进行播放
fis =newFileInputStream(file);
bis =newBufferedInputStream(fis);
player =newPlayer(bis);}while(isLoop);//循环播放,直到有终止条件isLoop = false}catch(Exception e){
e.printStackTrace();}}}
3.7 Note.java
packageMstw;importjavax.swing.*;importjava.awt.*;publicclassNoteextendsThread{privateImage noteBasicImage =newImageIcon(Main.class.getResource("../images/noteBasic.png")).getImage();privateint x, y =600-(1000/Main.sleep_time *Main.note_speed)*Main.reach_time;privateString noteType;privateboolean proceeded =true;publicbooleanisProceeded(){return proceeded;}publicStringgetNoteType(){return noteType;}publicvoidclose(){
proceeded =false;}publicNote(String noteType){if(noteType.equals("S")){
x =424;}if(noteType.equals("D")){
x =578;}if(noteType.equals("F")){
x =732;}if(noteType.equals("Space")){
x =886;}if(noteType.equals("J")){
x =1040;}if(noteType.equals("K")){
x =1194;}if(noteType.equals("L")){
x =1348;}this.noteType = noteType;}publicvoidscreenDraw(Graphics2D g){
g.drawImage(noteBasicImage, x, y,null);}//方块下落函数publicvoiddrop(){
y +=Main.note_speed;//以一个速度下落//方块超出判定区就消失,并且算Missif(y >965){System.out.println("Miss");close();}}@Overridepublicvoidrun(){try{while(true){drop();if(proceeded){Thread.sleep(Main.sleep_time);}else{interrupt();break;}}}catch(Exception e){
e.printStackTrace();}}publicStringjudge(){if(y >935){System.out.println("Late");Main.scout +=10;close();return"Late";}elseif(y >915){System.out.println("Good");Main.scout +=20;close();return"Good";}elseif(y >895){System.out.println("Prefect");Main.scout +=40;close();return"Prefect";}elseif(y >875){System.out.println("Early");Main.scout +=10;close();return"Early";}return"None";}publicintgetY(){return y;}}
3.8 Track.java
packageMstw;publicclassTrack{privateString titleImage;privateString startImage;privateString gameImage;privateString startMusic;privateString gameMusic;privateString titleName;publicStringgetTitleImage(){return titleImage;}publicvoidsetTitleImage(String titleImage){this.titleImage = titleImage;}publicStringgetStartImage(){return startImage;}publicvoidsetStartImage(String startImage){this.startImage = startImage;}publicStringgetGameImage(){return gameImage;}publicvoidsetGameImage(String gameImage){this.gameImage = gameImage;}publicStringgetStartMusic(){return startMusic;}publicvoidsetStartMusic(String startMusic){this.startMusic = startMusic;}publicStringgetGameMusic(){return gameMusic;}publicvoidsetGameMusic(String gameMusic){this.gameMusic = gameMusic;}publicStringgetTitleName(){return titleName;}publicvoidsetTitleName(String titleName){this.titleName = titleName;}publicTrack(String titleImage,String startImage,String gameImage,String startMusic,String gameMusic,String titleName){this.titleImage = titleImage;this.startImage = startImage;this.gameImage = gameImage;this.startMusic = startMusic;this.gameMusic = gameMusic;this.titleName = titleName;}}
本文转载自: https://blog.csdn.net/qq_53569667/article/details/127141942
版权归原作者 Fetters04 所有, 如有侵权,请联系我们删除。
版权归原作者 Fetters04 所有, 如有侵权,请联系我们删除。