飞机大战小游戏

实现过程

  1. 新建FlyObject类
  2. 创建飞行物(小蜜蜂、小飞机、大飞机、英雄机、子弹)子类
  3. 实现生成飞行物
  4. 实现飞行物的移动
  5. 实现碰撞消失以及爆炸效果
  6. 增加奖励以及分数机制
    添加了飞机爆炸效果,并且设有多重难度以及奖励,玩法更丰富
    以下是相应代码
    FlyObject类: 所有飞行物的父类
import java.awt.image.BufferedImage;/*** 飞行物类:x,y,图片、宽、高、速度 子类:英雄机、小敌机、小蜜蜂、子弹、大敌机*/
public abstract class FlyObject {protected int x;protected int y;protected BufferedImage image;protected int speed;protected int width;protected int height;protected int flood;public FlyObject() {}public abstract void move();public boolean crashBullet(Bullet a) {int x = a.getX();int y = a.getY();return this.x < x && x < this.x + width && this.y < y && y < this.y + height;}public BulletEnemy shootHero() {return null;}public int getFlood() {return flood;}public void setFlood(int flood) {this.flood = flood;}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 BufferedImage getImage() {return image;}public void setImage(BufferedImage image) {this.image = image;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}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;}}

Hero类: 英雄机

import java.awt.image.BufferedImage;/*** 英雄机类:继承FlyObject 子弹、生命、分数*/
public class Hero extends FlyObject {private int life;//英雄机生命private Bullet bullet;private int awardType;//判断奖励类型,攻击子弹类型private boolean check;//判断是否收到撞击,是否显示爆炸效果private int count;//显示爆炸效果的计数private int shootSpeed;//发射子弹速度public Hero() {x = 150;y = 450;// hero0.png图片和Hero类在同一个包下image = ShootGame.hero0Img;width = image.getWidth();height = image.getHeight();speed = 1;life = 3;awardType = 0;count = 0;check = false;shootSpeed = 30;}// 英雄机移动BufferedImage[] images = {ShootGame.hero0Img, ShootGame.hero1Img};BufferedImage[] images2 = {ShootGame.hero_ember0Img, ShootGame.hero_ember1Img, ShootGame.hero_ember2Img, ShootGame.hero_ember3Img};//英雄机动画效果@Overridepublic void move() {//切换imageif (check) {if (count < 4) {image = images2[count];count++;} else {count = 0;check = false;}} else {image = images[speed++ % 2];}}//敌机撞击public boolean crashFlyings(FlyObject flying) {int x = flying.getX();int y = flying.getY();int flyingWidth = flying.getWidth();int flyingHeight = flying.getHeight();return (this.x < x + flyingWidth && x < this.x + width && this.y + height / 3 < y + flyingHeight && y < this.y + height);}//撞击子弹public boolean crashBulletEnemy(BulletEnemy a) {int x = a.getX();int y = a.getY();return this.x < x && x < this.x + width && this.y + height / 3 < y && y < this.y + height;}//发射子弹public Bullet[] shoot() {int type = 0;if (awardType >= 70) {type = 7;} else if (awardType >= 40) {type = 6;} else if (awardType >= 20) {type = 5;} else if (awardType >= 10) {type = 4;} else if (awardType >= 5) {type = 3;} else if (awardType >= 1) {type = 2;} else if (awardType >= 0) {type = 1;}Bullet[] bu;if (type == 1) {bu = new Bullet[1];shootSpeed = 20;bu[0] = new Bullet(x + width / 2 - ShootGame.bulletImg.getWidth() / 2, y);} else if (type == 2) {bu = new Bullet[2];shootSpeed = 20;bu[0] = new Bullet(x + width / 4, y);bu[1] = new Bullet(x + width * 3 / 4, y);} else if (type == 3) {bu = new Bullet[2];shootSpeed = 15;bu[0] = new Bullet(x + width / 4, y);bu[1] = new Bullet(x + width * 3 / 4, y);} else if (type == 4) {bu = new Bullet[3];shootSpeed = 15;bu[0] = new Bullet(x + width / 4, y);bu[1] = new Bullet(x + width / 2, y);bu[2] = new Bullet(x + width * 3 / 4, y);} else if (type == 5) {bu = new Bullet[4];shootSpeed = 10;bu[0] = new Bullet(x, y);bu[1] = new Bullet(x + width / 4, y);bu[2] = new Bullet(x + width * 3 / 4, y);bu[3] = new Bullet(x + width, y);} else if (type == 6) {bu = new Bullet[6];shootSpeed = 5;bu[0] = new Bullet(x, y);bu[1] = new Bullet(x + width * 2 / 10, y);bu[2] = new Bullet(x + width * 4 / 10, y);bu[3] = new Bullet(x + width * 6 / 10, y);bu[4] = new Bullet(x + width * 8 / 10, y);bu[5] = new Bullet(x + width, y);} else if (type == 7) {bu = new Bullet[8];shootSpeed = 5;bu[0] = new Bullet(x - width * 2 / 10, y);bu[1] = new Bullet(x, y);bu[2] = new Bullet(x + width * 2 / 10, y);bu[3] = new Bullet(x + width * 4 / 10, y);bu[4] = new Bullet(x + width * 6 / 10, y);bu[5] = new Bullet(x + width * 8 / 10, y);bu[6] = new Bullet(x + width, y);bu[7] = new Bullet(x + width * 12 / 10, y);} else {bu = new Bullet[1];shootSpeed = 15;bu[0] = new Bullet(x + width / 2 - ShootGame.bulletImg.getWidth() / 2, y);}return bu;}public int getShootSpeed() {return shootSpeed;}public void setShootSpeed(int shootSpeed) {this.shootSpeed = shootSpeed;}public boolean isCheck() {return check;}public void setCheck(boolean check) {this.check = check;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getAwardType() {return awardType;}public void setAwardType(int awardType) {this.awardType = awardType;}public int getLife() {return life;}public void setLife(int life) {this.life = life;}
}

