飞机大战-java--源代码.docx

上传人:pei****hi 文档编号:2420025 上传时间:2020-03-22 格式:DOCX 页数:19 大小:20.38KB
返回 下载 相关 举报
飞机大战-java--源代码.docx_第1页
第1页 / 共19页
飞机大战-java--源代码.docx_第2页
第2页 / 共19页
点击查看更多>>
资源描述

《飞机大战-java--源代码.docx》由会员分享,可在线阅读,更多相关《飞机大战-java--源代码.docx(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、package com;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.ImageIO;public class Plane Image feijiImage = null;int x = 300;int y = 700;int lifeCount=5;pu

2、blic Plane() try feijiImage = ImageIO.read(Plane.class.getClassLoader().getResourceAsStream(images/feiji.png); catch (IOException e) e.printStackTrace();public void draw(Graphics g) /画飞机图片g.drawImage(feijiImage, x, y, null);/飞机移动this.move();/ 血条if(lifeCount0)g.setColor(Color.WHITE);g.fillRect(20, 80

3、, 100, 10);g.setColor(Color.red);g.fillRect(20, 80, (100/5)*lifeCount, 10);g.setColor(Color.blue);g.setFont(new Font(幼圆, Font.BOLD, 30);g.drawString(Score:+Play01.count, 20, 60);public void move()if(isUP & !isDown & !isLeft & !isRight)/上y=y-5;else if(!isUP & isDown & !isLeft & !isRight)/下y=y+5;else

4、if(!isUP & !isDown & isLeft & !isRight)/左x=x-5;else if(!isUP & !isDown & !isLeft & isRight)/右x=x+5;else if(isUP & !isDown & isLeft & !isRight)/左上x=x-5;y=y-5;else if(!isUP & isDown & isLeft & !isRight)/左下x=x-5;y=y+5;else if(isUP & !isDown & !isLeft & isRight)/右上x=x+5;y=y-5;else if(!isUP & isDown & !i

5、sLeft & isRight)/右下x=x+5;y=y+5;boolean isUP = false;boolean isDown = false;boolean isLeft = false;boolean isRight = false;/ 摁下public void keyPressed(KeyEvent e) int keyCode = e.getKeyCode();if (keyCode = KeyEvent.VK_RIGHT | keyCode = KeyEvent.VK_D) isRight=true; else if (keyCode = KeyEvent.VK_LEFT |

6、 keyCode = KeyEvent.VK_A) isLeft=true; else if (keyCode = KeyEvent.VK_UP | keyCode = KeyEvent.VK_W) isUP=true; else if (keyCode = KeyEvent.VK_DOWN | keyCode = KeyEvent.VK_S) isDown=true;/ 放开public void keyReleased(KeyEvent e) int keyCode = e.getKeyCode();if (keyCode = KeyEvent.VK_RIGHT | keyCode = K

7、eyEvent.VK_D) isRight=false; else if (keyCode = KeyEvent.VK_LEFT | keyCode = KeyEvent.VK_A) isLeft=false; else if (keyCode = KeyEvent.VK_UP | keyCode = KeyEvent.VK_W) isUP=false; else if (keyCode = KeyEvent.VK_DOWN | keyCode = KeyEvent.VK_S) isDown=false;public Rectangle getRectangle()return new Rec

8、tangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null); package com;public class PlaneStatus public static int roleNum = 1;public static int playStatus = 0;/ 0 游戏开始前 1 第一关 -1 游戏结束package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.event.KeyEv

9、ent;import java.io.IOException;import javax.imageio.ImageIO;public class Power Image PowerImage = null;int x = 20;int y = 0;public Power() this.x=(int)(Math.random()*540)+20;try PowerImage = ImageIO.read(Gift.class.getClassLoader().getResourceAsStream(images/power.png); catch (IOException e) e.print

10、StackTrace();public void draw(Graphics g) /画血瓶图片g.drawImage(PowerImage, x, y, null);/血瓶移动y+;public Rectangle getRectangle()return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null); package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.a

11、wt.event.KeyEvent;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;/ 第一关public class Play01 static int count=0;Image bgImage = null;/ 战机Plane plane = new Plane();/ 战机子弹List mzds = new ArrayList();/ 敌机List dijis = new ArrayList();/ 敌机子弹/List dij

12、izidans = new ArrayList();/ 血瓶List gifts = new ArrayList();List powers = new ArrayList();public Play01() try bgImage = ImageIO.read(Play01.class.getClassLoader().getResourceAsStream(images/bg_01.jpg); catch (IOException e) e.printStackTrace();int bgY1 = 0;int bgY2 = -600;int fireTime = 0;boolean fla

13、g=false;public void draw(Graphics g) / 画背景图片g.drawImage(bgImage, 0, bgY1, null);bgY1 += 5;if (bgY1 = 0) bgY2 = -600;/ 清理战机子弹for (int i = 0; i 800) mzds.remove(i);/ 添加子弹if (isFire1 = true & flag=false) if (fireTime % 8 = 0) mzds.add(new MyZiDan(plane.x + 25, plane.y + 0);fireTime+;if (isFire1= true &

14、 flag=true) if (fireTime % 4 = 0) mzds.add(new MyZiDan(plane.x +8, plane.y +0);mzds.add(new MyZiDan(plane.x +52, plane.y +0);fireTime+;/ 画战机子弹for (int i = 0; i mzds.size(); i+) MyZiDan myZidan = mzds.get(i);myZidan.draw(g);/ 清理敌机for (int i = 0; i dijis.size(); i+) Diji dj = dijis.get(i);if (dj.x -10

15、0) dijis.remove(i);/ 抽奖:添加敌机if (int) (Math.random() * 20) = 5) dijis.add(new Diji();/ 抽奖:添加血瓶if (int) (Math.random() * 1000) = 5) gifts.add(new Gift();for (int i = 0; i gifts.size(); i+) Gift gift = gifts.get(i);gift.draw(g);/ 抽奖:添加powerif (int) (Math.random() * 1000) = 5) powers.add(new Power();for

16、 (int i = 0; i powers.size(); i+) Power power = powers.get(i);power.draw(g);/ 画敌机for (int i = 0; i dijis.size(); i+) Diji dj = dijis.get(i);dj.draw(g);/ 画飞机plane.draw(g);/ 判断战机相撞for (int i = 0; i 0)flag=false; if(plane.lifeCount1) PlaneStatus.playStatus=-1; / 判断战机和血瓶相撞for (int i = 0; i gifts.size();

17、 i+) / 先得到每一个血瓶Gift gift = gifts.get(i);Rectangle r1 = gift.getRectangle();Rectangle r2 = plane.getRectangle();if (r1.intersects(r2) gifts.remove(i);if (plane.lifeCount 5) plane.lifeCount = plane.lifeCount + 1;/ 判断战机和Power相撞for (int i = 0; i powers.size(); i+) / 先得到每一个powerPower power = powers.get(i

18、);Rectangle r1 = power.getRectangle();Rectangle r2 = plane.getRectangle();if (r1.intersects(r2) powers.remove(i);flag=true;/ 判断敌机在中弹for (int i = 0; i mzds.size(); i+) / 得到每个战机子弹MyZiDan myzidan = mzds.get(i);Rectangle r1 = myzidan.getRectangle();for (int j = 0; j dijis.size(); j+) / 每一个敌机Diji diji =

19、dijis.get(j);Rectangle r2 = diji.getRectangle();if (r1.intersects(r2) mzds.remove(i);dijis.remove(j);count+;boolean isFire1 = false;boolean isFire2 = false;public void keyPressed(KeyEvent e) plane.keyPressed(e);if (e.getKeyCode() = KeyEvent.VK_J) isFire1 = true;/*if (e.getKeyCode() = KeyEvent.VK_K)

20、isFire2 = true;*/public void keyReleased(KeyEvent e) plane.keyReleased(e);if (e.getKeyCode() = KeyEvent.VK_J) isFire1 = false;fireTime = 0;/*if (e.getKeyCode() = KeyEvent.VK_K) isFire2 = false;fireTime = 0;*/if( e.getKeyCode()=KeyEvent.VK_P)for(int i=0;i KeyAdapterthis.addKeyListener(new KeyAdapter(

21、)Overridepublic void keyPressed(KeyEvent e) if(PlaneStatus.playStatus = 0)before.keyPressed(e);else if(PlaneStatus.playStatus =1)play01.keyPressed(e);else if(PlaneStatus.playStatus=-1)over.keyPressed(e);Overridepublic void keyReleased(KeyEvent e) if(PlaneStatus.playStatus = 0)before.keyReleased(e);e

22、lse if(PlaneStatus.playStatus =1)play01.keyReleased(e);else if(PlaneStatus.playStatus=-1)over.keyReleased(e););/启动一个线程:每隔20 毫秒执行一次new Thread()public void run()while(true)MainFrame.this.repaint();try Thread.sleep(20); catch (InterruptedException e) e.printStackTrace();.start();/显示this.setVisible(true

23、);Image bufferImage =null;/这个方法也是20毫秒执行一次public void paint(Graphics g)if(bufferImage=null)bufferImage = this.createImage(600, 800);Graphics g4Image = bufferImage.getGraphics();g4Image.setColor(Color.BLACK);g4Image.fillRect(0, 0, 600, 800);this.draw(g4Image);g.drawImage(bufferImage, 0, 0, null);publi

24、c void draw(Graphics g)if(PlaneStatus.playStatus = 0)before.draw(g);else if(PlaneStatus.playStatus =1)play01.draw(g);else if(PlaneStatus.playStatus=-1)over.draw(g);public static void main(String args) new MainFrame();package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangl

25、e;import java.io.IOException;import javax.imageio.ImageIO;public class MyZiDan Image zidanImage = null;int x ;int y ;public MyZiDan(int x,int y) this.x=x;this.y=y;try zidanImage = ImageIO.read(MyZiDan.class.getClassLoader().getResourceAsStream(images/zidan.png); catch (IOException e) e.printStackTra

26、ce();public void draw(Graphics g) /画我军子弹图片g.drawImage(zidanImage, x, y, null);/我的飞机子弹移动速度y-=20;public Rectangle getRectangle()return new Rectangle(x,y,zidanImage.getWidth(null),zidanImage.getHeight(null); package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import jav

27、a.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.ImageIO;public class Diji Image dijiImage = null;int y = -20;int x = 10;/20 760int r;public Diji() this.x=(int)(Math.random()*540)+40;try r = (int)(Math.random()*3)+1;dijiImage = ImageIO.read(Diji.class.getClassLoader().getResource

28、AsStream(images/diji_+r+.png); catch (IOException e) e.printStackTrace();public void draw(Graphics g) /画敌机图片g.drawImage(dijiImage, x, y, null);/敌机移动if(r=1)y+=4;else if(r=2)y+=4;else if(r=3)y+=7;public Rectangle getRectangle()return new Rectangle(x,y,dijiImage.getWidth(null),dijiImage.getHeight(null)

29、; package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.ImageIO;public class Gift Image GiftImage = null;int x = 20;int y = 0;public Gift() this.x=(int)(Math.random()*540)+20;try GiftImage =

30、 ImageIO.read(Gift.class.getClassLoader().getResourceAsStream(images/blood.png); catch (IOException e) e.printStackTrace();public void draw(Graphics g) /画血瓶图片g.drawImage(GiftImage, x, y, null);/血瓶移动y+;public Rectangle getRectangle()return new Rectangle(x,y,GiftImage.getWidth(null),GiftImage.getHeigh

31、t(null); package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.io.IOException;import javax.imageio.ImageIO;public class DijiZiDan Image zidanImage = null;int x ;int y ;int fangxiang =0;public DijiZiDan(int x,int y) this.x=x;this.y=y;fangxiang = (int)(Math.r

32、andom()*5);/0 7try zidanImage = ImageIO.read(DijiZiDan.class.getClassLoader().getResourceAsStream(images/dijizidan.jpg); catch (IOException e) e.printStackTrace();public void draw(Graphics g) /画敌机子弹图片g.drawImage(zidanImage, x, y, null);/移动this.move();public void move()if(fangxiang=0)/下y=y+2;else if(

33、fangxiang=1)/左x=x-2;else if(fangxiang=2)/右x=x+2;else if(fangxiang=3)/左下x=x-2;y=y+2;else if(fangxiang=4)/右下x=x+2;y=y+2;public Rectangle getRectangle()return new Rectangle(x,y,zidanImage.getWidth(null),zidanImage.getHeight(null); package com;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.Imag

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 其他报告

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