Java如何实现萝卜勇者游戏(java,开发技术)

时间:2024-05-05 15:26:10 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    主要设计

    1、 用Swing库做可视化界面

    2、键盘监听,用WSAD可以控制光标移动,J是确定,K是取消,游戏中,WSAD移动,J攻击,K格挡。

    3、 用线程实现画面刷新。

    4、用流实现音乐播放。

    5、支持多种语言,支持用户翻译语言文件。

    6、在帮助界面下,按键盘1234567890可以启用秘籍(传奇模式:一刀砍999血)

    7、使用的是JDK8

    8、文件编码GBK

    功能截图

    游戏开始

    Java如何实现萝卜勇者游戏

    游戏帮助

    Java如何实现萝卜勇者游戏

    游戏设置

    Java如何实现萝卜勇者游戏

    选择不同的关卡

    Java如何实现萝卜勇者游戏

    游戏效果

    Java如何实现萝卜勇者游戏

    怪物攻击效果

    Java如何实现萝卜勇者游戏

    代码实现

    启动类

    @SuppressWarnings("serial")publicclassTestWindowBuilderextendsJFrame{ TextLibrarytextLib; settingLoadersettingLib; privateJPanelcontentPane; booleanisCheating; saveSelectsaveSelectMenu; int[]startMenuPointer=newint[1]; JLabellblNewLabel1; JLabellblNewLabel2; JLabellblNewLabel3; JLabellblNewLabel3_2; JLabellblNewLabel3_1; JLabellblNewLabel4; JLabellblNewLabel5; JLabellblNewLabel6; CMusicLinkCMLHead=newCMusicLink(1); CMusicLinkCMLLast=newCMusicLink(2); booleansawExplain=true; CMusicmenuBGMusic; /** *Launchtheapplication. */ publicstaticvoidmain(String[]args){ EventQueue.invokeLater(newRunnable(){ publicvoidrun(){ try{ TestWindowBuilderframe=newTestWindowBuilder(); frame.setVisible(true); }catch(Exceptione){ e.printStackTrace(); } } }); } /** *Createtheframe. */publicTestWindowBuilder(){ this.setTitle("CARROTMANII"); this.setIconImage(newImageIcon("resource/down4.png").getImage()); intscreenWidth=Calculator.setInside(ScreenSize.getWidth(),800,99999); intscreenHeight=Calculator.setInside(ScreenSize.getHeight(),450,99999); System.out.println("启动控制台222..."); CMLHead.next=CMLLast; CMLLast.prev=CMLHead; settingLib=newsettingLoader(); settingLib.loadSetting();//加载设置 settingLib.saveSetting(); textLib=newTextLibrary(); textLib.saveText();//创建默认的语言文件 textLib.loadText(settingLib.settings[0]); startNewMenuBGMusic(1); /*游戏阶段: *0开始菜单 **/ intgamePhase=0; isCheating=false; /*开始菜单指针 *0开始 *1帮助 *2退出 **/ startMenuPointer[0]=0; intallSMPCount=5;//总指针数 TestWindowBuildertestMenu=this; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds((screenWidth-800)/2,(screenHeight-450)/2,800,450); contentPane=newJPanel(); contentPane.setBorder(newEmptyBorder(5,5,5,5)); setContentPane(contentPane); contentPane.setLayout(null); JPanelmainMenu=newJPanel(); mainMenu.setBounds(0,0,784,412); contentPane.add(mainMenu); mainMenu.setLayout(null); lblNewLabel1=newJLabel();//菜单标题 lblNewLabel1.setBounds(192,10,400,150); lblNewLabel1.setIcon(newImageIcon("resource/MenuTitle.png")); mainMenu.add(lblNewLabel1); lblNewLabel2=newJLabel();//开始 lblNewLabel2.setBounds(10,193,764,43); lblNewLabel2.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel2.setFont(newFont("Adobe黑体StdR",Font.PLAIN,21)); lblNewLabel2.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel2); lblNewLabel3=newJLabel();//帮助 lblNewLabel3.setBounds(10,223,764,43); lblNewLabel3.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel3.setFont(newFont("Adobe黑体StdR",Font.PLAIN,21)); lblNewLabel3.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel3); lblNewLabel3_2=newJLabel();//设置 lblNewLabel3_2.setBounds(10,253,764,43); lblNewLabel3_2.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel3_2.setFont(newFont("Adobe黑体StdR",Font.PLAIN,21)); lblNewLabel3_2.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel3_2); lblNewLabel3_1=newJLabel();//语言 lblNewLabel3_1.setBounds(10,283,764,43); lblNewLabel3_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel3_1.setFont(newFont("Adobe黑体StdR",Font.PLAIN,21)); lblNewLabel3_1.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel3_1); lblNewLabel4=newJLabel();//退出 lblNewLabel4.setBounds(10,313,764,43); lblNewLabel4.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel4.setFont(newFont("Adobe黑体StdR",Font.PLAIN,21)); lblNewLabel4.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel4); lblNewLabel5=newJLabel(textLib.textData[5]);//底部提示 lblNewLabel5.setBounds(10,371,764,31); lblNewLabel5.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel5.setFont(newFont("Adobe黑体StdR",Font.PLAIN,15)); lblNewLabel5.setForeground(newColor(200,200,200)); mainMenu.add(lblNewLabel5); lblNewLabel6=newJLabel();//半透明黑背景 lblNewLabel6.setBounds(267,193,250,209); lblNewLabel6.setIcon(newImageIcon("resource/mainMenuButtonBackGround.png")); mainMenu.add(lblNewLabel6); JLabelMenuBackGroundLabel=newJLabel();//白色背景 MenuBackGroundLabel.setBounds(0,0,800,450); MenuBackGroundLabel.setIcon(newImageIcon("resource/mainMenuBackGround.png")); mainMenu.add(MenuBackGroundLabel); reflash(); //键盘监听器 this.addKeyListener(newKeyAdapter(){ publicvoidkeyPressed(KeyEvente){ charcmd=e.getKeyChar(); if(gamePhase==0)//开始菜单 { if(sawExplain==false) { sawExplain=true; StartViewsv=newStartView(testMenu); sv.setVisible(true); testMenu.setVisible(false); } else { if(TestWindowBuilder.charEqualsIgnoreCase(cmd,'w')) { startMenuPointer[0]=(startMenuPointer[0]-1+allSMPCount)%allSMPCount; startNewSound("pointerMove.wav",false,1); } if(TestWindowBuilder.charEqualsIgnoreCase(cmd,'s')) { startMenuPointer[0]=(startMenuPointer[0]+1)%allSMPCount; startNewSound("pointerMove.wav",false,1); } reflash(); if(TestWindowBuilder.charEqualsIgnoreCase(cmd,'j')) { if(startMenuPointer[0]==0) { saveSelectMenu=newsaveSelect(testMenu); saveSelectMenu.setVisible(true); testMenu.setVisible(false); startNewSound("menuChange.wav",false,1); } if(startMenuPointer[0]==1) { HelpMenuhelpMenu=newHelpMenu(testMenu); helpMenu.setVisible(true); testMenu.setVisible(false); startNewSound("menuChange.wav",false,1); } if(startMenuPointer[0]==2) { settingMenusMenu=newsettingMenu(testMenu); sMenu.setVisible(true); testMenu.setVisible(false); startNewSound("menuChange.wav",false,1); } if(startMenuPointer[0]==3) { settingLanguagesettingMenu=newsettingLanguage(testMenu); settingMenu.setVisible(true); testMenu.setVisible(false); startNewSound("menuChange.wav",false,1); } if(startMenuPointer[0]==4) { startNewSound("menuChange.wav",false,1); System.exit(0); } } } } } }); } publicvoidreflash() { lblNewLabel1.setText(textLib.textData[1]); lblNewLabel2.setText(textLib.textData[6]); lblNewLabel3.setText(textLib.textData[3]); lblNewLabel3_2.setText(textLib.textData[60]); lblNewLabel3_1.setText(textLib.textData[49]); lblNewLabel4.setText(textLib.textData[4]); lblNewLabel5.setText(textLib.textData[5]); if(startMenuPointer[0]==0) { lblNewLabel2.setText(textLib.textData[6]+"<--"); } if(startMenuPointer[0]==1) { lblNewLabel3.setText(textLib.textData[3]+"<--"); } if(startMenuPointer[0]==2) { lblNewLabel3_2.setText(textLib.textData[60]+"<--"); } if(startMenuPointer[0]==3) { lblNewLabel3_1.setText(textLib.textData[49]+"<--"); } if(startMenuPointer[0]==4) { lblNewLabel4.setText(textLib.textData[4]+"<--"); } } publicvoidstartNewMenuBGMusic(intprepare) { if(settingLib.settings[1].equals("1")) { if(menuBGMusic!=null) menuBGMusic.endMusic(); menuBGMusic=newCMusic("menuMusic.wav",true,30000); menuBGMusic.start(); } elseif(prepare==1) { if(menuBGMusic!=null) menuBGMusic.endMusic(); menuBGMusic=newCMusic("menuMusic.wav",true,30000); menuBGMusic.start(); menuBGMusic.pauseMusic(); } } publicvoidstartNewSound(StringsoundName,booleanisReplay,intreplayDelay)//开启新音效 { if(settingLib.settings[2].equals("1")) newCMusicLink(CMLLast,soundName,isReplay,replayDelay); } /** *判断字母a和字母b是否相同(无视大小写) *@parama字母a *@paramb字母b *@returntrue:字母a和字母b相同|false:字母a和字母b不同,或ab中存在非字母 */ publicstaticbooleancharEqualsIgnoreCase(chara,charb) { if(((int)a)>=65&&((int)a)<=90)//大写 { if(a==b) returntrue; if(a+32==b) returntrue; } if(((int)a)>=97&&((int)a)<=122)//小写 { if(a==b) returntrue; if(a-32==b) returntrue; } returnfalse; }}

    键盘监听

    publicclassKeyLininterextendsKeyAdapter{ GameMenugameMenu; publicKeyLininter(GameMenugameMenu) { this.gameMenu=gameMenu; } publicvoidkeyPressed(KeyEvente){ charcmd=e.getKeyChar(); if(cmd=='w'||cmd=='W') { gameMenu.pressingW=1; } if(cmd=='s'||cmd=='S') { gameMenu.pressingS=1; } if(cmd=='a'||cmd=='A') { gameMenu.pressingA=1; } if(cmd=='d'||cmd=='D') { gameMenu.pressingD=1; } if(cmd=='j'||cmd=='J') { gameMenu.pressingJ=1; } if(cmd=='k'||cmd=='K') { gameMenu.pressingK=1; } } publicvoidkeyReleased(KeyEvente) { charcmd=e.getKeyChar(); if(cmd=='w'||cmd=='W') { gameMenu.pressingW=0; } if(cmd=='s'||cmd=='S') { gameMenu.pressingS=0; } if(cmd=='a'||cmd=='A') { gameMenu.pressingA=0; } if(cmd=='d'||cmd=='D') { gameMenu.pressingD=0; } if(cmd=='j'||cmd=='J') { gameMenu.pressingJ=0; } if(cmd=='k'||cmd=='K') { gameMenu.pressingK=0; } }}

    核心算法

    @SuppressWarnings("serial")publicclassGameMenuextendsJFrame{ GameMenuthisMenu; privateJPanelcontentPane; TestWindowBuilderfatherMenu; booleanisCheating; CMusicbattleBGM,questCompleteBGM; intpressingW; intpressingS; intpressingA; intpressingD; intpressingJ; intpressingK; intplayingSave; intplayingProfessionID; intg_int1; RoleplayingRole; doubleplayerX;//0-734 doubleplayerY;//0-312 introtationSpeed=15;//旋转速度 intmovementSpeed=3;//移动速度 intNormalSpeed=3; intattackSpeed=3;//攻击速度 intactiveCounter; intattackCounter; intdefenceCounter=0; intperfectDefenceTime=0;//完美格挡剩余时间(在敌人发动攻击前0.1秒启动的格挡能让格挡所需体力消耗减少到原来的十分之一) intdefenceCooldown=0;//格挡冷却这样阻止了玩家疯狂地启动/停止格挡 intplayerWidth=50; intplayerHeight=50; intgamePhaseTimeCounter=0; intgamePhaseTimeCounter2=0; intendPhase=6*4;//游戏通关的阶段 /*第一关0 *第二关6 **/ intgamePhase=0;//游戏开始时的阶段(正常玩家从第一关开始打,阶段应当从0开始) StringhumanIconContorler; JPanelviewMap; //玩家有两个Label一个在boss上方一个在boss下方 intusingPlayerLabel=0; JLabel[]playerLabel=newJLabel[2]; JLabellblNewLabel_6; JLabellblNewLabel_7; JLabel[]GiantBackGround=newJLabel[400]; JLabelbossLabel; JLabellblNewLabel_2; JLabellblNewLabel_3; JLabellblNewLabel_2B; JLabellblNewLabel_3B; JLabelMobHead; JLabelMobName; JLabelMobHPBar; JLabelMobHPText; JLabelTitleLabelT; JLabelSubTitleLabelT; JLabelTitleLabel; JLabelSubTitleLabel; JPanelTitles; JLabelplaceNameLabel; JLabelgroundArrayLabel; JLabelplaceChangeBlack;//换场地时的黑幕 JLabelplaceChangeBlack_1;//换场地时的黑幕(用于属性条) intmaxProCount=5000; JLabel[]proLabel=newJLabel[maxProCount]; boolean[]proIsUsed=newboolean[maxProCount]; proLinkproHead=newproLink(); intexistProCount; intmaxParCount=5000; JLabel[]parLabel=newJLabel[maxParCount]; boolean[]parIsUsed=newboolean[maxParCount]; parLinkparHead=newparLink(); intexistParCount; intexistPro=0; intproTeamPointer=0;//队列指针0-499 intexistPar=0; intparTeamPointer=0; JPanelpanel; Mobboss; intallPhaseCount=1; intgameTime=0; Mapmap; /** *Createtheframe. */ publicGameMenu(TestWindowBuilderfatherMenu,booleanisCheating,intprofessionID,intpartID,intplayingSave) { this.setTitle("CARROTMANII"); this.setIconImage(newImageIcon("resource/down4.png").getImage()); for(inti=0;i<proIsUsed.length;++i) proIsUsed[i]=false; for(inti=0;i<parIsUsed.length;++i) parIsUsed[i]=false; playingProfessionID=professionID; this.playingSave=playingSave; gamePhase=(partID-1)*6; this.fatherMenu=fatherMenu; this.isCheating=isCheating; //this.isCheating=true;//测试用 g_int1=0; playerX=50; playerY=200; thisMenu=this; activeCounter=0; attackCounter=0; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(fatherMenu.getBounds()); contentPane=newJPanel(); contentPane.setBorder(newEmptyBorder(5,5,5,5)); setContentPane(contentPane); contentPane.setLayout(null); //转场黑幕第100层 placeChangeBlack=newJLabel(""); placeChangeBlack.setIcon(newImageIcon("resource/black.png")); placeChangeBlack.setVisible(true); placeChangeBlack_1=newJLabel(""); placeChangeBlack_1.setIcon(newImageIcon("resource/black.png")); placeChangeBlack_1.setVisible(true); placeChangeBlack.setBounds(0,0,784,362); placeChangeBlack_1.setBounds(0,0,784,362); //contentPane.add(placeChangeBlack); JPanelpanel_1=newJPanel(); panel_1.setBounds(0,0,784,50); contentPane.add(panel_1); panel_1.setLayout(null); panel_1.add(placeChangeBlack_1); JLabellblNewLabel=newJLabel("");//头像 lblNewLabel.setBounds(0,0,50,50); lblNewLabel.setIcon(newImageIcon("resource/HPBarHead.png")); panel_1.add(lblNewLabel); lblNewLabel_2B=newJLabel("");//血量字 lblNewLabel_2B.setForeground(Color.WHITE); lblNewLabel_2B.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_2B.setBounds(50,0,200,25); panel_1.add(lblNewLabel_2B); lblNewLabel_3B=newJLabel("");//体力字 lblNewLabel_3B.setForeground(Color.WHITE); lblNewLabel_3B.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_3B.setBounds(50,25,200,25); panel_1.add(lblNewLabel_3B); lblNewLabel_2=newJLabel("");//血条 lblNewLabel_2.setBounds(50,0,200,25); lblNewLabel_2.setIcon(newImageIcon("resource/RedBar.png")); panel_1.add(lblNewLabel_2); lblNewLabel_3=newJLabel("");//体力条 lblNewLabel_3.setBounds(50,25,200,25); lblNewLabel_3.setIcon(newImageIcon("resource/GreenBar.png")); panel_1.add(lblNewLabel_3); JLabellblNewLabel_1=newJLabel("");//血条体力条背景 lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1.setBounds(50,0,200,50); lblNewLabel_1.setIcon(newImageIcon("resource/HPBarBG.png")); panel_1.add(lblNewLabel_1); JLabellblNewLabel_4=newJLabel("J"); lblNewLabel_4.setForeground(Color.GRAY); lblNewLabel_4.setFont(newFont("黑体",Font.BOLD,30)); lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_4.setBounds(250,0,50,50); panel_1.add(lblNewLabel_4); JLabellblNewLabel_5=newJLabel(""); lblNewLabel_5.setFont(newFont("宋体",Font.PLAIN,20)); lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_5.setBounds(250,0,50,50); lblNewLabel_5.setIcon(newImageIcon("resource/skillJ.png")); panel_1.add(lblNewLabel_5); JLabelskillKLabel=newJLabel("K"); skillKLabel.setForeground(Color.GRAY); skillKLabel.setFont(newFont("黑体",Font.BOLD,30)); skillKLabel.setHorizontalAlignment(SwingConstants.CENTER); skillKLabel.setBounds(300,0,50,50); panel_1.add(skillKLabel); JLabelskillKLabel2=newJLabel(""); skillKLabel2.setFont(newFont("宋体",Font.PLAIN,20)); skillKLabel2.setHorizontalAlignment(SwingConstants.CENTER); skillKLabel2.setBounds(300,0,50,50); skillKLabel2.setIcon(newImageIcon("resource/skillK.png")); panel_1.add(skillKLabel2); placeNameLabel=newJLabel(""); placeNameLabel.setFont(newFont("宋体",Font.PLAIN,30)); placeNameLabel.setHorizontalAlignment(SwingConstants.CENTER); placeNameLabel.setBounds(350,0,200,50); panel_1.add(placeNameLabel); JLabelplaceNameLabel2=newJLabel(""); placeNameLabel2.setHorizontalAlignment(SwingConstants.CENTER); placeNameLabel2.setBounds(350,0,200,50); placeNameLabel2.setIcon(newImageIcon("resource/placeNameBackGround.png")); panel_1.add(placeNameLabel2); MobHead=newJLabel(""); MobHead.setHorizontalAlignment(SwingConstants.CENTER); MobHead.setBounds(550,0,50,50); MobHead.setIcon(newImageIcon("resource/MobHead_0.png")); panel_1.add(MobHead); MobName=newJLabel(""); MobName.setHorizontalAlignment(SwingConstants.CENTER); MobName.setBounds(600,0,184,25); panel_1.add(MobName); MobHPText=newJLabel(""); MobHPText.setForeground(Color.WHITE); MobHPText.setHorizontalAlignment(SwingConstants.CENTER); MobHPText.setBounds(600,25,184,25); panel_1.add(MobHPText); MobHPBar=newJLabel(""); MobHPBar.setHorizontalAlignment(SwingConstants.CENTER); MobHPBar.setBounds(600,25,184,25); MobHPBar.setIcon(newImageIcon("resource/RedBar.png")); panel_1.add(MobHPBar); JLabelMobNameBG=newJLabel(""); MobNameBG.setHorizontalAlignment(SwingConstants.CENTER); MobNameBG.setBounds(600,0,184,50); MobNameBG.setIcon(newImageIcon("resource/MobNameBG.png")); panel_1.add(MobNameBG); panel=newJPanel(); panel.setBounds(0,50,784,362); contentPane.add(panel); viewMap=panel; panel.setLayout(null); panel.add(placeChangeBlack); lblNewLabel_7=newJLabel("");//玩家tryingFace指示器(暂时停用) lblNewLabel_7.setBounds((int)playerX,(int)playerY,5,5); lblNewLabel_7.setIcon(newImageIcon("resource/moveDeraction2.png")); lblNewLabel_7.setVisible(false); panel.add(lblNewLabel_7); //第99层 //标题 TitleLabelT=newJLabel(""); TitleLabelT.setFont(newFont("黑体",Font.PLAIN,45)); TitleLabelT.setHorizontalAlignment(SwingConstants.CENTER); TitleLabelT.setBounds(100,50,584,50); TitleLabelT.setVisible(true); panel.add(TitleLabelT); //副标题 SubTitleLabelT=newJLabel(""); SubTitleLabelT.setFont(newFont("黑体",Font.PLAIN,25)); SubTitleLabelT.setHorizontalAlignment(SwingConstants.CENTER); SubTitleLabelT.setBounds(100,100,584,50); SubTitleLabelT.setVisible(true); panel.add(SubTitleLabelT); //预先创建粒子效果的Label,第6层 for(inti=0;i<maxParCount;++i) { parLabel[i]=newJLabel(""); panel.add(parLabel[i]); } //boss上方的玩家,第5层 playerLabel[1]=newJLabel(""); playerLabel[1].setHorizontalAlignment(SwingConstants.CENTER); playerLabel[1].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerHeight,playerWidth); playerLabel[1].setIcon(newImageIcon("resource/HumanRight_0.png")); playerLabel[1].setVisible(false); panel.add(playerLabel[1]); //bossLabel,第4.5层 bossLabel=newJLabel(""); panel.add(bossLabel); //boss下方的玩家,第4层 playerLabel[0]=newJLabel(""); playerLabel[0].setHorizontalAlignment(SwingConstants.CENTER); playerLabel[0].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerHeight,playerWidth); playerLabel[0].setIcon(newImageIcon("resource/HumanRight_0.png")); playerLabel[0].setVisible(true); panel.add(playerLabel[0]); //玩家面向方向指示器,第3层 lblNewLabel_6=newJLabel(""); lblNewLabel_6.setBounds((int)playerX,(int)playerY,5,5); lblNewLabel_6.setIcon(newImageIcon("resource/moveDeraction.png")); panel.add(lblNewLabel_6); //预先创建发射物的Label,第2层 for(inti=0;i<maxProCount;++i) { proLabel[i]=newJLabel(""); panel.add(proLabel[i]); } //创建地面箭头,第1.5层 groundArrayLabel=newJLabel(""); groundArrayLabel.setBounds(709,156,50,50); groundArrayLabel.setIcon(newImageIcon("resource/groundArray.png")); groundArrayLabel.setVisible(false); panel.add(groundArrayLabel); //创建地面Label,第1层 for(inti=0;i<400;++i) { GiantBackGround[i]=newJLabel(""); GiantBackGround[i].setVisible(true); panel.add(GiantBackGround[i]); } playingRole=newRole(professionID,thisMenu); KeyLininterkl=newKeyLininter(thisMenu);this.addKeyListener(kl); map=newMap(thisMenu); map.start(); } //发射物相关 publicintaddProjectile(Projectilepro) { existProCount=proHead.getLength(); if(existProCount<maxProCount) { proHead.insert(newproLink(pro)); inttempFinder=0; for(inti=0;i<maxProCount;++i) { if(proIsUsed[i]==false) { proIsUsed[i]=true; tempFinder=i; break; } } returntempFinder; } return-1; } publicvoidremoveProjectile(intid) { proLabel[id].setVisible(false); proIsUsed[id]=false; } publicvoidallProjectilesFly() { proLinktempNode=proHead; while(tempNode.next!=null) { tempNode=tempNode.next; tempNode.data.doFly(); } } //发射物相关结束 //粒子效果相关 publicintaddParticle(particlepar) { existParCount=parHead.getLength(); if(existParCount<maxParCount) { parHead.insert(newparLink(par)); inttempFinder=0; for(inti=0;i<maxParCount;++i) { if(parIsUsed[i]==false) { parIsUsed[i]=true; tempFinder=i; break; } } returntempFinder; } return-1; } publicvoidremoveParticle(intid) { parLabel[id].setVisible(false); parIsUsed[id]=false; } publicvoidallParticlesFly() { parLinktempNode=parHead; while(tempNode.next!=null) { tempNode=tempNode.next; tempNode.data.doFly(); } } publicvoidcheckPlayerLocation()//检测玩家位置如果超出地图则拉回地图 { if(playerX<playerWidth/2) playerX=playerWidth/2; if(playerX>784-playerWidth/2) playerX=784-playerWidth/2; if(playerY<playerHeight/2) playerY=playerHeight/2; if(playerY>362-playerHeight/2) playerY=362-playerHeight/2; } @SuppressWarnings("unused") publicvoidsaveData(intpart) { if(Integer.parseInt(fatherMenu.saveSelectMenu.savedData[playingSave].split("")[1])<part) { String[]temp={ fatherMenu.saveSelectMenu.savedData[playingSave].split("")[0], fatherMenu.saveSelectMenu.savedData[playingSave].split("")[1] }; fatherMenu.saveSelectMenu.savedData[playingSave]=""+playingProfessionID+""+part; fatherMenu.saveSelectMenu.saveSaves(); } } @SuppressWarnings({"deprecation"}) publicvoidmodTick()//每秒执行50次 { if(attackCounter==0&&defenceCounter==0) playingRole.regenerate(); if(playingRole.HP>0) { //刷新体力条 lblNewLabel_3.setBounds(50,25,(int)(200*playingRole.energy/playingRole.MaxEnergy),25); lblNewLabel_3B.setText(""+(int)playingRole.energy+"/"+(int)playingRole.MaxEnergy); //刷新血条 lblNewLabel_2.setBounds(50,0,(int)(200*playingRole.HP/playingRole.MaxHP),25); lblNewLabel_2B.setText(""+(int)playingRole.HP+"/"+(int)playingRole.MaxHP); } elseif(gamePhase!=-1) { boss.target=null; gamePhaseTimeCounter=0; gamePhase=-1; movementSpeed=0; TitleLabelT.setText("YOUDIED"); TitleLabelT.setVisible(true); playerLabel[usingPlayerLabel].setVisible(false); lblNewLabel_6.setVisible(false); lblNewLabel_3.setBounds(50,25,(int)(200*playingRole.energy/playingRole.MaxEnergy),25); lblNewLabel_3B.setText(""+(int)playingRole.energy+"/"+(int)playingRole.MaxEnergy); lblNewLabel_2.setBounds(50,0,(int)(200*playingRole.HP/playingRole.MaxHP),25); lblNewLabel_2B.setText(""+(int)playingRole.HP+"/"+(int)playingRole.MaxHP); } ++gameTime; allProjectilesFly(); allParticlesFly(); if(gamePhase==0) { if(gamePhaseTimeCounter<50) gamePhaseTimeCounter=50; ++gamePhaseTimeCounter; if(gamePhaseTimeCounter==51) { System.out.println("启动控制台..."); movementSpeed=0; placeChangeBlack.setBounds(0,0,784,362); placeChangeBlack_1.setBounds(0,0,784,362); placeChangeBlack.setVisible(true); placeChangeBlack_1.setVisible(true); TitleLabelT.setVisible(false); SubTitleLabelT.setVisible(false); groundArrayLabel.setVisible(false); for(inti=0;i<128;++i)//初始化第一关地面 { StringbackGroundContorler="resource/ground"; GiantBackGround[i].setBounds(50*(i%16),50*(int)(i/16),50,50); if(Math.random()<0.9) backGroundContorler=backGroundContorler+"1.png"; else backGroundContorler=backGroundContorler+"2.png"; GiantBackGround[i].setIcon(newImageIcon(backGroundContorler)); } playerX=50; playerY=200; placeNameLabel.setText(fatherMenu.textLib.textData[13]); TitleLabelT.setText(fatherMenu.textLib.textData[13]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[37]); SubTitleLabelT.setVisible(true); } if(gamePhaseTimeCounter>=100&&gamePhaseTimeCounter<=150) { placeChangeBlack.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); placeChangeBlack_1.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); } if(gamePhaseTimeCounter==151) { startNewBattleBGM(1); placeChangeBlack.setVisible(false); placeChangeBlack_1.setVisible(false); gamePhaseTimeCounter=0; ++gamePhase; } } if(gamePhase%6==1) { ++gamePhaseTimeCounter; if(gamePhaseTimeCounter>=200) { ++gamePhase; TitleLabelT.setVisible(false); SubTitleLabelT.setVisible(false); movementSpeed=NormalSpeed; } } if(gamePhase==2) { gamePhaseTimeCounter=0; boss=newMob(1,bossLabel,thisMenu); boss.reflash(); bossLabel.setVisible(true); ++gamePhase; } if(gamePhase==3) { if(boss.HP>0) boss.reflash(); else { bossLabel.setVisible(false); MobHPBar.setBounds(600,25,184*boss.HP/boss.MaxHP,25); MobHPText.setText(""+boss.HP+"/"+boss.MaxHP); ++gamePhase; gamePhaseTimeCounter=0; TitleLabelT.setText(fatherMenu.textLib.textData[38]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[39]); SubTitleLabelT.setVisible(true); saveData(2); startQuestCompleteBGM(); endBattleBGM(); } } if(gamePhase%6==4) { playingRole.percentReHP(0.005); ++gamePhaseTimeCounter; if(gamePhaseTimeCounter>200) { gamePhaseTimeCounter=0; ++gamePhase; TitleLabelT.setText(fatherMenu.textLib.textData[40]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[41]); SubTitleLabelT.setVisible(true); gamePhaseTimeCounter2=0; } } if(gamePhase%6==5) { ++gamePhaseTimeCounter; if(gamePhaseTimeCounter2==0) { if(gamePhaseTimeCounter>50) { groundArrayLabel.setVisible(true); if(playerX>709&&playerX<759&&playerY>156&&playerY<206)//进入箭头 { gamePhaseTimeCounter=0; gamePhaseTimeCounter2=1; movementSpeed=0; placeChangeBlack.setBounds(-784,0,784,362); placeChangeBlack.setVisible(true); placeChangeBlack_1.setBounds(-784,0,784,362); placeChangeBlack_1.setVisible(true); } } } if(gamePhaseTimeCounter2==1) { if(gamePhaseTimeCounter>=0&&gamePhaseTimeCounter<=50) { placeChangeBlack.setBounds((int)(gamePhaseTimeCounter*0.02*784-784),0,784,362); placeChangeBlack_1.setBounds((int)(gamePhaseTimeCounter*0.02*784-784),0,784,362); } if(gamePhaseTimeCounter==51) { ++gamePhase; gamePhaseTimeCounter=0; } } } if(gamePhase==6) { movementSpeed=0; if(gamePhaseTimeCounter<50) gamePhaseTimeCounter=50; ++gamePhaseTimeCounter; if(gamePhaseTimeCounter==51) { placeChangeBlack.setBounds(0,0,784,362); placeChangeBlack.setVisible(true); placeChangeBlack_1.setBounds(0,0,784,362); placeChangeBlack_1.setVisible(true); TitleLabelT.setVisible(false); SubTitleLabelT.setVisible(false); groundArrayLabel.setVisible(false); for(inti=0;i<325;++i) { GiantBackGround[i].setBounds(32*(i%25),32*(int)(i/25),32,32); GiantBackGround[i].setIcon(newImageIcon("resource/ground6.png")); } playerX=50; playerY=200; placeNameLabel.setText(fatherMenu.textLib.textData[14]); TitleLabelT.setText(fatherMenu.textLib.textData[14]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[42]); SubTitleLabelT.setVisible(true); } if(gamePhaseTimeCounter>=100&&gamePhaseTimeCounter<=150) { placeChangeBlack.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); placeChangeBlack_1.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); } if(gamePhaseTimeCounter==151) { startNewBattleBGM(2); placeChangeBlack.setVisible(false); placeChangeBlack_1.setVisible(false); gamePhaseTimeCounter=0; ++gamePhase; } } if(gamePhase==8) { gamePhaseTimeCounter=0; boss=newMob(2,bossLabel,thisMenu); bossLabel.setBounds(375,175,100,100); boss.reflash(); bossLabel.setVisible(true); ++gamePhase; } if(gamePhase==9) { if(boss.HP>0) boss.reflash(); else { bossLabel.setVisible(false); MobHPBar.setBounds(600,25,184*boss.HP/boss.MaxHP,25); MobHPText.setText(""+boss.HP+"/"+boss.MaxHP); ++gamePhase; gamePhaseTimeCounter=0; TitleLabelT.setText(fatherMenu.textLib.textData[43]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[39]); SubTitleLabelT.setVisible(true); saveData(3); startQuestCompleteBGM(); endBattleBGM(); } } if(gamePhase==12) { movementSpeed=0; if(gamePhaseTimeCounter<50) gamePhaseTimeCounter=50; ++gamePhaseTimeCounter; if(gamePhaseTimeCounter==51) { placeChangeBlack.setBounds(0,0,784,362); placeChangeBlack.setVisible(true); placeChangeBlack_1.setBounds(0,0,784,362); placeChangeBlack_1.setVisible(true); TitleLabelT.setVisible(false); SubTitleLabelT.setVisible(false); groundArrayLabel.setVisible(false); for(inti=0;i<325;++i) { GiantBackGround[i].setBounds(32*(i%25),32*(int)(i/25),32,32); GiantBackGround[i].setIcon(newImageIcon("resource/ground7.png")); } playerX=50; playerY=200; placeNameLabel.setText(fatherMenu.textLib.textData[15]); TitleLabelT.setText(fatherMenu.textLib.textData[15]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[44]); SubTitleLabelT.setVisible(true); } if(gamePhaseTimeCounter>=100&&gamePhaseTimeCounter<=150) { placeChangeBlack.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); placeChangeBlack_1.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); } if(gamePhaseTimeCounter==151) { startNewBattleBGM(3); placeChangeBlack.setVisible(false); placeChangeBlack_1.setVisible(false); gamePhaseTimeCounter=0; ++gamePhase; } } if(gamePhase==14) { gamePhaseTimeCounter=0; boss=newMob(3,bossLabel,thisMenu); bossLabel.setBounds(375,175,100,100); boss.reflash(); bossLabel.setVisible(true); ++gamePhase; } if(gamePhase==15) { if(boss.HP>0) boss.reflash(); else { bossLabel.setVisible(false); MobHPBar.setBounds(600,25,184*boss.HP/boss.MaxHP,25); MobHPText.setText(""+boss.HP+"/"+boss.MaxHP); ++gamePhase; gamePhaseTimeCounter=0; TitleLabelT.setText(fatherMenu.textLib.textData[45]); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[39]); SubTitleLabelT.setVisible(true); saveData(4); startQuestCompleteBGM(); endBattleBGM(); } } if(gamePhase==18) { movementSpeed=0; if(gamePhaseTimeCounter<50) gamePhaseTimeCounter=50; ++gamePhaseTimeCounter; if(gamePhaseTimeCounter==51) { placeChangeBlack.setBounds(0,0,784,362); placeChangeBlack.setVisible(true); placeChangeBlack_1.setBounds(0,0,784,362); placeChangeBlack_1.setVisible(true); TitleLabelT.setVisible(false); SubTitleLabelT.setVisible(false); groundArrayLabel.setVisible(false); //需要修改开始 for(inti=0;i<325;++i) { GiantBackGround[i].setBounds(32*(i%25),32*(int)(i/25),32,32); GiantBackGround[i].setIcon(newImageIcon("resource/ground8.png")); } //需要修改结束 playerX=50; playerY=200; //需要修改开始 placeNameLabel.setText(fatherMenu.textLib.textData[70]); TitleLabelT.setText(fatherMenu.textLib.textData[70]); TitleLabelT.setForeground(Color.LIGHT_GRAY); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[71]); SubTitleLabelT.setForeground(Color.LIGHT_GRAY); //需要修改结束 SubTitleLabelT.setVisible(true); } if(gamePhaseTimeCounter>=100&&gamePhaseTimeCounter<=150) { placeChangeBlack.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); placeChangeBlack_1.setBounds((int)((gamePhaseTimeCounter-100)*0.02*784),0,784,362); } if(gamePhaseTimeCounter==151) { startNewBattleBGM(4);//需要修改 placeChangeBlack.setVisible(false); placeChangeBlack_1.setVisible(false); gamePhaseTimeCounter=0; ++gamePhase; } } if(gamePhase==20) { gamePhaseTimeCounter=0; boss=newMob(4,bossLabel,thisMenu);//需要修改 bossLabel.setBounds(375,175,100,100);//需要修改 boss.reflash(); bossLabel.setVisible(true); ++gamePhase; } if(gamePhase==21) { if(boss.HP>0) boss.reflash(); else { bossLabel.setVisible(false); MobHPBar.setBounds(600,25,184*boss.HP/boss.MaxHP,25); MobHPText.setText(""+boss.HP+"/"+boss.MaxHP); ++gamePhase; gamePhaseTimeCounter=0; //需要修改开始 TitleLabelT.setText(fatherMenu.textLib.textData[72]); TitleLabelT.setForeground(Color.LIGHT_GRAY); TitleLabelT.setVisible(true); SubTitleLabelT.setText(fatherMenu.textLib.textData[39]); TitleLabelT.setForeground(Color.LIGHT_GRAY); SubTitleLabelT.setVisible(true); //saveData(4); //需要修改完成 startQuestCompleteBGM(); endBattleBGM(); } } if(gamePhase==endPhase) { endBattleBGM(); winMenuWM=newwinMenu(thisMenu); WM.setVisible(true); thisMenu.setVisible(false); map.stop(); } if(gamePhase==-1) { if(boss.HP>0) boss.reflash(); ++gamePhaseTimeCounter; if(gamePhaseTimeCounter>250) { endBattleBGM(); loseMenuLM=newloseMenu(thisMenu); LM.setVisible(true); thisMenu.setVisible(false); map.stop(); } } //刷新bossBar if(boss!=null&&gamePhase%6==3) { MobHead.setIcon(newImageIcon("resource/MobHead_"+boss.MobId+".png")); MobName.setText(""+boss.Name); MobHPBar.setBounds(600,25,184*boss.HP/boss.MaxHP,25); MobHPText.setText(""+boss.HP+"/"+boss.MaxHP); } //玩家试图面向方向 if(attackCounter==0&&defenceCounter==0) { if(pressingD==1&&pressingW==0&&pressingS==0) { playingRole.tryingFace=0; } elseif(pressingD==1&&pressingW==1) { playingRole.tryingFace=45; } elseif(pressingW==1&&pressingA==0&&pressingD==0) { playingRole.tryingFace=90; } elseif(pressingW==1&&pressingA==1) { playingRole.tryingFace=135; } elseif(pressingA==1&&pressingW==0&&pressingS==0) { playingRole.tryingFace=180; } elseif(pressingA==1&&pressingS==1) { playingRole.tryingFace=225; } elseif(pressingS==1&&pressingA==0&&pressingD==0) { playingRole.tryingFace=270; } elseif(pressingS==1&&pressingD==1) { playingRole.tryingFace=315; } } playingRole.setFace(); humanIconContorler="resource/human_"+playingRole.face; if(attackCounter==0&&defenceCounter==0&&(pressingW==1||pressingS==1||pressingA==1||pressingD==1))//进行转向 { doubled_angle=(playingRole.facingAngle-playingRole.tryingFace+360)%360;//0-180为顺时针 if(d_angle<=rotationSpeed||d_angle>=360-rotationSpeed)//旋转功能 { playingRole.facingAngle=playingRole.tryingFace; } else { if(d_angle>0&&d_angle<=180) { playingRole.facingAngle-=rotationSpeed; } else { playingRole.facingAngle+=rotationSpeed; } } } if(attackCounter==0&&defenceCounter==0&&(pressingW==1||pressingS==1||pressingA==1||pressingD==1))//进行移动 { ++activeCounter; playerX+=(int)(movementSpeed*Math.cos(playingRole.facingAngle*Math.PI/180)); playerY-=(int)(movementSpeed*Math.sin(playingRole.facingAngle*Math.PI/180)); checkPlayerLocation(); if(activeCounter>=10)//10Tick一次切换移动图片 { activeCounter=0; playingRole.lastMoveActive=(playingRole.lastMoveActive+1)%4; } if(playingRole.lastMoveActive==2) humanIconContorler=humanIconContorler+'_'+0; elseif(playingRole.lastMoveActive==3) humanIconContorler=humanIconContorler+'_'+2; else humanIconContorler=humanIconContorler+'_'+playingRole.lastMoveActive; } if(pressingJ==1&&attackCounter==0&&defenceCounter==0&&playingRole.HP>0)//发动攻击 { attackCounter=1; } if(attackCounter>0&&attackCounter<=8*attackSpeed)//正在攻击 { ++attackCounter; if(attackCounter>0&&attackCounter<=attackSpeed) { humanIconContorler=humanIconContorler+'_'+3; } elseif(attackCounter<=2*attackSpeed) { humanIconContorler=humanIconContorler+'_'+4; } elseif(attackCounter<=3*attackSpeed) { humanIconContorler=humanIconContorler+'_'+5; } elseif(attackCounter<=4*attackSpeed) { humanIconContorler=humanIconContorler+'_'+6; } elseif(attackCounter<=5*attackSpeed) { humanIconContorler=humanIconContorler+'_'+7; } elseif(attackCounter<=6*attackSpeed) { humanIconContorler=humanIconContorler+'_'+8; } elseif(attackCounter<=7*attackSpeed) { humanIconContorler=humanIconContorler+'_'+9; } } if(attackCounter==3*attackSpeed)//完成攻击 { playingRole.costEnergy(10); fatherMenu.startNewSound("sweep"+Calculator.randomInt(1,3)+".wav",false,1); playingRole.doAttack(); } if(attackCounter>8*attackSpeed)//结束硬直 { attackCounter=0; } if(defenceCooldown>0) { --defenceCooldown; } if(perfectDefenceTime>0) { --perfectDefenceTime; } if(pressingK==1&&attackCounter==0&&defenceCounter==0&&defenceCooldown==0)//发动防御 { perfectDefenceTime=5; defenceCounter=1; playingRole.defenceStance=true; playingRole.isBlocked=false; } if(defenceCounter==1) { if(pressingK==0)//解除格挡 { defenceCounter=0; playingRole.defenceStance=false; if(playingRole.isBlocked==true) defenceCooldown=0; else defenceCooldown=30; } else { humanIconContorler=humanIconContorler+'_'+10; } } humanIconContorler=humanIconContorler+".png"; if(boss!=null&&playingRole.HP>0) { if(playerY>=boss.bossY&&usingPlayerLabel==0)//玩家应当盖住boss,但是却没盖住 { usingPlayerLabel=1; playerLabel[0].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerWidth,playerHeight); playerLabel[1].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerWidth,playerHeight); playerLabel[0].setVisible(false); playerLabel[1].setVisible(true); } if(playerY<boss.bossY&&usingPlayerLabel==1)//boss应当盖住玩家,但是却没盖住 { usingPlayerLabel=0; playerLabel[0].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerWidth,playerHeight); playerLabel[1].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerWidth,playerHeight); playerLabel[0].setVisible(true); playerLabel[1].setVisible(false); } } playerLabel[usingPlayerLabel].setIcon(newImageIcon(humanIconContorler)); playerLabel[usingPlayerLabel].setBounds((int)playerX-playerWidth/2,(int)playerY-playerHeight/2,playerWidth,playerHeight); lblNewLabel_6.setBounds((int)playerX+(int)(25*Math.cos(playingRole.facingAngle*Math.PI/180))-2,(int)playerY-(int)(25*Math.sin(playingRole.facingAngle*Math.PI/180))-2,4,4); lblNewLabel_7.setBounds((int)playerX+(int)(25*Math.cos(playingRole.tryingFace*Math.PI/180))-2,(int)playerY-(int)(25*Math.sin(playingRole.tryingFace*Math.PI/180))-2,4,4); } publicvoidstartNewBattleBGM(intbossID) { if(fatherMenu.settingLib.settings[1].equals("1")) { if(battleBGM!=null) battleBGM.endMusic(); battleBGM=newCMusic("boss"+bossID+".wav",true,1); battleBGM.start(); } } publicvoidendBattleBGM() { if(battleBGM!=null) battleBGM.endMusic(); } publicvoidstartQuestCompleteBGM() { if(questCompleteBGM!=null) questCompleteBGM.endMusic(); questCompleteBGM=newCMusic("QuestComplete.wav",false,1); questCompleteBGM.start(); }}
     </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
    本文:Java如何实现萝卜勇者游戏的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:小程序中rpx和px有哪些区别下一篇:

    16 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18