版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上中国地质大学长城学院Java 程 序 设 计题目 基于Java的打飞机游戏设计与实现 系 别 信息工程系 专 业 计算机科学与技术 学生姓名 马辉 学 号 指导教师 田玉龙 2015 年 6 月 18 日基于Java的打飞机游戏设计与实现1、软件运行所需要的软硬件环境本系统是以Windows系统为操作平台,用Java编程语言来实现本系统所需功能的。本机器的配置如下:处理器:AMD A4 或英特尔同级别处理器主频:1.2Hz以上内存:1G以上硬盘:HHD 50G或更高采用的主要技术和软件编程语言:Java开发环境:windows7开发软件:Eclipse 3.72、软件
2、开发环境配置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.ev
3、ent.KeyEvent;import java.util.Random;import java.util.Vector;import javax.swing.JOptionPane;import javax.swing.Timer;public class Controller extends KeyAdapterpublic static Vector<Bang> bangs = new Vector<Bang>();public static Vector<EBullet> ebullets = new Vector<EBullet>();
4、public static Vector<PBullet> pbullets = new Vector<PBullet>();public static Vector<EPlane> eplanes = new Vector<EPlane>();public static PPlane pplane = new PPlane();private GamePanel gamePanel;private Random random = new Random();public static int baoZhaNum;public Controller
5、(Vector<Bang> bang,Vector<EBullet> ebullet,Vector<PBullet> pbullet,Vector<EPlane> eplane,PPlane pplane,GamePanel gamePanel) super();this.bangs = bang;this.ebullets = ebullet;this.pbullets = pbullet;this.eplanes = eplane; this.pplane = pplane;this.gamePanel = gamePanel;/使用定时器
6、每隔一秒为每一个敌机 产生一个子弹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
7、.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 = t
8、rue;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
9、 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(ppla
10、ne.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.a
11、dd(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);baoZha
12、Num+;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 EPlan
13、e(x, y, 30, 30);eplanes.add(eplane);/让敌机 移动 并且判断出界 for(int i=0;i < eplanes.size();i+)eplanes.elementAt(i).eplaneMove();if(eplanes.elementAt(i).y >= Global.FRAME_HEIGHT)eplanes.remove(i);/让 敌机 子弹 移动 并将超过边界的敌机子弹 移除for(int i=0;i < ebullets.size();i+)ebullets.elementAt(i).bulletMove();if(ebulle
14、ts.elementAt(i).isEBulletHitPPlane()ebullets.elementAt(i).isUsed = true;PPlane.life -= 2;if(ebullets.elementAt(i).y >= 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.prin
15、tStackTrace();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 v
16、oid 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 =
17、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, widt
18、h, 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, jp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 儿童皮肤问题护理案例分析
- 客户拜访提升沟通效果手册
- 勤学善思成就未来-小学主题班会课件
- 勤俭节约从小事做起珍惜每一份资源-小学主题班会课件
- 电商平台用户行为分析与个性化营销策略
- 2026年上半年广西百色工业投资发展集团有限公司社会招聘44人考试模拟试题及答案详解
- 关于2026年品牌联合营销计划执行细节的商洽函5篇范文
- 2026年益阳市资阳区事业单位人员招聘考试备考试题及答案详解
- 2026年烟台市芝罘区事业单位人员招聘笔试参考试题及答案详解
- 2026年云南省丽江市事业单位人员招聘考试备考题库及答案详解
- 2026年6月汉江国有资本投资集团有限公司招聘14人笔试备考题库及答案详解
- 2026中国中医科学院广安门医院招聘合同制人员29人(护理岗位)笔试模拟试题及答案详解
- 2026年云南省中考英语试卷(含答案及解析)
- 2026年甘肃省兰州大学草地农业科技学院聘用制B岗招聘考试参考题库及答案详解
- 昆明市消防救援局政府专职消防员招聘笔试真题2025
- 2026陕西西安交通大学专业技术人员招聘笔试模拟试题及答案解析
- 2025-2026学年湘科版三年级科学下册(全册)课时练习及答案(附目录)
- 抖音营销团队考核制度
- 定向培养军士就业前景分析
- 2026年材料员考试题库含答案【完整版】
- 监理单位全员安全生产责任制
评论
0/150
提交评论