Bee类: 小蜜蜂(加奖励)

/*** 小蜜蜂类:继承FlyObject* 奖励、血量*/
public class Bee extends FlyObject implements Award{private boolean flag = true;private int award;public Bee() {image = ShootGame.beeImg;width = image.getWidth();height = image.getHeight();x = (int) (Math.random() * (400 - width));y = -height;speed = 3;flood = 2;award = (int) (Math.random() * 2);//奖励类型:0/1}@Overridepublic int getAward() {return award;}@Overridepublic void move() {setY(getY() + getSpeed());if (getX() < 0) {flag = true;} else if (getX() > 400 - width) {flag = false;}if (flag) {setX(getX() + getSpeed() * 2);} else {setX(getX() - getSpeed() * 2);}}public int getFlood() {return flood;}public void setFlood(int flood) {this.flood = flood;}}

Airplane类: 小飞机(加分数)

/*** 敌机类:继承FlyObject 血量、分数*/
public class Airplane extends FlyObject implements Enemy {private int score;public Airplane() {image = ShootGame.airplaneImg;width = image.getWidth();height = image.getHeight();speed = 4;x = (int) (Math.random() * (400 - width));y = -height;flood = 2;score = 10;}@Overridepublic int getScore() {return score;}@Overridepublic void move() {setY(getY() + getSpeed());}public int getFlood() {return flood;}public void setFlood(int flood) {this.flood = flood;}}

Bigplane类: 大飞机(加分数和加奖励)

/*** 大敌机类:继承FlyObject 奖励、血量、分数*/
public class Bigplane extends FlyObject implements Enemy, Award{private int score;private int award;public Bigplane() {image = ShootGame.bigplaneImg;width = image.getWidth();height = image.getHeight();speed = 1;x = (int) (Math.random() * (400 - width));y = -height;flood = 6;score = 20;award = 1;}@Overridepublic int getScore() {return score;}@Overridepublic BulletEnemy shootHero() {BulletEnemy bu = new BulletEnemy(x + width / 2 - ShootGame.bulletImg.getWidth() / 2, y + height);return bu;}@Overridepublic void move() {setY(getY() + getSpeed());}public int getFlood() {return flood;}public void setFlood(int flood) {this.flood = flood;}public int getAward() {return award;}}

Bullet类 英雄机发射的子弹

/*** 子弹类:继承FlyObject 类型*/
public class Bullet extends FlyObject {private int bulletType;public Bullet(int x, int y) {// x、y是根据英雄机的位置动态传入的值image = ShootGame.bulletImg;width = image.getWidth();height = image.getHeight();speed = 3;this.x = x;this.y = y;}@Overridepublic void move() {setY(getY() - getSpeed() * 3);}public int getBulletType() {return bulletType;}public void setBulletType(int bulletType) {this.bulletType = bulletType;}}

Ember类: 爆炸效果的父类

public class Ember {protected FlyObject fly;protected int count;public Ember(FlyObject fly) {this.fly = fly;}public Ember() {}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public void changeImg(){}
}

EmberAirplane类: 小飞机爆炸

import java.awt.image.BufferedImage;public class EmberAirplane extends Ember {public EmberAirplane(FlyObject fly) {super(fly);count = 0;}BufferedImage[] images = {ShootGame.airplane_ember0Img, ShootGame.airplane_ember1Img, ShootGame.airplane_ember2Img, ShootGame.airplane_ember3Img};@Overridepublic void changeImg() {this.fly.setImage(images[count]);}
}

EmberBee类: 小蜜蜂爆炸

import java.awt.image.BufferedImage;public class EmberBee extends Ember {public EmberBee(FlyObject fly) {super(fly);count = 0;}BufferedImage[] images = {ShootGame.bee_ember0Img, ShootGame.bee_ember1Img, ShootGame.bee_ember2Img, ShootGame.bee_ember3Img};@Overridepublic void changeImg() {this.fly.setImage(images[count]);}
}

EmberBigplane类: 大飞机爆炸

import java.awt.image.BufferedImage;public class EmberBigplane extends Ember{public EmberBigplane(FlyObject fly) {super(fly);count = 0;}BufferedImage[] images = {ShootGame.bigplane_ember0Img, ShootGame.bigplane_ember1Img, ShootGame.bigplane_ember2Img, ShootGame.bigplane_ember3Img};@Overridepublic void changeImg() {this.fly.setImage(images[count]);}
}

BulletEnemy类: 难度4以上,大飞机发射的子弹

public class BulletEnemy extends FlyObject{public BulletEnemy(int x, int y) {// x、y是根据英雄机的位置动态传入的值image = ShootGame.bulletImg;width = image.getWidth();height = image.getHeight();speed = 3;this.x = x;this.y = y;}@Overridepublic void move() {setY(getY() + getSpeed() * 3);}
}

Award接口:

public interface Award {int getAward();
}

Enemy接口:

public interface Enemy {int getScore();
}

ShootGame类以及主程序:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;/*** 界面类:窗口类JFrame-对象 面板/画板类JPanel - 不能直接new JPanel();*/
public class ShootGame extends JPanel {//画板宽度和高度public static final int WIDTH = 400;//游戏界面的宽public static final int HEIGHT = 654;//游戏界面的高public static final int START = 0;//开始public static final int RUNNING = 1;//运行public static final int PAUSE = 2;//暂停public static final int GAMEOVER = 3;//游戏结束private int state;//游戏状态private int score = 0;//游戏分数private int difficultType = 1;//游戏难度private int difficultShootSpeed = 60;//大飞机射击速度private int difficult = 50;//飞行物生成速度private int countAirplane = 0;//计数击杀小飞机数量private int countBigplane = 0;//计数击杀大飞机数量private int countBee = 0;//计数击杀小蜜蜂数量private Hero hero;//英雄机private FlyObject[] flyings = {};// 用于存放敌机+小蜜蜂private Bullet[] bullets = {};// 用于存放英雄机子弹private BulletEnemy[] bullets2 = {};// 用于存放大敌机子弹private Ember[] embers = {};//用于存放灰烬// 定义所有的图片资源public static BufferedImage hero0Img;public static BufferedImage hero1Img;public static BufferedImage hero_ember0Img;public static BufferedImage hero_ember1Img;public static BufferedImage hero_ember2Img;public static BufferedImage hero_ember3Img;public static BufferedImage beeImg;public static BufferedImage bee_ember0Img;public static BufferedImage bee_ember1Img;public static BufferedImage bee_ember2Img;public static BufferedImage bee_ember3Img;public static BufferedImage bulletImg;public static BufferedImage airplaneImg;public static BufferedImage airplane_ember0Img;public static BufferedImage airplane_ember1Img;public static BufferedImage airplane_ember2Img;public static BufferedImage airplane_ember3Img;public static BufferedImage bigplaneImg;public static BufferedImage bigplane_ember0Img;public static BufferedImage bigplane_ember1Img;public static BufferedImage bigplane_ember2Img;public static BufferedImage bigplane_ember3Img;public static BufferedImage background;public static BufferedImage startImg;public static BufferedImage pauseImg;public static BufferedImage gameoverImg;public static BufferedImage luFuImg;static {// 加载图片资源
//      String path = Hero.class.getResource("hero0.png").getFile();try {// read静态方法background = ImageIO.read(Hero.class.getResource("background.png"));hero0Img = ImageIO.read(Hero.class.getResource("hero0.png"));hero1Img = ImageIO.read(Hero.class.getResource("hero1.png"));hero_ember0Img = ImageIO.read(Hero.class.getResource("hero_ember0.png"));hero_ember1Img = ImageIO.read(Hero.class.getResource("hero_ember1.png"));hero_ember2Img = ImageIO.read(Hero.class.getResource("hero_ember2.png"));hero_ember3Img = ImageIO.read(Hero.class.getResource("hero_ember3.png"));beeImg = ImageIO.read(Hero.class.getResource("bee.png"));bee_ember0Img = ImageIO.read(Hero.class.getResource("bee_ember0.png"));bee_ember1Img = ImageIO.read(Hero.class.getResource("bee_ember1.png"));bee_ember2Img = ImageIO.read(Hero.class.getResource("bee_ember2.png"));bee_ember3Img = ImageIO.read(Hero.class.getResource("bee_ember3.png"));bulletImg = ImageIO.read(Hero.class.getResource("bullet.png"));airplaneImg = ImageIO.read(Hero.class.getResource("airplane.png"));airplane_ember0Img = ImageIO.read(Hero.class.getResource("airplane_ember0.png"));airplane_ember1Img = ImageIO.read(Hero.class.getResource("airplane_ember1.png"));airplane_ember2Img = ImageIO.read(Hero.class.getResource("airplane_ember2.png"));airplane_ember3Img = ImageIO.read(Hero.class.getResource("airplane_ember3.png"));bigplaneImg = ImageIO.read(Hero.class.getResource("bigplane.png"));bigplane_ember0Img = ImageIO.read(Hero.class.getResource("bigplane_ember0.png"));bigplane_ember1Img = ImageIO.read(Hero.class.getResource("bigplane_ember1.png"));bigplane_ember2Img = ImageIO.read(Hero.class.getResource("bigplane_ember2.png"));bigplane_ember3Img = ImageIO.read(Hero.class.getResource("bigplane_ember3.png"));startImg = ImageIO.read(Hero.class.getResource("start.png"));pauseImg = ImageIO.read(Hero.class.getResource("pause.png"));gameoverImg = ImageIO.read(Hero.class.getResource("gameover.png"));luFuImg = ImageIO.read(Hero.class.getResource("lufu.jpg"));} catch (IOException e) {e.printStackTrace();}}public ShootGame() {// 测试// 创建英雄机hero = new Hero();}// 绘画的方法 - JPanel父类给的 - 重写// setVisiable(true)-自动调用paint@Overridepublic void paint(Graphics g) {super.paint(g);// 自定义绘画方法// 图片对象:image x坐标 y坐标 nullg.drawImage(background, 0, 0, null);//画背景paintAll(g);g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);//画英雄机paintFlyObject(g);//画敌机+小蜜蜂paintBullets(g);//画子弹paintBulletEnemy(g);//画敌机子弹paintEmbers(g);//画爆炸效果paintState(g);//画状态// 字符串 x坐标 y坐标g.setColor(new Color(255, 255, 0));//设置颜色Font font = new Font("宋体", Font.BOLD, 15);//字体、风格:三个常量IFont.PLAIN,Font.BOLD,Font.ITALIC、字号setFont(font);g.drawString("SCORE:" + score, 10, 15);//画分数g.drawString("LIFE:" + hero.getLife(), 10, 30);//画生命paintDifficult(g);//画难度g.drawString("小飞机:" + countAirplane, 10, 580);//画击杀小飞机数量g.drawString("大敌机:" + countBigplane, 10, 595);//画击杀大敌机数量g.drawString("小蜜蜂:" + countBee, 10, 610);//画击杀小蜜蜂数量g.drawString("awardType:" + hero.getAwardType(), 250, 610);//奖励数量}// 画飞行物:画敌机+小蜜蜂flyingsprivate void paintFlyObject(Graphics g) {for (int i = 0; i < flyings.length; i++) {g.drawImage(flyings[i].getImage(), flyings[i].getX(), flyings[i].getY(), null);}}// 画子弹private void paintBullets(Graphics g) {for (int i = 0; i < bullets.length; i++) {g.drawImage(bullets[i].getImage(), bullets[i].getX(), bullets[i].getY(), null);}}//画地方子弹private void paintBulletEnemy(Graphics g) {for (int i = 0; i < bullets2.length; i++) {g.drawImage(bullets2[i].getImage(), bullets2[i].getX(), bullets2[i].getY(), null);}}//画状态private void paintState(Graphics g) {switch (state) {case START:g.drawImage(startImg, 0, 0, null);break;case PAUSE:g.drawImage(pauseImg, 0, 0, null);break;case GAMEOVER:g.drawImage(gameoverImg, 0, 0, null);break;}}//画难度public void paintDifficult(Graphics g) {switch (difficultType) {case 1:g.drawString("难度:1", 10, 45);break;case 2:g.drawString("难度:2", 10, 45);break;case 3:g.drawString("难度:3", 10, 45);break;case 4:g.drawString("难度:4", 10, 45);break;case 5:g.drawString("难度:5", 10, 45);break;case 6:g.drawString("难度:6", 10, 45);break;}}//画爆炸效果public void paintEmbers(Graphics g) {for (int i = 0; i < embers.length; i++) {g.drawImage(embers[i].fly.getImage(), embers[i].fly.getX(), embers[i].fly.getY(), null);}}//画清屏效果public void paintAll(Graphics g) {if (countClick >= 3) {g.drawImage(luFuImg, -500, 0, null);}}/*** 开始游戏:定时器-持续性、周期性* 生成敌机、小蜜蜂 -> 飞行物* 生成子弹 -> 英雄机* 移动 -> 飞行物* 判断游戏是否结束* 判断飞行物和子弹有没有撞击* 判断英雄机和飞行物有没有撞击* 判断子弹有没有出界* 判断飞行物有没有出界*/// 定时器Timer timer = new Timer();//清屏效果private int countClick = 0;private long time1 = 0;private long time2 = 0;// 开始游戏public void action() {//匿名内部类方式实现监听MouseAdapter ma = new MouseAdapter() {//鼠标单击时调用@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);switch (state) {case START:state = RUNNING;break;case GAMEOVER://全部清空归0hero = new Hero();flyings = new FlyObject[0];bullets = new Bullet[0];bullets2 = new BulletEnemy[0];state = START;score = 0;countAirplane = 0;countBee = 0;countBigplane = 0;difficultShootSpeed = 60;difficultType = 1;break;case RUNNING://清屏效果(生命-1)if (countClick < 3) {time1 = System.currentTimeMillis();countClick++;} else {time2 = System.currentTimeMillis();countClick = 0;}if (Math.abs(time2 - time1) <= 500) {
//                            flyings = new FlyObject[0];
//                            bullets2 = new BulletEnemy[0];
//                            System.out.println("清屏");hero.setLife(hero.getLife() - 1);//作弊器:快速点击三下鼠标,攻击最大化hero.setAwardType(80);time1 = 0;time2 = 0;}}}//鼠标进入时调用@Overridepublic void mouseEntered(MouseEvent e) {super.mouseEntered(e);if (state == PAUSE) {state = RUNNING;}}//鼠标移出时调用@Overridepublic void mouseExited(MouseEvent e) {super.mouseExited(e);if (state != GAMEOVER && state != START) {state = PAUSE;}}//鼠标移动时调用@Overridepublic void mouseMoved(MouseEvent e) {super.mouseMoved(e);if (state == RUNNING) {hero.setX(e.getX() - hero.getWidth() / 2);hero.setY(e.getY() - hero.getHeight() / 2);repaint();}}};//添加鼠标事件this.addMouseListener(ma);this.addMouseMotionListener(ma);// 过1s后开始执行,每0.1s执行一次timer.schedule(new TimerTask() {// 周期性任务@Overridepublic void run() {if (state == RUNNING) {//生成飞行物enterFlyObject();//生成子弹shootAction();//移动goFlyings();//子弹与飞行物撞击crashBulletAndFlyings();//英雄机与飞行物撞击crashHeroFlyings();//英雄机和大飞机子弹撞击crashBulletEnemy();//清理出界outOfBoundsAction();//爆炸效果goEmber();//检查游戏是否结束checkGame();}//界面刷新repaint();}}, 100, 20);}private int countFlyings = 0;//定时// 生成敌机、小蜜蜂 -> flyingsprivate void enterFlyObject() {countFlyings++;if (score > 5000) {difficultShootSpeed = 10;//大飞机射击速度difficultType = 6;//游戏难度difficult = 5;//飞行物生成速度} else if (score > 3000) {difficultShootSpeed = 30;//大飞机射击速度difficultType = 5;//游戏难度difficult = 20;//飞行物生成速度} else if (score > 1500) {difficultShootSpeed = 60;difficultType = 4;difficult = 30;} else if (score > 500) {difficultShootSpeed = 60;difficultType = 3;difficult = 30;} else if (score > 200) {difficultShootSpeed = 60;difficultType = 2;difficult = 50;} else {difficultShootSpeed = 60;difficultType = 1;difficult = 60;}if (countFlyings % difficult == 0) {int random = (int) (Math.random() * 15);FlyObject newFly;if (random == 0) {newFly = new Bee();} else if (random > 0 && random < 5) {newFly = new Bigplane();} else {newFly = new Airplane();}flyings = Arrays.copyOf(flyings, flyings.length + 1);flyings[flyings.length - 1] = newFly;}}//发射子弹private void shootAction() {if (countFlyings % hero.getShootSpeed() == 0) {Bullet[] newBullet = hero.shoot();bullets = Arrays.copyOf(bullets, bullets.length + newBullet.length);System.arraycopy(newBullet, 0, bullets, bullets.length - newBullet.length, newBullet.length);}if (difficultType >= 4) {if (countFlyings % difficultShootSpeed == 0) {for (int i = 0; i < flyings.length; i++) {if (flyings[i] instanceof Bigplane) {BulletEnemy newBullet = flyings[i].shootHero();bullets2 = Arrays.copyOf(bullets2, bullets2.length + 1);bullets2[bullets2.length - 1] = newBullet;}}}}}//飞行物移动private void goFlyings() {for (int i = 0; i < flyings.length; i++) {flyings[i].move();}for (int i = 0; i < bullets.length; i++) {bullets[i].move();}for (int i = 0; i < bullets2.length; i++) {bullets2[i].move();}hero.move();}//清理过界private void outOfBoundsAction() {//判断飞行物for (int i = 0; i < flyings.length; i++) {if (flyings[i].getY() > HEIGHT) {hero.setAwardType(hero.getAwardType() / 2);//有飞机飞过界奖励减半FlyObject temp = flyings[i];flyings[i] = flyings[flyings.length - 1];flyings[flyings.length - 1] = temp;flyings = Arrays.copyOf(flyings, flyings.length - 1);}}//判断子弹for (int i = 0; i < bullets.length; i++) {if (bullets[i].getY() < -bullets[i].height) {Bullet temp = bullets[i];bullets[i] = bullets[bullets.length - 1];bullets[bullets.length - 1] = temp;bullets = Arrays.copyOf(bullets, bullets.length - 1);}}for (int i = 0; i < bullets2.length; i++) {if (bullets2[i].getY() > HEIGHT) {bullets2[i] = bullets2[bullets2.length - 1];bullets2 = Arrays.copyOf(bullets2, bullets2.length - 1);}}}//子弹与飞行物撞击private void crashBulletAndFlyings() {int flag = -1;//飞行物下标for (int i = 0; i < bullets.length; i++) {// i -> 子弹下标flag = crashBulletFlyings(bullets[i]);if (flag != -1) {flyings[flag].setFlood(flyings[flag].getFlood() - 1);if (flyings[flag].getFlood() == 0) {//生成灰烬Ember newEmber;if (flyings[flag] instanceof Airplane) {newEmber = new EmberAirplane(flyings[flag]);} else if (flyings[flag] instanceof Bigplane) {newEmber = new EmberBigplane(flyings[flag]);} else {newEmber = new EmberBee(flyings[flag]);}embers = Arrays.copyOf(embers, embers.length + 1);embers[embers.length - 1] = newEmber;if (flyings[flag] instanceof Airplane) {Airplane a = (Airplane) flyings[flag];countAirplane++;//判断击杀计数score += a.getScore();//计分} else if (flyings[flag] instanceof Bigplane) {Bigplane a = (Bigplane) flyings[flag];countBigplane++;//判断击杀计数score += a.getScore();//计分if (a.getAward() == 0) {//判断获得奖励hero.setLife(hero.getLife() + 1);//加生命} else {hero.setAwardType(hero.getAwardType() + 1);//加攻击等级}} else {Bee a = (Bee) flyings[flag];countBee++;//判断击杀计数if (a.getAward() == 0) {//判断获得奖励hero.setLife(hero.getLife() + 1);//加生命} else {hero.setAwardType(hero.getAwardType() + 1);//加攻击等级}}FlyObject temp = flyings[flag];//清除撞击物flyings[flag] = flyings[flyings.length - 1];flyings[flyings.length - 1] = temp;flyings = Arrays.copyOf(flyings, flyings.length - 1);}Bullet temp2 = bullets[i];//清除撞击物bullets[i] = bullets[bullets.length - 1];bullets[bullets.length - 1] = temp2;bullets = Arrays.copyOf(bullets, bullets.length - 1);}}}private int crashBulletFlyings(Bullet bullet) {int flag = -1;//飞行物下标for (int i = 0; i < flyings.length; i++) {if (flyings[i].crashBullet(bullet)) {flag = i;//记录撞击飞行物break;}}return flag;}//英雄机与飞行物撞击private void crashHeroFlyings() {int flag = -1;//飞行物下标for (int i = 0; i < flyings.length; i++) {if (hero.crashFlyings(flyings[i])) {flag = i;//记录撞击飞行物break;}}if (flag != -1) {hero.setCheck(true);//显示爆炸效果hero.setLife(hero.getLife() - 1);//生命-1hero.setAwardType(hero.getAwardType() / 2);//清除奖励FlyObject temp = flyings[flag];//清除撞击物flyings[flag] = flyings[flyings.length - 1];flyings[flyings.length - 1] = temp;flyings = Arrays.copyOf(flyings, flyings.length - 1);}}//英雄机和子弹撞击private void crashBulletEnemy() {int flag = -1;//飞行物下标for (int i = 0; i < bullets2.length; i++) {if (hero.crashBulletEnemy(bullets2[i])) {flag = i;//记录撞击飞行物break;}}if (flag != -1) {hero.setCheck(true);//显示爆炸效果hero.setLife(hero.getLife() - 1);//生命-1hero.setAwardType(hero.getAwardType() / 2);//清除奖励bullets2[flag] = bullets2[bullets2.length - 1];//清除子弹bullets2 = Arrays.copyOf(bullets2, bullets2.length - 1);}}//爆炸效果public void goEmber() {for (int i = 0; i < embers.length; i++) {if (embers[i].getCount() >= 4) {embers[i] = embers[embers.length - 1];embers = Arrays.copyOf(embers, embers.length - 1);} else {embers[i].changeImg();embers[i].setCount(embers[i].getCount() + 1);}}}//判断游戏是否结束public void checkGame() {if (hero.getLife() <= 0) {state = GAMEOVER;}}// 创建窗口,初始化画板public void showMe() {JFrame window = new JFrame("飞机大战");// 设置窗口大小window.setSize(WIDTH, HEIGHT);// 设置窗口的关闭选项window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置初始位置 - 居中window.setLocationRelativeTo(null);// 居中// 将画板添加到窗口上window.add(this);window.setVisible(true);// 显示窗口,最后一步}public static void main(String[] args) {ShootGame s = new ShootGame();s.showMe();s.action();}
}

本人初学者,写的不好请多多见谅,勿喷!

我的Java学习笔记(二)飞机大战小游戏相关推荐

