Java实现飞机大战(有图片素材) 您所在的位置:网站首页 飞机大战游戏背景是沙漠的 Java实现飞机大战(有图片素材)

Java实现飞机大战(有图片素材)

2024-06-26 18:19| 来源: 网络整理| 查看: 265

http://【【尚学堂】Java飞机大战3.0重制版,1小时快速上手掌握,源码_课件_资料,基础小白必学项目,毕设练手项目经验_Java项目_java入门_游戏开发】https://www.bilibili.com/video/BV1aX4y167Y3?vd_source=fc0c1b2d853ba00c1f9a210df63c3e86 代码参考了尚学堂飞机大战3.0版本,同时在此基础上添加了自己的一些想法,游戏失败或者通关后游戏重新开始,添加背景音乐。其他的没做太大变动。代码主要是碰撞有点多,容易漏写,其他的还行。

目录

思维导图

游戏解析:

 一,主窗口类

 二,工具类

 三, 所有游戏元素的父类

 四,背景类

 五,Boss子弹类

 六, BossObj类

 七, 我方飞机二级子弹类

 八,敌机1类

 九,敌机2子弹类

  十, 敌机2类

 十一,爆炸类

 十二,  道具类

 十三,LittleBoss1类 

 十四, LittleBoss1子弹类

  十五,LittleBoss2类

 十六, LittleBoss2子弹类

 十七,我方飞机类 

 十八,我方飞机子弹类 

 十九,我方飞机三级子弹类 

 二十, BOSS警示类

 游戏效果显示

 图片资源

思维导图

游戏思维导图写的非常非常详细,图片有点放不下。可以看一下,对做游戏有很大帮助

 

游戏解析:

##我方子弹有三种形态,一级子弹shell,二级子弹doubleshell,三级子弹tripleshell。

##敌方有5种形态,敌机1enemy1,敌机2enemy2,  littleboss1,littleboss2,最终大boss。

##敌机2,littleboss1,littleboss2,boss可以发射子弹,但是boss2发射的是追踪导弹。

##我方可以击毁littleboss2发射的导弹。

##击败littleboss1,littleboss2后会产生道具,吃到道具后会我方飞机会改变子弹形态。

##最终大boss出现前会有一个警示出现。

##击败敌方时,会产生爆炸图

