找回密码
 会员注册
查看: 28|回复: 0

Java简易版王者荣耀

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
64454
发表于 2024-9-3 20:07:32 | 显示全部楼层 |阅读模式
所有包和类 GameFrame类packagenewKingOfHonor;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.io.File;importjava.util.ArrayList;importjavax.sound.sampled.AudioInputStream;importjavax.sound.sampled.AudioSystem;importjavax.sound.sampled.Clip;importjavax.sound.sampled.FloatControl;importjavax.swing.JButton;importjavax.swing.JFrame;publicclassGameFrameextendsJFrame{//游戏开始0,游戏胜利1,游戏失败2intstate=0;//窗口尺寸finalintwindowWidth=1400;finalintwindowHeight=700;//双缓冲图片privateImageoffScreenImage=null;//攻击图片privateImageattack=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/attack.jpg");//游戏胜利失败图片privateImagegameWin=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/gameWin.png");privateImagegameLose=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/gameLose.png");//游戏背景Backgroundbackground=newBackground(this);//游戏玩家Championplayer=newChampionDaji(this);//双方小兵MinionBluemb=newMinionBlue(this);MinionRedmr=newMinionRed(this);//野怪publicBeastbeast=newBeast(this);//防御塔Turretturret=newTurret(this);//游戏元素列表publicArrayListobjList=newArrayList();ArrayListredList=newArrayList();//红色方ArrayListblueList=newArrayList();//蓝色方publicArrayListremoveList=newArrayList();//存放将要删除的元素publicvoidlaunch(){//设置尺寸setSize(windowWidth,windowHeight);//窗口居中setLocationRelativeTo(null);//关闭事件setDefaultCloseOperation(3);//用户不能调整窗口大小setResizable(false);//setTitle("王者荣耀");//窗口可见setVisible(true);//添加键盘监视器this.addKeyListener(newGameFrame.KeyMonitor());//添加游戏元素objList.add(background);objList.add(player);objList.addAll(beast.beastList);objList.addAll(turret.turretList);for(inti=0;igameObjList){if(hasTarget){//目标离开范围后寻找新的目标if(!recIntersectsCir(target.getRec(),getX(),getY(),getDis())){setHasTarget(false);}//目标死亡,寻找新目标elseif(!target.isAlive()){setHasTarget(false);}elseif(isAttackCoolDown()&isAlive()){Bulletbullet=null;//防御塔攻击if(Turret.class.isAssignableFrom(getClass())){bullet=newBullet(gameFrame,this,getTarget(),500,50);}//小兵攻击elseif(Minion.class.isAssignableFrom(getClass())){bullet=newBullet(gameFrame,this,getTarget(),50,30);}//玩家攻击elseif(thisinstanceofChampion){bullet=newBullet(gameFrame,this,getTarget(),500,50);}gameFrame.objList.add(bullet);//线程开始newAttackCD().start();}}else{//遍历列表for(GameObjectobj:gameObjList){//判断攻击范围(圆形)与敌方(矩形)是否相交if(recIntersectsCir(obj.getRec(),getX(),getY(),getDis())){//找到目标setTarget(obj);setHasTarget(true);//跳出循环break;}}//玩家是否在攻击范围内if(!hasTarget&gameObjList==gameFrame.blueList){if(recIntersectsCir(gameFrame.player.getRec(),getX(),getY(),getDis())){//找到目标setTarget(gameFrame.player);setHasTarget(true);}}else{//野怪是否在攻击范围内for(GameObjectobj:gameFrame.beast.beastList){//判断攻击范围(圆形)与敌方(矩形)是否相交if(recIntersectsCir(obj.getRec(),getX(),getY(),getDis())){//找到目标setTarget(obj);setHasTarget(true);//跳出循环break;}}}}}//publicabstractvoidaddTurret();classAttackCDextendsThread{publicvoidrun(){//将攻击功能设置为冷却状态setAttackCoolDown(false);//线程休眠try{Thread.sleep(attackCoolDownTime);}catch(Exceptione){e.printStackTrace();}//将攻击功能设置为攻击状态setAttackCoolDown(true);//线程终止this.interrupt();}}//绘制元素publicabstractvoidpaintSelf(Graphicsg);//返回矩形publicabstractRectanglegetRec();publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}publicImagegetImg(){returnimg;}publicvoidsetImg(Stringimg){this.img=Toolkit.getDefaultToolkit().getImage(img);}publicintgetSpd(){returnspd;}publicvoidsetSpd(intspd){this.spd=spd;}publicintgetHp(){returnhp;}publicvoidsetHp(inthp){this.hp=hp;}publicintgetCurrentHp(){returncurrentHp;}publicvoidsetCurrentHp(intcurrentHp){this.currentHp=currentHp;}publicGameObjectgetTarget(){returntarget;}publicvoidsetTarget(GameObjecttarget){this.target=target;}publicbooleanisHasTarget(){returnhasTarget;}publicvoidsetHasTarget(booleanhasTarget){this.hasTarget=hasTarget;}publicintgetDis(){returndis;}publicvoidsetDis(intdis){this.dis=dis;}publicintgetAttackCoolDownTime(){returnattackCoolDownTime;}publicvoidsetAttackCoolDownTime(intattackCoolDownTime){this.attackCoolDownTime=attackCoolDownTime;}publicbooleanisAttackCoolDown(){returnattackCoolDown;}publicvoidsetAttackCoolDown(booleanattackCoolDown){this.attackCoolDown=attackCoolDown;}publicbooleanisAlive(){returnalive;}publicvoidsetAlive(booleanalive){this.alive=alive;}}Turret类packagenewKingOfHonor;importjava.awt.*;importjava.util.ArrayList;//防御塔类publicclassTurretextendsGameObject{ArrayListturretList=newArrayList();publicTurretturretBlueOne;publicTurretturretBlueTwo;publicTurretturretBlueThree;publicTurretturretBlueBase;publicTurretturretRedOne;publicTurretturretRedTwo;publicTurretturretRedThree;publicTurretturretRedBase;publicTurret(GameFramegameFrame){super(gameFrame);setImg("img/王者荣耀图片/turret.png");//初始化八个防御塔turretList.add(turretBlueOne=newTurretBlue(1860,3790,gameFrame));turretList.add(turretBlueTwo=newTurretBlue(2650,3820,gameFrame));turretList.add(turretBlueThree=newTurretBlue(3995,3830,gameFrame));turretList.add(turretBlueBase=newTurretBlue(1130,3650,gameFrame));turretList.add(turretRedOne=newTurretRed(5100,3030,gameFrame));turretList.add(turretRedTwo=newTurretRed(5120,2100,gameFrame));turretList.add(turretRedThree=newTurretRed(5060,1570,gameFrame));turretList.add(turretRedBase=newTurretRed(4850,1100,gameFrame));}publicTurret(intx,inty,GameFramegameFrame){super(x,y,gameFrame);setImg("img/王者荣耀图片/turret.png");setHp(6000);setCurrentHp(getHp());setAttackCoolDownTime(1000);setDis(300);}publicvoidisLive(){if(gameFrame.turret.turretBlueBase.getCurrentHp()==0){gameFrame.state=2;}if(gameFrame.turret.turretRedBase.getCurrentHp()==0){gameFrame.state=1;}}@OverridepublicvoidpaintSelf(Graphicsg){//生命值为0if(getCurrentHp()attacked;//一技能移动次数intstep=0;//技能二目标GameObjectabilityTwoTarget;//技能二子弹BulletabilityTwoBullet;//三技能的五个子弹,释放三技能后重新定义Bullet[]bulletList={newBullet(),newBullet(),newBullet(),newBullet(),newBullet()};publicChampionDaji(GameFramegameFrame){super(gameFrame);abilityOne=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityOne.jpg");abilityTwo=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityTwo.jpg");abilityThree=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Daji/abilityThree.jpg");//三个技能冷却时间coolDownTimeOne=3000;coolDownTimeTwo=5000;coolDownTimeThree=8000;}publicvoidexit(){this.gameFrame.removeMouseListener(m);}publicvoidabilityOneMove(){p.translate((int)(50*cos),-(int)(50*sin));for(GameObjectredObj:gameFrame.redList){//是红色方小兵&发生碰撞&没在attacked列表里if(redObjinstanceofMinionRed&p.intersects(redObj.getRec())&attacked.indexOf(redObj)==-1){//小兵扣血,添加到attacked里redObj.setCurrentHp(redObj.getCurrentHp()-400);attacked.add(redObj);}}}@OverridepublicvoidabilityOne(){if(coolDownOne){m=newMouseMonitor();p=newPolygon();gameFrame.addMouseListener(m);attacked=newArrayList();}}publicvoidabilityTwo(){if(coolDownTwo){booleanfind=false;for(GameObjectredObj:gameFrame.objList){//是红色小兵&距离小于250&存活if(redObjinstanceofMinionRed&recIntersectsCir(redObj.getRec(),getX(),getY(),250)&redObj.isAlive()){//添加子弹abilityTwoBullet=newBullet(gameFrame,this,redObj,250,60,"img/王者荣耀图片/Daji/abilityTwoBullet.png");gameFrame.objList.add(abilityTwoBullet);//给目标赋值abilityTwoTarget=redObj;//释放二技能ifAbilityTwo=true;find=true;break;}}if(find){newAbilityTwoCD().start();find=false;}}}/***点击技能三释放技能先将技能范围内目标储存到targetList里提前定义五个子弹技能释放时初始化五个子弹子弹目标从targetList里随机选择*如果期间目标死亡,制作一个目标替身,生命值设置为true子弹与目标或替身碰撞后消失*/@OverridepublicvoidabilityThree(){if(coolDownThree){//创建列表来储存目标ArrayListtargetList=newArrayList();//遍历redList,找到符合条件的目标,储存到列表里for(inti=0;i0){Thread.sleep(1000);System.out.println("一技能冷却时间:"+one/1000);one-=1000;}}catch(Exceptione){e.printStackTrace();}//将技能一设置为攻击状态coolDownOne=true;//线程终止this.interrupt();}}//技能二冷却时间classAbilityTwoCDextendsThread{publicvoidrun(){//将技能二设置为冷却状态coolDownTwo=false;//线程休眠try{//one来表示二技能冷却时间inttwo=coolDownTimeTwo;while(two>0){Thread.sleep(1000);System.out.println("二技能冷却时间:"+two/1000);two-=1000;}}catch(Exceptione){e.printStackTrace();}//将技能二设置为攻击状态coolDownTwo=true;//线程终止this.interrupt();}}//技能二控制时间classAbilityControllCDextendsThread{publicvoidrun(){abilityTwoTarget.beControlled=true;//线程休眠try{Thread.sleep(20000);}catch(Exceptione){e.printStackTrace();}abilityTwoTarget.beControlled=false;this.interrupt();}}//技能三冷却状态classAbilityThreeCDextendsThread{publicvoidrun(){//将攻击功能设置为冷却状态coolDownThree=false;//休眠try{intthree=coolDownTimeThree;while(coolDownTimeThree>0){Thread.sleep(1000);System.out.println("技能三冷却时间:"+coolDownTimeThree/1000);coolDownTimeThree-=1000;}coolDownTimeThree=three;}catch(Exceptione){e.printStackTrace();}//将攻击功能解除冷却状态coolDownThree=true;//线程终止this.interrupt();}}classAbilityThreeBulletCDextendsThread{publicvoidrun(){//休眠try{System.out.println("Threadstart");gameFrame.objList.add(bulletList[0]);Thread.sleep(100);gameFrame.objList.add(bulletList[1]);Thread.sleep(100);gameFrame.objList.add(bulletList[2]);Thread.sleep(100);gameFrame.objList.add(bulletList[3]);Thread.sleep(100);gameFrame.objList.add(bulletList[4]);}catch(Exceptione){e.printStackTrace();}//线程终止this.interrupt();}}//鼠标监视器privateclassMouseMonitorextendsMouseAdapter{@OverridepublicvoidmousePressed(MouseEvente){//当鼠标点击时intmouseX=e.getX(),mouseY=e.getY(),playerX=700,playerY=350;doubledis=getDis(mouseX,mouseY,playerX,playerY);//三角函数cos=(mouseX-playerX)/dis;sin=-(mouseY-playerY)/dis;//坐标差intdifX=(int)(60*sin);intdifY=(int)(60*cos);p.addPoint(getX()-difX,getY()-difY);p.addPoint(getX()+difX,getY()+difY);p.addPoint(getX()+difX+(int)(20*cos),getY()+difY-(int)(20*sin));p.addPoint(getX()-difX+(int)(20*cos),getY()-difY-(int)(20*sin));exit();newAbilityOneCD().start();ifAbilityOne=true;}}}Minion类packagenewKingOfHonor;importjava.awt.*;importjava.util.ArrayList;publicabstractclassMinionextendsGameObject{//是否生成下一个小兵privatebooleannextMinion=true;//是否生成下一波小兵privatebooleannextLine=true;//生成小兵数量privateintminionCount=0;//是否检测到目标privatebooleanifFindTarget=false;publicMinion(GameFramegameFrame){super(gameFrame);setHp(800);setCurrentHp(getHp());setDis(100);setAttackCoolDownTime(2000);}/***(1325,3750)(4425,3750)(5050,3125)(5050,1125)*/publicabstractvoidmove(ArrayListobjList);publicvoidfindTarget(ArrayListobjList){for(GameObjectobjbjList){if(recIntersectsCir(obj.getRec(),getX(),getY(),200)){setTarget(obj);setIfFindTarget(true);}}if(objList==gameFrame.blueList){if(recIntersectsCir(gameFrame.player.getRec(),getX(),getY(),200)){setTarget(gameFrame.player);setIfFindTarget(true);}}}publicvoidmoveToTarget(){intdis=(int)getDis(getX(),getY(),getTarget().getX(),getTarget().getY());if(dis!=0){intxSpeed=(getSpd()*(getTarget().getX()-getX())/dis);intySpeed=(getSpd()*(getTarget().getY()-getY())/dis);setX(getX()+xSpeed);setY(getY()+ySpeed);}}publicvoidcreateMinion(GameFramegameFrame,ArrayListminionList){if(nextLine){if(nextMinion){//蓝色方小兵if(minionList==this.gameFrame.blueList){MinionBluemb=newMinionBlue(gameFrame);gameFrame.objList.add(mb);minionList.add(mb);}//红色方小兵else{MinionRedmr=newMinionRed(gameFrame);gameFrame.objList.add(mr);minionList.add(mr);}minionCount++;newNextMinion().start();}if(minionCount==3){minionCount=0;newNextLine().start();}}}//每个小兵生成时间classNextMinionextendsThread{publicvoidrun(){nextMinion=false;//休眠1.5stry{Thread.sleep(1500);}catch(Exceptione){e.printStackTrace();}nextMinion=true;//线程终止this.interrupt();}}//每波小兵生成时间classNextLineextendsThread{publicvoidrun(){nextLine=false;//休眠15stry{Thread.sleep(15000);}catch(Exceptione){e.printStackTrace();}nextLine=true;//线程终止this.interrupt();}}@OverridepublicvoidpaintSelf(Graphicsg){//生命值为0if(getCurrentHp()objList){if(isIfFindTarget()){//离开检测范围if(!recIntersectsCir(getTarget().getRec(),getX(),getY(),200)){setIfFindTarget(false);}else{if(!isHasTarget()){moveToTarget();}attack(objList);}}else{findTarget(objList);//原路线移动if(getX()=4425){setSpd(20);setX(getX()+getSpd());setY(getY()-getSpd());}elseif(getX()>=4900){setSpd(18);setY(getY()-getSpd());}}}}MinionRed类packagenewKingOfHonor;importjava.util.ArrayList;publicclassMinionRedextendsMinion{publicMinionRed(GameFramegameFrame){super(gameFrame);setImg("img/王者荣耀图片/minion/red.jpg");setX(5050);setY(1125);}@Overridepublicvoidmove(ArrayListobjList){if(isIfFindTarget()){//离开检测范围if(!recIntersectsCir(getTarget().getRec(),getX(),getY(),200)){setIfFindTarget(false);}else{if(!isHasTarget()){moveToTarget();}attack(objList);}}else{findTarget(objList);//原路线移动if(getY()=3125){setSpd(20);setY(getY()+getSpd());setX(getX()-getSpd());}elseif(getY()>=3750){setSpd(25);setX(getX()-getSpd());}}}}Bullet类packagenewKingOfHonor;importjava.awt.*;//子弹类publicclassBulletextendsGameObject{//发射子弹的游戏元素GameObjectattacker;//目标GameObjecttarget;//攻击力intad;publicBullet(GameFramegameFrame,GameObjectattacker,GameObjecttarget,intad,intspd){super(attacker.getX(),attacker.getY(),gameFrame);this.attacker=attacker;this.target=target;setAd(ad);setSpd(spd);}publicBullet(GameFramegameFrame,GameObjectattacker,GameObjecttarget,intad,intspd,Stringimg){super(attacker.getX(),attacker.getY(),gameFrame);this.attacker=attacker;this.target=target;setImg(img);setAd(ad);setSpd(spd);}publicBullet(){super();}publicvoidmove(){//子弹与目标碰撞,子弹消失,目标减血if(recIntersectsRec(getRec(),target.getRec())){target.setCurrentHp(target.getCurrentHp()-getAd());gameFrame.removeList.add(this);}intdis=(int)getDis(getX(),getY(),target.getX(),target.getY());if(dis!=0){intxSpeed=(getSpd()*(target.getX()-getX())/dis);intySpeed=(getSpd()*(target.getY()-getY())/dis);setX(getX()+xSpeed);setY(getY()+ySpeed);}}@OverridepublicvoidpaintSelf(Graphicsg){g.drawImage(getImg(),getX()-16,getY()-16,null);g.setColor(Color.BLACK);g.fillOval(getX()-5,getY()-5,10,10);g.drawRect(getX()-5,getY()-5,10,10);move();}@OverridepublicRectanglegetRec(){returnnewRectangle(getX()-5,getY()-5,10,10);}publicintgetAd(){returnad;}publicvoidsetAd(intad){this.ad=ad;}}Background类packagenewKingOfHonor;importjava.awt.*;//背景类publicclassBackgroundextendsGameObject{publicBackground(GameFramegameFrame){super(gameFrame);}Imagebg=Toolkit.getDefaultToolkit().getImage("img/王者荣耀图片/Map.jpg");publicvoidpaintSelf(Graphicsg){g.drawImage(bg,0,0,null);}@OverridepublicRectanglegetRec(){returnnull;}}Beast类packagenewKingOfHonor;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Rectangle;importjava.util.ArrayList;publicclassBeastextendsGameObject{publicArrayListbeastList=newArrayList();intwidth;intheight;//复活的元素Beastbeast=null;publicBeast(GameFramegameFrame){super(gameFrame);beastList.add(newRedBuff(3045,3170,gameFrame));beastList.add(newBear(2800,2855,gameFrame));beastList.add(newBird(3570,3380,gameFrame));beastList.add(newLizard(4585,2365,gameFrame));beastList.add(newBlueBuff(4025,2295,gameFrame));beastList.add(newWolf(4235,1945,gameFrame));}publicBeast(intx,inty,GameFramegameFrame){super(x,y,gameFrame);setHp(1000);setCurrentHp(getHp());beast=this;}@OverridepublicvoidpaintSelf(Graphicsg){if(getCurrentHp()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-28 06:10 , Processed in 0.913675 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表