  1. java飞机场模拟程序_java 飞机大战 小游戏源码

    [实例简介] 本项目是一个使用java做的一个飞机大战的小游戏,一个英雄机,初始有三次生命,当打中蜜蜂会有一次生命奖励,当打中敌机会有相应分数奖励,但如果被敌机打中会失去一次生命机会.如果生命都失去, ...

  2. Java学习笔记二:数据类型

    Java学习笔记二:数据类型 1. 整型:没有小数部分,允许为负数,Java整型分4种:int short long byte 1.1 Int最为常用,一个Int类型变量在内存中占用4个字节,取值范围 ...

  3. JAVA学习笔记(四)城堡游戏

    城堡游戏 我们在尝试了之前的简单媒体库构造之后,试着整合一下之前学到的关于类,继承,多态等知识,制作一个简单的城堡游戏,城堡游戏是一个简单的文字游戏,通过输入命令可以在地图上不同的房间进行移动. 目录 ...

  4. 基于Java语言在窗体上实现飞机大战小游戏

    全套资料下载地址:https://download.csdn.net/download/sheziqiong/85594271 项目介绍 飞机大战:用 Java 语言在窗体上实现飞机大战小游戏,运行程 ...

  5. 【Java代码实现飞机大战小游戏】简单理解

    飞机大战 飞机大战小游戏历经10天完成,主要用到的就是我们面向对象部分的知识:类,封装,继承,多态,静态代码块等等内容+swing部分内容.所以即使你是java小白,也不用担心欧! 游戏说明:游戏有3 ...

