• 4.40 MB
  • 21页

基于java雷电游戏项目报告毕业论文

  • 21页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'《Java程序设计》实验报告年级、专业、班级2011级网络1班2011级网络1班2011级网络1班姓名实验题目编程实现雷电游戏实验时间2013.10.22-2013.11.6实验地点主教0410实验成绩实验性质□验证性□设计性■综合性教师评价:□算法/实验过程正确;□源程序/实验内容提交□程序结构/实验步骤合理;□实验结果正确;□语法、语义正确;□报告规范;其他:评价教师签名:一、实验目的利用JavaGUI、JavaGraphics和多线程技术,编写实现飞行射击游戏——雷电。二、实验项目内容1、根据项目任务书,形成完整的需求分析。2、根据需求,完成详细的设计。3、基于Java编程实现。三、实验过程或算法(源程序)关键代码:Boss.javapackagecqu;publicclassBossextendsFlyingObject{publicBoss(intx){super(x,0,250,100,Resources.bossPNG);//TODOAuto-generatedconstructorstub} @Overridepublicvoidmove(longtime){//TODO自动生成的方法存根}publicBulletfire(){intx=(int)(Math.random()*350);inty=100;Bulletb=newBullet(x,y,true);returnb;}}Bullet.javapackagecqu;publicclassBulletextendsFlyingObject{publicintxSpeed;publicBullet(intx,inty,booleanenemy){super(x,y,10,10,Resources.bulletPNG);this.enemy=enemy;xSpeed=0;}privatebooleanenemy;publicbooleanisEnemy(){returnenemy;}@Overridepublicvoidmove(longtime){if(time%40==0){if(enemy){y+=30;}else{y-=30;x+=xSpeed;}}}}CBullet.javapackagecqu; publicclassCBulletextendsFlyingObject{publicCBullet(intx){super(x,0,30,30,Resources.addPNG);}@Overridepublicvoidmove(longtime){if(time%160==0){y+=Math.random()*4+10;//x+=4-Math.random()*6;}}}Enemy.javapublicclassEnemyextendsFlyingObject{publicEnemy(intx,inty){super(x,y,40,40,Resources.enemyPNG);}publicBulletfire(){intx=this.x+12;inty=this.y+this.height;Bulletb=newBullet(x,y,true);returnb;}@Overridepublicvoidmove(longtime){if(time%160==0){y+=Math.random()*4+10;//x+=4-Math.random()*6;}}}FlyObject.javapackagecqu;importjava.awt.Graphics;importjava.awt.image.BufferedImage;abstractpublicclassFlyingObject{protectedintx;protectedinty;protectedintwidth; protectedintheight;privateBufferedImageimage;publicFlyingObject(intx,inty,intwidth,intheight,BufferedImageimage){super();this.x=x;this.y=y;this.width=width;this.height=height;this.image=image;}abstractpublicvoidmove(longtime);publicvoiddraw(Graphicsg){g.drawImage(image,x,y,width,height,null);}publicintgetWidth(){returnwidth;}publicvoidsetWidth(intwidth){this.width=width;}publicintgetHeight(){returnheight;}publicvoidsetHeight(intheight){this.height=height;}/*publicbooleancollision(FlyingObjectother){returnfalse;}*/publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}}GameCanvas.javapackagecqu;importjava.awt.Graphics;importjava.awt.event.KeyEvent; importjava.awt.event.KeyListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.util.ArrayList;importjava.util.List;importjavax.swing.JPanel;publicclassGameCanvasextendsJPanelimplementsKeyListener{privatebooleanstart1=false;privatebooleanstart2=true;Threadanimate;CBulletcbullet=newCBullet((int)(Math.random()*350));intcflag=0;intisdeath=1;intend=0;intsflag=0;inty=0;intcount=0;booleanclick=false;privateListenemies=newArrayList();privateListebullets=newArrayList();privateListhbullets=newArrayList();privateListblast=newArrayList();privateHerohero;privateBossboss;publicGameCanvas(){this.addKeyListener(this);hero=newHero(200,500);boss=newBoss((int)(Math.random()*150));Enemye=newEnemy((int)(Math.random()*350),0);enemies.add(e);animate=newThread(){//intisdeath=1;intscale=1;//intcflag=0;publicvoidrun(){longtime=0;while(true){try{if(isdeath==1){time=0;click=false;ebullets.clear();hbullets.clear();enemies.clear();} elseif(isdeath==2){time=0;click=true;ebullets.clear();hbullets.clear();enemies.clear();}else//if(isdeath==0){EnemyMove(time);for(Bullete:ebullets){e.move(time);}for(Bulleth:hbullets){h.move(time);}if(scale==1){if(time%1000==0){for(Enemye:enemies){Bulletb=e.fire();ebullets.add(b);}}}else{if(time%800==0){for(Enemye:enemies){Bulletb=e.fire();ebullets.add(b);}if(end==1){for(inti=0;i<6;i++){Bulletbu=boss.fire();ebullets.add(bu);}}}}addNewEnemy(time); keymove();collision();getaway();System.out.println(time);if(y++>=600)y=0;time+=40;Thread.sleep(50);}}catch(InterruptedExceptione){e.printStackTrace();}repaint();blast.clear();}}privatevoidgetaway(){for(inti=0;i600)enemies.remove(i);}for(inti=0;i600)ebullets.remove(i);for(inti=0;i600)cflag=0;}privatevoidkeymove(){if(hero.up)hero.moveUp(8);if(hero.down)hero.moveDown(8);if(hero.left)hero.moveLeft(8);if(hero.right)hero.moveRight(8);}privatevoidcollision(){intesize=enemies.size();//inthsize=hbullets.size();for(inti=0;i=1)for(intj=0;jboss.getX()&&hbullets.get(i).getX()0&&hbullets.get(i).getY()<100){count++;hbullets.remove(i);i--;}}if(count>=300)isdeath=2;}/*for(inti=0;ihero.getX())if(hero.getX()+25>ebullets.get(i).getX())if(ebullets.get(i).getY()>hero.getY())if(hero.getY()+25>ebullets.get(i).getY()){if(sflag==0)isdeath=1;elsesflag=0;Blasta=newBlast();a.set(hero.getX(),hero.getY());blast.add(a);ebullets.remove(i);break;}*/if(cbullet.getX()hero.getX())if(cbullet.getY()hero.getX()+25)if(cbullet.getY()=50000&&time<){scale=2;if(time%==0){cflag=1;cbullet=newCBullet((int)(Math.random()*350));}if(time%5000==0)for(inti=0;i<3;i++){Enemye=newEnemy((int)(Math.random()*350),0);enemies.add(e);}}elseif(time>=){if(end==0)if(time%==0){cflag=1;cbullet=newCBullet((int)(Math.random()*350));}if(time%5000==0)for(inti=0;i<6;i++){Enemye=newEnemy((int)(Math.random()*350),0);enemies.add(e);}if(time>){if(time%10000==0){cflag=1;cbullet=newCBullet((int)(Math.random()*350));}end=1;}} }privatevoidEnemyMove(longtime){for(Enemye:enemies){e.move(time);}if(cflag==1)cbullet.move(time);}};this.addMouseListener(newMouseListener(){@OverridepublicvoidmouseReleased(MouseEvente){//TODO自动生成的方法存根}@OverridepublicvoidmousePressed(MouseEvente){//TODO自动生成的方法存根}@OverridepublicvoidmouseExited(MouseEvente){//TODO自动生成的方法存根}@OverridepublicvoidmouseEntered(MouseEvente){//TODO自动生成的方法存根}@OverridepublicvoidmouseClicked(MouseEvente){//TODO自动生成的方法存根//System.out.println(e.getX()+""+e.getY());if(!click){if(e.getX()>47&&e.getX()<315&&e.getY()>237&&e.getY()<315){start1=true;start2=false;isdeath=0;animate.start();click=true; }}else;}});}publicvoidpaint(Graphicsg){super.paint(g);if(start2)g.drawImage(Resources.startPNG,0,0,400,600,null);if(start1){g.drawImage(Resources.backPNG,0,y,400,600,null);g.drawImage(Resources.backPNG,0,y-600,400,600,null);if(isdeath==0){for(Enemye:enemies)e.draw(g);for(Bullete:ebullets)e.draw(g);for(Bulleth:hbullets)h.draw(g);for(Blastb:blast)b.draw(g);hero.draw(g);if(end==1)boss.draw(g);if(cflag==1)cbullet.draw(g);}elseif(isdeath==1)g.drawImage(Resources.endPNG,0,0,400,600,null);elseg.drawImage(Resources.passPNG,0,0,400,600,null);}}@OverridepublicvoidkeyTyped(KeyEvente){}@OverridepublicvoidkeyPressed(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP://hero.moveUp(2); hero.up=true;break;caseKeyEvent.VK_DOWN://hero.moveDown(2);hero.down=true;break;caseKeyEvent.VK_LEFT://hero.moveLeft(4);hero.left=true;break;caseKeyEvent.VK_RIGHT://hero.moveRight(4);hero.right=true;break;caseKeyEvent.VK_SPACE:if(sflag==1){Bulletb=hero.fire();Bulletb1=hero.fire();Bulletb2=hero.fire();b.x=hero.x+13;b.y=hero.y;b.xSpeed=15;hbullets.add(b);b1.x=hero.x+13;b1.y=hero.y;b1.xSpeed=-15;hbullets.add(b1);b2.x=hero.x+13;b2.y=hero.y;b2.xSpeed=0;hbullets.add(b2);break;}else{Bulletb=hero.fire();b.x=hero.x+13;b.y=hero.y;b.xSpeed=0;hbullets.add(b);}}}@OverridepublicvoidkeyReleased(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP: hero.up=false;break;caseKeyEvent.VK_DOWN:hero.down=false;break;caseKeyEvent.VK_LEFT:hero.left=false;break;caseKeyEvent.VK_RIGHT:hero.right=false;break;}}}GameFrame.javapackagecqu;importjava.awt.BorderLayout;importjavax.swing.JFrame;publicclassGameFrameextendsJFrame{privateGameCanvascanvas=newGameCanvas();publicGameFrame(){canvas.setFocusable(true);canvas.requestFocus();this.add(canvas,BorderLayout.CENTER);}/***@paramargs*/publicstaticvoidmain(String[]args){GameFrameframe=newGameFrame();frame.setSize(400,600);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}Hero.javapackagecqu;publicclassHeroextendsFlyingObject{publicHero(intx,inty){super(x,y,40,40,Resources.heroPNG);//TODOAuto-generatedconstructorstub} publicbooleanup=false;publicbooleandown=false;publicbooleanleft=false;publicbooleanright=false;publicBulletfire(){Bulletb=newBullet(this.x,this.y-this.height,false);returnb;}@Overridepublicvoidmove(longtime){}publicvoidmoveUp(intd){if(y>0)y-=d;elsey=0;}publicvoidmoveDown(intd){if(y<560)y+=d;elsey=560;}publicvoidmoveLeft(intd){if(x-d>0)x-=d;elsex=0;}publicvoidmoveRight(intd){if(x+d<360)x+=d;}}Resources.javapackagecqu;importjava.awt.image.BufferedImage;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassResources{static{try{enemyPNG=ImageIO.read(Resources.class.getResourceAsStream("/enemy.png")); bulletPNG=ImageIO.read(Resources.class.getResourceAsStream("/bullet.png"));heroPNG=ImageIO.read(Resources.class.getResourceAsStream("/hero.png"));blastPNG=ImageIO.read(Resources.class.getResourceAsStream("/blast.png"));startPNG=ImageIO.read(Resources.class.getResourceAsStream("/start.png"));backPNG=ImageIO.read(Resources.class.getResourceAsStream("/back.png"));addPNG=ImageIO.read(Resources.class.getResourceAsStream("/add.png"));endPNG=ImageIO.read(Resources.class.getResourceAsStream("/end.png"));bossPNG=ImageIO.read(Resources.class.getResourceAsStream("/boss.png"));passPNG=ImageIO.read(Resources.class.getResourceAsStream("/pass.png"));}catch(IOExceptione){e.printStackTrace();}}publicstaticBufferedImageenemyPNG;publicstaticBufferedImagebulletPNG;publicstaticBufferedImageheroPNG;publicstaticBufferedImageblastPNG;publicstaticBufferedImagestartPNG;publicstaticBufferedImagebackPNG;publicstaticBufferedImageaddPNG;publicstaticBufferedImageendPNG;publicstaticBufferedImagebossPNG;publicstaticBufferedImagepassPNG;}四、实验结果及分析和(或)源程序调试过程这次的java项目是编写一个飞机射击游戏--雷电..经过2星期的努力我们完成了该项目。下面是我们的实验结果分析:1,运行后的主界面 2,点击界面上的“rarden”开始游戏。游戏开始的界面:从上方飞出的为敌机,它们会发射子弹。下方的为己方的飞机。被子弹击中后则游戏失败。 3.通过键盘上的4个“”键来操控己方飞机的上下左右移动。4,按住空格键即可发射子弹,击中敌机则敌机将被击毁。 5.控制台里会显示计时。6.“”时会出现奖励。可移动飞机至奖励路径来得到奖励 7,获得奖励后,子弹会升级同时发射3个不同方向的子弹和增加一条性命。8,“”时会出现boss,boss需击中300次才会死亡‘11,击杀boss后成功赢得游戏! '