##点击游戏,音乐开始;关闭游戏,音乐结束。

 我的程序在IEDA就是这样,类没有分的很细,但是也做到了见名知意

 一,主窗口类 import javax.sound.sampled.*; import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.util.Scanner; public class GameWin extends JFrame { //记录游戏状态的变量 //0未开始 1游戏中,2暂停,3失败,4通关 public static int state = 0; //背景图对象 BgObj bgObj = new BgObj(GameUtils.bgImg, 0, -1800, 2); //定义一张画布 Image offScreenImage = null; //我方飞机的对象 PlaneObj planeObj = new PlaneObj(GameUtils.planeImg, 37, 41, 290, 550, 0, this); //记录游戏绘制的次数 int count = 0; //敌方boss1的对象 LittleBoss1 littleBoss1 = new LittleBoss1(GameUtils.littleBoss1Img, 172, 112, -200, 350, 3, this); //敌方boss2的对象 LittleBoss2 littleBoss2 = new LittleBoss2(GameUtils.littleBoss2Img, 172, 112, 300, -150, 2, this); //获取敌方boss对象 BossObj bossObj = new BossObj(GameUtils.bossImg, 240, 174, 180, -180, 2, this); //获取警示标志的对象 WarningObj waringObj = new WarningObj(GameUtils.warningImg, 599, 90, 0, 350, 0, this); //定义一个变量来记录我方飞机的索引 public static int planeIndex = 0; //定义一个变量记录游戏得分 public static int score = 0; public void launch() { //窗口是否可见 this.setVisible(true); //窗口的大小 this.setSize(600, 800); //窗口的位置 this.setLocationRelativeTo(null); //窗口的标题 this.setTitle("飞机大战3.0"); //关闭窗口会自动结束进程 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //将所有要绘制的游戏物体全部放入所有元素集合中进行绘制 GameUtils.gameObjList.add(bgObj); GameUtils.gameObjList.add(planeObj); playMusic(); //这里拿到我方飞机的索引值 planeIndex = GameUtils.gameObjList.indexOf(planeObj); //鼠标的点击事件 this.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == 1 && state == 0) {//当我们游戏处于一个未开始的状态下点击才能有反应 state = 1;//游戏开始状态 } //当游戏失败或者通关时重新开始游戏 if (e.getButton() == 3 && state == 3 || state == 4) { count = 1; GameUtils.enemy2ObjList.clear(); GameUtils.shellObjList.clear(); GameUtils.explodeObjList.clear(); GameUtils.littleBoss2BulletList.clear(); GameUtils.littleBoss1BulletList.clear(); GameUtils.tripleShellObjList.clear(); GameUtils.enemy2BulletObjList.clear(); GameUtils.giftObjList.clear(); GameUtils.doubleShellObjList.clear(); GameUtils.gameObjList.clear(); GameUtils.gameObjList.add(bgObj); GameUtils.gameObjList.add(planeObj); bossObj = null; // planeObj=null; //飞机装上道具的次数归0 planeObj.times = 0; score = 0; state = 1; repaint(); } } }); //添加键盘监听事件 this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == 32) { if (state == 1) { state = 2; } else if (state == 2) { state = 1; } } } }); while (true) { repaint(); try { Thread.sleep(25);//25毫秒 } catch (Exception e) { e.printStackTrace(); } } } public void paint(Graphics g) { //初始化双缓存图片对象 if (offScreenImage == null) { offScreenImage = createImage(600, 800);//大小要和游戏窗口大小相同 } //获取双缓存图片对象的画笔 Graphics gImage = offScreenImage.getGraphics(); //将画布画成一个矩形 gImage.fillRect(0, 0, 600, 800); if (state == 0) { gImage.drawImage(GameUtils.bgImg, 0, 0, null); gImage.drawImage(GameUtils.explodeImg, 270, 350, null); gImage.drawImage(GameUtils.planeImg, 280, 470, null); gImage.drawImage(GameUtils.bossImg, 190, 70, null); //绘制游戏开始界面的文字 gImage.setColor(Color.BLUE); gImage.setFont(new Font("仿宋", Font.BOLD, 30)); gImage.drawString("鼠标左键开始游戏", 180, 300); } if (state == 1) { createObj(); //bgObj.paintSelf(gImage); //planeObj.paintSelf(gImage); //shellObj.paintSelf(gImage); //将爆炸集合添加到所有元素集合中 GameUtils.gameObjList.addAll(GameUtils.explodeObjList); //不再单独绘制某个游戏元素,因为所有游戏元素都放入了所有元素集合中,这里只需要将集合中所有元素遍历出来,然后绘制自身即可 for (int i = 0; i < GameUtils.gameObjList.size(); i++) { GameUtils.gameObjList.get(i).paintSelf(gImage); } //将要移除的元素从所有元素集合删除,减小绘制的压力, //gameObjList和removeList中有很多元素重合, GameUtils.gameObjList.removeAll(GameUtils.removeList); count++; } //游戏暂停 if (state == 2) { gImage.drawImage(GameUtils.bgImg, 0, 0, null); GameUtils.drawWord(gImage, "游戏暂停", Color.YELLOW, 30, 220, 300); } //失败 if (state == 3) { gImage.drawImage(GameUtils.bgImg, 0, 0, null); GameUtils.drawWord(gImage, "游戏失败", Color.RED, 30, 220, 300); } //通关 if (state == 4) { gImage.drawImage(GameUtils.bgImg, 0, 0, null); GameUtils.drawWord(gImage, "游戏通关", Color.GREEN, 30, 220, 300); } //绘制游戏的积分面板 GameUtils.drawWord(gImage, score + "分", Color.green, 40, 30, 100); //将双缓存图片绘制在游戏窗口 g.drawImage(offScreenImage, 0, 0, null); } //整个方法是用来批量创建物体 void createObj() { if (count % 15 == 0) {//这里控制子弹产生的速度 if (planeObj.times == 3) {//这里使用的是一级子弹 GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, 14, 29, planeObj.getX() + 12, planeObj.getY() - 20, 40, this)); GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));//添加到所有元素集合中的对象,是新new出来的子弹对象,并不是整个子弹集合 } if (planeObj.times == 0) {//这里使用的是二级子弹 GameUtils.doubleShellObjList.add(new DoubleShellObj(GameUtils.doubleShellImg, 32, 64, planeObj.getX() + 5, planeObj.getY() - 20, 15, this)); GameUtils.gameObjList.add(GameUtils.doubleShellObjList.get(GameUtils.doubleShellObjList.size() - 1)); } if (planeObj.times == 1) {//这里使用的是三级子弹 GameUtils.tripleShellObjList.add(new TripleShellObj(GameUtils.tripleShellImg, 64, 182, planeObj.getX() - 5, planeObj.getY() - 100, 15, this)); GameUtils.gameObjList.add(GameUtils.tripleShellObjList.get(GameUtils.tripleShellObjList.size() - 1)); } } //两种敌方飞机 if (count % 15 == 0) {//控制敌方小飞机的产生速度 GameUtils.enemy1ObjList.add(new Enemy1Obj(GameUtils.enemy1Img, 32, 24, (int) ((Math.random() * 10) * 60), 0, 5, this)); GameUtils.gameObjList.add(GameUtils.enemy1ObjList.get(GameUtils.enemy1ObjList.size() - 1));//76 } if (count % 25 == 0) { if (count % 100 == 0) { GameUtils.enemy2ObjList.add(new Enemy2Obj(GameUtils.enemy2Img, 44, 67, (int) ((Math.random() * 10) * 60), 0, 3, this)); GameUtils.gameObjList.add(GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1)); } //只有敌方大飞机子弹产生后,才能产生敌方大飞机子弹 if (GameUtils.enemy2ObjList.size() > 0) { //这里x,y 产生的坐标就是最新敌方大飞机产生坐标的位置,然后用这个位置产生敌方大飞机的子弹 int x = (GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1)).getX(); int y = (GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1)).getY(); GameUtils.enemy2BulletObjList.add(new Enemy2BulletObj(GameUtils.enemy2BulletImg, 14, 25, x + 17, y + 55, 6, this)); GameUtils.gameObjList.add(GameUtils.enemy2BulletObjList.get(GameUtils.enemy2BulletObjList.size() - 1)); } } if (count == 800 && (!GameUtils.gameObjList.contains(bossObj))) { GameUtils.gameObjList.add(bossObj); } if (count == 600 && (!GameUtils.gameObjList.contains(littleBoss2))) { GameUtils.gameObjList.add(littleBoss2); } if (count == 600 && (!GameUtils.gameObjList.contains(littleBoss1))) { GameUtils.gameObjList.add(littleBoss1); } if (count % 15 == 0) { if (GameUtils.gameObjList.contains(littleBoss1)) { GameUtils.littleBoss1BulletList.add(new LittleBoss1Bullet(GameUtils.littleBoss1BulletImg, 42, 42, littleBoss1.getX() + 75, littleBoss1.getY() + 100, 4, this)); GameUtils.gameObjList.add(GameUtils.littleBoss1BulletList.get(GameUtils.littleBoss1BulletList.size() - 1)); } } if (count % 40 == 0) { if (GameUtils.gameObjList.contains(littleBoss2)) { GameUtils.littleBoss2BulletList.add(new LittleBoss2Bullet(GameUtils.littleBoss2BulletImg, 21, 59, littleBoss2.getX() + 78, littleBoss2.getY() + 100, 4, this)); GameUtils.gameObjList.add(GameUtils.littleBoss2BulletList.get(GameUtils.littleBoss2BulletList.size() - 1)); } } if (count == 1300 && (!GameUtils.gameObjList.contains(bossObj))) { GameUtils.gameObjList.add(bossObj); } if (count % 20 == 0) { if (GameUtils.gameObjList.contains(bossObj)) { //敌方1号boss子弹 GameUtils.littleBoss1BulletList.add(new LittleBoss1Bullet(GameUtils.littleBoss1BulletImg, 42, 42, bossObj.getX() + 10, bossObj.getY() + 130, 4, this)); GameUtils.gameObjList.add(GameUtils.littleBoss1BulletList.get(GameUtils.littleBoss1BulletList.size() - 1)); //敌方2号boss的子弹 //再次取余为了让导弹速度慢一些 if (count % 150 == 0) { GameUtils.littleBoss2BulletList.add(new LittleBoss2Bullet(GameUtils.littleBoss2BulletImg, 21, 59, bossObj.getX() + 220, bossObj.getY() + 130, 1, this)); GameUtils.gameObjList.add(GameUtils.littleBoss2BulletList.get(GameUtils.littleBoss2BulletList.size() - 1)); } //boss子弹 GameUtils.bossBulletList.add(new BossBullet(GameUtils.bossBulletImg, 51, 72, bossObj.getX() + 70, bossObj.getY() + 100, 8, this)); GameUtils.gameObjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1)); } } //警示出现的时间 if (count == 750 && (!GameUtils.gameObjList.contains(waringObj))) { GameUtils.gameObjList.add(waringObj); } //警示消失的时间 if (count == 780) { GameUtils.removeList.add(waringObj); } } static Clip clip; public static void playMusic() { try { //这里面放 绝对路径,音频必须是wav格式,用音频转换软件 把mp3 转成wav格式 File musicPath = new File("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\PlayMusic\\bgm.wav"); if(musicPath.exists()) { AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath); clip = AudioSystem.getClip(); clip.open(audioInput); FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-20.0f);//设置音量,范围为 -60.0f 到 6.0f clip.start(); clip.loop(Clip.LOOP_CONTINUOUSLY); } else { } } catch(Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { GameWin gameWin = new GameWin(); gameWin.launch(); } }  二,工具类 import java.awt.*; import java.util.ArrayList; import java.util.List; //游戏元素的父类 public class GameUtils { //获取背景图片 public static Image bgImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\bg.jpg"); //获取boss图片 public static Image bossImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\boss.png"); //获取爆炸图片 public static Image explodeImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\explode\\e6.gif"); //获取我方飞机图片 public static Image planeImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\plane.png"); //获取我方飞机子弹的图片 public static Image shellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\shell.png"); //获取大飞机子弹的图片 public static Image enemy2BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy2bullet.png"); //获取敌方小飞机的图片 public static Image enemy1Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy1.png"); //获取敌方大飞机的图片 public static Image enemy2Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy2.png"); //获取敌方boss1的图片 public static Image littleBoss1Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss1.png"); //获取敌方boss2的图片 public static Image littleBoss2Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss2.png"); //获取敌方1号boss子弹的图片 public static Image littleBoss1BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss1bullet.png"); //获取敌方2号boss子弹的图片 public static Image littleBoss2BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss2bullet.png"); //获取补给的图片 public static Image giftImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\gift.png"); //获取二级子弹的图片 public static Image doubleShellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\doubleshell.png"); //获取三级子弹的图片 public static Image tripleShellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\tripleshell.png"); //获取boss子弹的图片 public static Image bossBulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\bossbullet.png"); //获取警示标志的图片 public static Image warningImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\warning.gif"); //创建我方飞机子弹的集合 public static List shellObjList=new ArrayList(); //创建敌方小飞机的集合 public static List enemy1ObjList=new ArrayList(); //创建敌方大飞机的集合 public static List enemy2ObjList=new ArrayList(); //创建大飞机子弹的集合 public static List enemy2BulletObjList=new ArrayList(); //所有元素的集合 public static List gameObjList=new ArrayList(); //爆炸集合 public static List explodeObjList=new ArrayList(); //移除窗口元素的集合 public static List removeList=new ArrayList(); //1号boss子弹的集合 public static List littleBoss1BulletList=new ArrayList(); //2号boss子弹的集合 public static List littleBoss2BulletList=new ArrayList(); //补给的集合 public static List giftObjList=new ArrayList(); //二级子弹的集合 public static List doubleShellObjList=new ArrayList(); //三级子弹的集合 public static List tripleShellObjList=new ArrayList(); //boss子弹的集合 public static List bossBulletList=new ArrayList(); public static void drawWord(Graphics gImage,String str,Color color, int size,int x,int y){ gImage.setColor(color); gImage.setFont(new Font("仿宋",Font.BOLD,size)); gImage.drawString(str,x,y); } } 三, 所有游戏元素的父类 import java.awt.*; //所有元素的父类 public class GameObj { //元素的图片 Image img; //游戏元素的大小 int width; int height; //游戏元素的位置 int x; int y; //元素的运动速度 double speed; //窗口类 GameWin frame; //set和get方法 public Image getImg() { return img; } public void setImg(Image img) { this.img = img; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public GameWin getFrame() { return frame; } public void setFrame(GameWin frame) { this.frame = frame; } //构造方法 public GameObj() { } public GameObj(int x,int y) { this.x=x; this.y=y; } public GameObj(Image img, int x, int y, double speed) { this.img = img; this.x = x; this.y = y; this.speed = speed; } public GameObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) { this.img = img; this.width = width; this.height = height; this.x = x; this.y = y; this.speed = speed; this.frame = frame; } //绘制元素自身的方法 public void paintSelf(Graphics g){ g.drawImage(img,x,y,null); } //获取自身矩形的方法,用来进行碰撞检测 public Rectangle getRec(){ return new Rectangle(x,y,width,height); } }  四,背景类 import java.awt.*; public class BgObj extends GameObj{ public BgObj() { super(); } public BgObj(Image img, int x, int y, double speed) { super(img, x, y, speed); } public BgObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) { super(img, width, height, x, y, speed, frame); } @Override public void paintSelf(Graphics g) { super.paintSelf(g); //背景图向下移动 y+=speed; if(y>=0){ y=-1800; } } @Override public Rectangle getRec() { return super.getRec(); } }  五,Boss子弹类 import java.awt.*; public class BossBullet extends GameObj{ public BossBullet() { super(); } public BossBullet(Image img, int width, int height, int x, int y, double speed, GameWin frame) { super(img, width, height, x, y, speed, frame); } public BossBullet(Image img, int x, int y, double speed) { super(img, x, y, speed); } public BossBullet(int x, int y) { super(x, y); } @Override public void paintSelf(Graphics g) { super.paintSelf(g); y+=speed; //越界判断 if(this.y>800){ GameUtils.removeList.add(this); } } @Override public Rectangle getRec() { return super.getRec(); } } 六, BossObj类 import java.awt.*; public class BossObj extends GameObj{ int health=200; public BossObj() { super(); } public BossObj(Image img, int x, int y, double speed) { super(img, x, y, speed); } public BossObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) { super(img, width, height, x, y, speed, frame); } @Override public void paintSelf(Graphics g) { super.paintSelf(g); //与littleBoss2运动轨迹一样,先从上往下走,再左右移动 if(y0){ shellObj.setX(-100); shellObj.setY(-100); GameUtils.removeList.add(shellObj); health--; } else if (this.getRec().intersects(shellObj.getRec())&&health0){ doubleshellObj.setX(-100); doubleshellObj.setY(-100); GameUtils.removeList.add(doubleshellObj); health-=3; } else if (this.getRec().intersects(doubleshellObj.getRec())&&health0){ tripleshellObj.setX(-100); tripleshellObj.setY(-100); GameUtils.removeList.add(tripleshellObj); health-=5; } else if (this.getRec().intersects(tripleshellObj.getRec())&&health800){ GameUtils.removeList.add(this); } } @Override public Rectangle getRec() { return super.getRec(); } } 十, 敌机2类 import java.awt.*; public class Enemy2Obj extends GameObj{ int health=10; public Enemy2Obj() { super(); } public Enemy2Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) { super(img, width, height, x, y, speed, frame); } public Enemy2Obj(Image img, int x, int y, double speed) { super(img, x, y, speed); } @Override public void paintSelf(Graphics g) { super.paintSelf(g); y+=speed; //一级子弹和敌方大飞机碰撞的代码 for (ShellObj shellObj: GameUtils.shellObjList) { if(this.getRec().intersects(shellObj.getRec())&&health>0){ //子弹消失 shellObj.setX(-100); shellObj.setY(-100); GameUtils.removeList.add(shellObj); health--; }else if(this.getRec().intersects(shellObj.getRec())&&health0){ doubleshellObj.setX(-100); doubleshellObj.setY(-100); GameUtils.removeList.add(doubleshellObj); health-=3; } else if (this.getRec().intersects(doubleshellObj.getRec())&&health0){ tripleshellObj.setX(-100); tripleshellObj.setY(-100); GameUtils.removeList.add(tripleshellObj); health-=5; } else if (this.getRec().intersects(tripleshellObj.getRec())&&health800){ GameUtils.removeList.add(this); } } @Override public Rectangle getRec() { return super.getRec(); } }  十一,爆炸类 import java.awt.*; public class ExplodeObj extends GameObj{ //定义一个image类型的静态数组,存放一串爆炸图 static Image[] explodePic=new Image[16]; //定义变量来记录爆炸图的次数 int explodeCount=0; //定义一个静态代码块来将爆炸图片放到数组当中 static{ for (int i = 0; i < explodePic.length; i++) { explodePic[i]=Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\explode\\e"+(i+1)+".gif"); } } @Override public void paintSelf(Graphics g) { if(explodeCount400){ speed=-1; } //我方一级子弹和敌方1号boss碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失 //遍历我方飞机子弹 for(ShellObj shellObj: GameUtils.shellObjList){ if(this.getRec().intersects(shellObj.getRec())&&health>0){ shellObj.setX(-100); shellObj.setY(-100); GameUtils.removeList.add(shellObj); health--; } else if (this.getRec().intersects(shellObj.getRec())&&health0){ doubleshellObj.setX(-100); doubleshellObj.setY(-100); GameUtils.removeList.add(doubleshellObj); health-=3; } else if (this.getRec().intersects(doubleshellObj.getRec())&&health0){ tripleshellObj.setX(-100); tripleshellObj.setY(-100); GameUtils.removeList.add(tripleshellObj); health-=5; } else if (this.getRec().intersects(tripleshellObj.getRec())&&health


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有