  6. 【Java】Java基础飞机大战小游戏完整代码

    Java基础飞机大战小游戏完整代码 先来展示一下代码实现结果图 主函数ShootGame 初始化游戏原始背景图片,游戏人物图片,游戏开始结束图片:构建产生敌人算法:产生英雄机算法:发射子弹算法:判断是 ...

  7. Qt学习总结——飞机大战小游戏制作

    Qt学习总结--飞机大战小游戏制作 1. 需求分析 这篇文章写于2020年暑假,完成学校实训项目之后,对自己的项目实践做了一个总结,回顾整个项目的制作过程,同时也复习一下Qt的相关知识,总结项目制作过 ...

  8. 点击list view中一行内容可以在combox中显示_java版飞机大战小游戏详细教程(零基础小白也可以分分钟学会!)...

    一:游戏展示 飞机大战小游戏我们都玩过,通过移动飞机来打敌机,这里给大家展示一下游戏成果:呜呜呜由于gif只能上传5M大小,所以就不能给大家展示操作了,如果大家有兴趣可以自己自己做出来再玩哟. 这里面 ...

  9. 用C语言实现飞机大战小游戏

    我的个人博客:谋仁·Blog 该项目已上传至GitHub:点击跳转 文章目录 摘要 运行环境 整体功能思维导图 效果预览 具体功能的实现 图形界面:EasyX EasyX图形库简介 EasyX图形库的 ...

