java打飞机完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.util.Random; 敌飞机: 是飞行物,也是敌人 public class Airplane extends FlyingObject implements Enemy { private int speed = 3; //移动步骤 /** 初始化数据 */ public Airplane(){ this.image = ShootGame.airplane; width = image.getWidth(); height = image.getHeight(); y = -height; Random rand = new Random(); x = rand.nextInt(ShootGame.WIDTH - width); } /** 获取分数 */ @Override public int getScore() { return 5; } /** //越界处理 */ @Override public boolean outOfBounds() { return y>ShootGame.HEIGHT; } /** 移动 */ @Override public void step() { y += speed; } } 分数奖励 1 2 3 4 5 6 7 8 9 /** * 奖励 */ public interface Award { int DOUBLE_FIRE = 0; //双倍火力 int LIFE = 1; //1条命 /** 获得奖励类型(上面的0或1) */ int getType(); } 蜜蜂 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import java.util.Random; /** 蜜蜂 */ public class Bee extends FlyingObject implements Award{ private int xSpeed = 1; //x坐标移动速度 private int ySpeed = 2; //y坐标移动速度 private int awardType; //奖励类型 /** 初始化数据 */ public Bee(){ this.image = ShootGame.bee; width = image.getWidth(); height = image.getHeight(); y = -height; Random rand = new Random(); x = rand.nextInt(ShootGame.WIDTH - width); awardType = rand.nextInt(2); //初始化时给奖励 } /** 获得奖励类型 */ public int getType(){ return awardType; } /** 越界处理 */ @Override public boolean outOfBounds() { return y>ShootGame.HEIGHT; } /** 移动,可斜着飞 */ @Override public void step() { x += xSpeed; y += ySpeed; if(x > ShootGame.WIDTH-width){ xSpeed = -1; } if(x < 0){ xSpeed = 1; } } } 子弹类:是飞行物体 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /** * 子弹类:是飞行物 */ public class Bullet extends FlyingObject { private int speed = 3; //移动的速度 /** 初始化数据 */ public Bullet(int x,int y){ this.x = x; this.y = y; this.image = ShootGame.bullet; } /** 移动 */ @Override public void step(){ y-=speed; } /** 越界处理 */ @Override public boolean outOfBounds() { return y<-height; } } 敌人的分数 1 2 3 4 5 6 7 /** * 敌人,可以有分数 */ public interface Enemy { /** 敌人的分数 */ int getScore(); } 飞行物(敌机,蜜蜂,子弹,英雄机) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 import java.awt.image.BufferedImage; /** * 飞行物(敌机,蜜蜂,子弹,英雄机) */ public abstract class FlyingObject { protected int x; //x坐标 protected int y; //y坐标 protected int width; //宽 protected int height; //高 protected BufferedImage image; //图片 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 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 BufferedImage getImage() { return image; } public void setImage(BufferedImage image) { this.image = image; } /** * 检查是否出界 * @return true 出界与否 */ public abstract boolean outOfBounds(); /** * 飞行物移动一步 */ public abstract void step(); /** * 检查当前飞行物体是否被子弹(x,y)击(shoot)中 * @param Bullet 子弹对象 * @return true表示被击中了 */ public boolean shootBy(Bullet bullet){ int x = bullet.x; //子弹横坐标 int y = bullet.y; //子弹纵坐标 return this.x0){ //双倍火力 Bullet[] bullets = new Bullet[2]; bullets[0] = new Bullet(x+xStep,y-yStep); //y-yStep(子弹距飞机的位置) bullets[1] = new Bullet(x+3*xStep,y-yStep); return bullets; }else{ //单倍火力 Bullet[] bullets = new Bullet[1]; bullets[0] = new Bullet(x+2*xStep,y-yStep); return bullets; } } /** 移动 */ @Override public void step() { if(images.length>0){ image = images[index++/10%images.length]; //切换图片hero0,hero1 } } /** 碰撞算法 */ public boolean hit(FlyingObject other){ int x1 = other.x - this.width/2; //x坐标最小距离 int x2 = other.x + this.width/2 + other.width; //x坐标最大距离 int y1 = other.y - this.height/2; //y坐标最小距离 int y2 = other.y + this.height/2 + other.height; //y坐标最大距离 int herox = this.x + this.width/2; //英雄机x坐标中心点距离 int heroy = this.y + this.height/2; //英雄机y坐标中心点距离 return herox>x1 && heroxy1 && heroy