Java如何实现经典拳皇误闯冒险岛游戏
短信预约 -IT技能 免费直播动态提醒
这篇文章主要为大家展示了“Java如何实现经典拳皇误闯冒险岛游戏”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java如何实现经典拳皇误闯冒险岛游戏”这篇文章吧。
主要设计
游戏面板生成显示
背景选用冒险岛素材图
设计英雄,包含生命值,法术值,英雄的动作变化处理,英雄的技能特效
设计怪兽,包含怪物血量,攻击力,位置,步长等
键盘监听:
按下空格:跳
按A键或左方向键<- :向左
按D键或右方向键-> :向右
按S键或下方向键 +K键:释放技能
按J键:攻击
功能截图
游戏开始
拳皇攻击
拳皇放技能
代码实现
游戏主界面
public class MainFrame extends JFrame {private static final long serialVersionUID = 7492509868810783126L;private static KeyWork key;//双缓冲方法解决闪烁问题Image ImageBuffer=null;Graphics img;public static int sri=0,sli=0,jri=0,jli=0,wli=0,wri=0,ari=0,ali=0,s0i=0;int bgindex=0;public static List<Monster> monsters=new ArrayList<Monster>();public MainFrame() {this.setTitle("拳皇误闯冒险岛");this.setBounds((int)(GameValue.WIDTH-GameValue.Width)/2,(int)(GameValue.HEIGHT-GameValue.Hight)/2,GameValue.Width,GameValue.Hight);this.setLayout(null);this.setResizable(false);key=new KeyWork();this.addKeyListener(key);this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubisclose();}});initMonster();new ActionThread();}private void initMonster() {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile(true) {try {Monster m=new Monster();monsters.add(m);Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}).start();}public void isclose() {System.exit(0);int choose=JOptionPane.showConfirmDialog(this,"是否退出?","退出",JOptionPane.YES_NO_OPTION);if(choose==0){System.exit(0);}elsesetDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);}public void display(boolean show) {this.setVisible(show);}@Overridepublic void paint(Graphics g) {if(ImageBuffer==null){ImageBuffer=createImage(this.getWidth(),this.getHeight());}img=ImageBuffer.getGraphics();super.paint(img);img.drawImage(GameValue.bgimgs[bgindex],0,0,GameValue.Width,GameValue.Hight,this);img.setColor(Color.WHITE);img.setFont(GameValue.display_font);int x0=30,y0=70,yh=20+8;img.drawString("生命值", x0,y0);img.drawString("法术值", x0,y0+yh);img.drawRect(x0*3+8-1, y0-15-1, 150+1, 15+1);img.drawRect(x0*3+8-1, y0+yh-15-1, 150+1, 15+1);img.setColor(Color.GREEN);int hp_width=(int)(((double)(Hero.HP_NOW)/Hero.HP_ALL)*150);img.fillRect(x0*3+8, y0-15, hp_width, 15);img.setColor(Color.BLUE);int mp_width=(int)(((double)(Hero.MP_NOW)/Hero.MP_ALL)*150);img.fillRect(x0*3+8, y0+yh-15, mp_width, 15);img.setColor(Color.WHITE);img.drawString(Hero.HP_NOW+"/"+Hero.HP_ALL,x0*3+8-1+45, y0);img.drawString(Hero.MP_NOW+"/"+Hero.MP_ALL,x0*3+8-1+45, y0+yh);for(int i=0;i<monsters.size();i++) {Monster m=monsters.get(i);if(m.islive) {if(m.left) {img.drawImage(m.imgs_left[m.index],m.mX,m.Y,70,70,this);}else if(m.right) {img.drawImage(m.imgs_right[m.index],m.mX,m.Y,70,70,this);}}}if(Hero.ishurted) {}else if(Hero.isfall) {}else {if(Hero.left && !Hero.jump && !Hero.walk && !Hero.attack && !Hero.skill0_attack) {img.drawImage(Hero.stand_left[sli],Hero.X,Hero.Y,this);}if(Hero.right && !Hero.jump && !Hero.walk && !Hero.attack && !Hero.skill0_attack) {img.drawImage(Hero.stand_right[sri],Hero.X,Hero.Y,this);}if(Hero.jump) {if(Hero.attack) {if(Hero.left) {img.drawImage(Hero.jump_attack_left[jli],Hero.X,Hero.jumpY,this);}if(Hero.right) {img.drawImage(Hero.jump_attack_right[jri],Hero.X,Hero.jumpY,this);}}else {if(Hero.left) {img.drawImage(Hero.jump_left[jli],Hero.X-7,Hero.jumpY,this);}if(Hero.right) {img.drawImage(Hero.jump_right[jri],Hero.X+7,Hero.jumpY,this);}}}else {if(Hero.attack) {if(Hero.left) {img.drawImage(Hero.attack_left[ali],Hero.X-50,Hero.Y,this);}if(Hero.right) {img.drawImage(Hero.attack_right[ari],Hero.X+50,Hero.Y,this);}}else {if(Hero.walk) {if(Hero.left) {img.drawImage(Hero.walk_left[wli],Hero.X,Hero.Y,this);}if(Hero.right) {img.drawImage(Hero.walk_right[wri],Hero.X,Hero.Y,this);}}else {if(Hero.skill0_attack) {if(Hero.left) {img.drawImage(Hero.skill0_attack_left[s0i],Hero.X-40,Hero.Y-75,this);}if(Hero.right) {img.drawImage(Hero.skill0_attack_right[s0i],Hero.X-10,Hero.Y-80,this);}}}}}}img.dispose();g.drawImage(ImageBuffer,0,0,this);//super.paint(g);}class ActionThread implements Runnable{public ActionThread() {// TODO Auto-generated constructor stubnew Thread(this).start();}@Overridepublic void run() {// TODO Auto-generated method stubwhile(Hero.HP_NOW>0) {if(!Hero.ishurted) {if(!Hero.jump) {if(Hero.attack) {for(int i=0;i<Hero.attack_right.length;i++) {if(Hero.left) {ali=i;}else if(Hero.right) {ari=i;}repaintUI(80);}ali=0;ari=0;Hero.attack=false;}else {if(Hero.walk) {for(int i=0;i<Hero.walk_right.length;i++) {if(!Hero.walk || Hero.jump) {break;}if(Hero.left) {wli=i;Hero.X-=Hero.step;if(Hero.X<=0) {if(bgindex!=0) {bgindex--;Hero.X=GameValue.Width-105;AllClear();repaint();}else{Hero.X=0;}}}else if(Hero.right) {wri=i;Hero.X+=Hero.step;if(Hero.X>=(GameValue.Width-80)) {if(bgindex!=GameValue.bgimgs.length-1) {bgindex++;Hero.X=0;AllClear();repaint();}else{Hero.X=GameValue.Width-80;}}}repaintUI(100);}}else {if(Hero.skill0_attack) {if(Hero.MP_NOW>=Hero.skill0_mp) {Hero.MP_NOW-=Hero.skill0_mp;for(int i=0;i<Hero.skill0_attack_right.length;i++) {s0i=i;repaintUI(80);}}else if(Hero.MP_NOW<=0) {Hero.MP_NOW=0;repaint();}s0i=0;Hero.skill0_attack=false;}else {for(int i=0;i<Hero.stand_right.length;i++) {if(Hero.walk || Hero.jump) {break;}if(Hero.left) {sli=i;}else if(Hero.right) {sri=i;}if(Hero.X>=(GameValue.Width-101)) {Hero.X=GameValue.Width-101;}repaintUI(100);}}}}}else if(Hero.jump) {for(int i=0;i<Hero.jump_right.length;i++) {if(Hero.left) {jli=i;}else if(Hero.right) {jri=i;}if(KeyWork.pressA) {Hero.X-=Hero.step+5;if(Hero.X<=0) {Hero.X=0;}}else if(KeyWork.pressD) {Hero.X+=Hero.step+5;if(Hero.X>=(GameValue.Width-103)) {Hero.X=GameValue.Width-103;}}if(i>=2 && i<=7) {Hero.jumpY-=12;}if(i>=9 && i<=14) {Hero.jumpY+=12;}repaintUI(80);}jli=0;jri=0;Hero.jump=false;Hero.attack=false;repaint();}//System.out.print("");}else {try {Thread.sleep(200);Hero.ishurted=false;} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}public void repaintUI(int times) {this.repaint();try {Thread.sleep(times);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void AllClear() {for(Monster m:monsters) {m.islive=false;}monsters.clear();}}
英雄
public class Hero {public static int HP_NOW=100,HP_ALL=100,MP_NOW=120,MP_ALL=120;public static int X=50,Y=400,jumpY=Y-52;public static boolean ishurted=false,isfall=false,left=false,right=true,jump=false,walk=false,attack=false,skill0_attack=false;public static int attack_hp=10,jump_attack_hp=15,skill0_mp=15,skill0_hp=20;public static int step=15;public static Image[] stand_left= {Toolkit.getDefaultToolkit().getImage("img/role/stand-left/stand01.png"),Toolkit.getDefaultToolkit().getImage("img/role/stand-left/stand02.png"),Toolkit.getDefaultToolkit().getImage("img/role/stand-left/stand03.png")};public static Image[] jump_left= {Toolkit.getDefaultToolkit().getImage("img/role/jump-left/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-left/19.png")};public static Image[] walk_left= {Toolkit.getDefaultToolkit().getImage("img/role/walk-left/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-left/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-left/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-left/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-left/4.png")};public static Image[] attack_left= {Toolkit.getDefaultToolkit().getImage("img/role/attack-left/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-left/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-left/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-left/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-left/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-left/5.png")};public static Image[] jump_attack_left= {Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-left/19.png")};public static Image[] skill0_attack_left= {Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/19.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/left/20.png")};public static Image[] stand_right= {Toolkit.getDefaultToolkit().getImage("img/role/stand-right/stand01.png"),Toolkit.getDefaultToolkit().getImage("img/role/stand-right/stand02.png"),Toolkit.getDefaultToolkit().getImage("img/role/stand-right/stand03.png")};public static Image[] jump_right= {Toolkit.getDefaultToolkit().getImage("img/role/jump-right/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-right/19.png")};public static Image[] walk_right= {Toolkit.getDefaultToolkit().getImage("img/role/walk-right/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-right/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-right/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-right/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/walk-right/4.png")};public static Image[] attack_right= {Toolkit.getDefaultToolkit().getImage("img/role/attack-right/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-right/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-right/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-right/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-right/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/attack-right/5.png")};public static Image[] jump_attack_right= {Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/jump-attack-right/19.png")};public static Image[] skill0_attack_right= {Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/0.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/1.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/2.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/3.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/4.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/5.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/6.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/7.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/8.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/9.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/10.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/11.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/12.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/13.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/14.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/15.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/16.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/17.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/18.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/19.png"),Toolkit.getDefaultToolkit().getImage("img/role/skills/0/right/20.png")};public static void attacked(int atk) {ishurted=true;HP_NOW-=atk;if(HP_NOW<0) {HP_NOW=0;FightGameLaunch.getMainFrame().repaint();int choose=JOptionPane.showConfirmDialog(null,"游戏结束,是否退出?","退出",JOptionPane.YES_NO_OPTION);if(choose==0){System.exit(0);}elseSystem.out.println("重开");}}}
以上是“Java如何实现经典拳皇误闯冒险岛游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341