  10. canvas绘制“飞机大战”小游戏,真香

    canvas是ArkUI开发框架里的画布组件,常用于自定义绘制图形.因为其轻量.灵活.高效等优点,被广泛应用于UI界面开发中. 本期,我们将为大家介绍canvas组件的使用. 目录 一.canvas介 ...

最新文章

  1. 树莓派学习笔记——交叉编译工具链
  2. oracle交流 提问,Oracle常见提问5(转)
  3. 【控制】《复杂运动体系统的分布式协同控制与优化》-方浩老师-第2章-基于速度估计的多欧拉-拉格朗日系统分布式控制
  4. matlab 连续显示,请教下MATLAB一个问题啊 我想检测一行数据里面出现连续出现0的次数,...
  5. Python类的自定义属性访问及动态属性设置
  6. Ubuntu 命令行打开pdf文件和打开命令行当前目录
  7. python获取一个月之前日期_利用python获取当前日期前后N天或N月日期的方法示例...
  8. ld cannot find an existing library
  9. 《计算机网络课程设计(第2版)》——2.4节课程设计分析
  10. 关于苹果与摄影的事。
  11. 基于前后端分离的模版探索
  12. EJS脚本中MD5应用
  13. 一、JAVA基础(数据类型、运算符、变量常量)
  14. 第二人生的源码分析(2)第二人生的基本功能
  15. 论文阅读:Semantic Parsing on Freebase from Question-Answer Pairs
  16. 最大公约数简便算法_三种求最大公约数的方法
  17. STEP2——《数据分析:企业的贤内助》重点摘要笔记(六)——数据描述
  18. 第三方支付机构的资本:客户备付金
  19. Excel 获取工龄公式
  20. 职业化形象与商务礼仪

热门文章

  1. Rasa课程、Rasa培训、Rasa面试系列之:Rasa 3.x rasa run actions等运行命令学习
  2. 三.Java基本语法
  3. AttnGAN代码复现(详细步骤+避坑指南)文本生成图像
  4. [Ansible系列⑦]ansible fact变量
  5. 安卓Alarm闹钟唤醒耗电问题的排查
  6. 程序员如何避免沦为IT民工
  7. Anaconda新建虚拟环境-报错Multiple Errors Encountered
  8. Android使用OpenCV合成双目裸眼3D图片(推荐Native方法)
  9. 介绍身份证号姓名实名认证 身份证号姓名校验 身份证号验人API
  10. Java实现短信验证