飞机大战JAVA程序设计报告.doc_第1页
飞机大战JAVA程序设计报告.doc_第2页
飞机大战JAVA程序设计报告.doc_第3页
飞机大战JAVA程序设计报告.doc_第4页
飞机大战JAVA程序设计报告.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

中国地质大学长城学院Java 程 序 设 计题目 基于Java的打飞机游戏设计与实现 系 别 信息工程系 专 业 计算机科学与技术 学生姓名 马辉 学 号 041120101 指导教师 田玉龙 2015 年 6 月 18 日基于Java的打飞机游戏设计与实现1、软件运行所需要的软硬件环境本系统是以Windows系统为操作平台,用Java编程语言来实现本系统所需功能的。本机器的配置如下:处理器:AMD A4 或英特尔同级别处理器主频:1.2Hz以上内存:1G以上硬盘:HHD 50G或更高采用的主要技术和软件编程语言:Java开发环境:windows7开发软件:Eclipse 3.72、软件开发环境配置JAVA_HOME = F:JAVAjdkPATH = % JAVA_HOME%bin;%JAVA_HOME%lib;%JAVA_HOME%jrelib;CLASSPATH = %JAVA_HOME%lib;%JAVA_HOME%jrelib;3、软件功能框图重新开始或再次游戏游戏界面 生命值降到04、软件所实现的截图5、主要功能部分的源代码import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Random;import java.util.Vector;import javax.swing.JOptionPane;import javax.swing.Timer;public class Controller extends KeyAdapterpublic static Vector bangs = new Vector();public static Vector ebullets = new Vector();public static Vector pbullets = new Vector();public static Vector eplanes = new Vector();public static PPlane pplane = new PPlane();private GamePanel gamePanel;private Random random = new Random();public static int baoZhaNum;public Controller(Vector bang,Vector ebullet,Vector pbullet,Vector eplane,PPlane pplane,GamePanel gamePanel) super();this.bangs = bang;this.ebullets = ebullet;this.pbullets = pbullet;this.eplanes = eplane; this.pplane = pplane;this.gamePanel = gamePanel;/使用定时器 每隔一秒为每一个敌机 产生一个子弹Timer timer = new Timer(1000, new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubfor(int i=0;i eplanes.size();i+)EBullet ebullet = new EBullet(eplanes.elementAt(i).x, eplanes.elementAt(i).y,8,2);ebullets.add(ebullet););timer.start(); /声明定时器之后 就 开启定时器Overridepublic void keyPressed(KeyEvent e) / TODO Auto-generated method stubswitch (e.getKeyCode()case KeyEvent.VK_UP:PPlane.UP = true;break;case KeyEvent.VK_DOWN:PPlane.DOWN = true;break;case KeyEvent.VK_LEFT:PPlane.LEFT = true;break;case KeyEvent.VK_RIGHT:PPlane.RIGHT = true;break;case KeyEvent.VK_X:PPlane.isFired = true;break;Overridepublic void keyReleased(KeyEvent e) / TODO Auto-generated method stubswitch (e.getKeyCode()case KeyEvent.VK_UP:PPlane.UP = false;break;case KeyEvent.VK_DOWN:PPlane.DOWN = false;break;case KeyEvent.VK_LEFT:PPlane.LEFT = false;break;case KeyEvent.VK_RIGHT:PPlane.RIGHT = false;break;case KeyEvent.VK_X:PPlane.isFired = false;public void StartRun()new Thread()public void run()int count = 0; /通过count控制子弹 避免连续按发送键时 子弹连成线while(true)/本机移动pplane.pplaneMove();/添加本机子弹 if(PPlane.isFired & count%5=0)PBullet pbullet1 = new PBullet(pplane.x+65, pplane.y+50, 8, 15);pbullets.add(pbullet1);PBullet pbullet2 = new PBullet(pplane.x+50, pplane.y+50, 8, 15);pbullets.add(pbullet2);PBullet pbullet3 = new PBullet(pplane.x+35, pplane.y+50, 8, 15);pbullets.add(pbullet3);PBullet pbullet4 = new PBullet(pplane.x+20, pplane.y+50, 8, 15);pbullets.add(pbullet4);count+;/让本机子弹 移动 并判断是否打中敌机for(int i=0;i pbullets.size();i+)pbullets.elementAt(i).bulletMove();int index = pbullets.elementAt(i).isPbulletHitEplane();if(index != -1) /不等于-1 证明打中了 并产生爆炸 Bang bang = new Bang(pbullets.elementAt(i).x,pbullets.elementAt(i).y,30,30);bangs.add(bang);baoZhaNum+;eplanes.remove(index);/判断本机子弹 出界就移除for(int i=0;i pbullets.size();i+)if(pbullets.elementAt(i).y = 0)pbullets.remove(i);/System.out.println(子弹 移除);/添加 敌机if(eplanes.size() Global.ENEMY_NUMBER)int x = random.nextInt(Global.FRAME_WIDTH);int y = -30;EPlane eplane = new EPlane(x, y, 30, 30);eplanes.add(eplane);/让敌机 移动 并且判断出界 for(int i=0;i = Global.FRAME_HEIGHT)eplanes.remove(i);/让 敌机 子弹 移动 并将超过边界的敌机子弹 移除for(int i=0;i = Global.FRAME_HEIGHT)ebullets.remove(i);for(int i=0;i bangs.size();i+)if(bangs.elementAt(i).isBang = true)bangs.remove(i);try sleep(30); catch (InterruptedException e) e.printStackTrace();JudgeLife();gamePanel.display(bangs, ebullets, pbullets, eplanes, pplane);.start();public void JudgeLife()if(!pplane.isAlive()int result = JOptionPane.showConfirmDialog(gamePanel, 继续重玩?,提示,JOptionPane.YES_OPTION);if(result=0)newGame();elseSystem.exit(0);public void newGame()bangs.clear(); /重玩 必须将一切对象都 清空ebullets.clear();pbullets.clear();eplanes.clear();pplane = new PPlane(250, 400, 100, 100);baoZhaNum = 0;pplane.life = 100; /不重置生命值 在进行JudgeLife判断 会一直出现 是否重玩的对话框PPlane.DOWN = false; /重新开始游戏之后 必须重置 所有的静态变量 否则会保存上一次的静态变量值运动和发射子弹PPlane.UP = false;PPlane.LEFT = false;PPlane.RIGHT = false;PPlane.isFired = false;public class PBullet extends Bulletprivate Image img; /保存子弹的图片private JPanel jpanel;public JPanel getJpanel() return jpanel;public void setJpanel(JPanel jpanel) this.jpanel = jpanel;public PBullet(int x, int y, int width, int heigth) super(x, y, width, heigth);img = new ImageIcon(Image/fire.png).getImage();/ TODO Auto-generated constructor stubpublic void bulletMove() / TODO Auto-generated method stubthis.y-=20; /子弹的速度一定要大于 飞机的速度 否则 子弹会出现在飞机后面public void drawMe(Graphics g) / TODO Auto-generated method stubg.drawImage(img, x, y, width, heigth, jpanel);/在 本机子弹判断是否打中敌机public int isPbulletHitEplane()for(int j=0;j Controller.eplanes.size();j+)Rectangle recPbullet = new Rectangle(x,y,width,heigth);Rectangle recEplane = new Rectangle(Controller.eplanes.elementAt(j).x,Controller.eplanes.elementAt(j).y,Controller.eplanes.elementAt(j).w,Controller.eplanes.elementA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论