【Java基础实战小项目】飞机大战


飞机大战

实现代码

飞行物原型

package PlaneFight;


import java.awt.image.BufferedImage;

public abstract class prototype {
   
    private int x,y,hp,height,weight,score;
    protected BufferedImage image;
    protected BufferedImage[] explosion;

    public BufferedImage[] getExplosion() {
   
        return explosion;
    }

    public void setExplosion(BufferedImage[] explosion) {
   
        this.explosion = explosion;
    }

    public int getScore() {
   
        return score;
    }

    public void setScore(int score) {
   
        this.score = score;
    }

    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 getHp() {
   
        return hp;
    }

    public void setHp(int hp) {
   
        this.hp = hp;
    }

    public int getHeight() {
   
        return height;
    }

    public void setHeight(int height) {
   
        this.height = height;
    }

    public int getWeight() {
   
        return weight;
    }

    public void setWeight(int weight) {
   
        this.weight = weight;
    }

    public BufferedImage getImg() {
   
        return img;
    }

    public void setImg(BufferedImage img) {
   
        this.img = img;
    }

    private BufferedImage img;

    public prototype() {
   
        this.x = x;
        this.y = y;
        this.hp = hp;
        this.height = height;
        this.weight = weight;
        this.img = img;
    }

    public abstract void move();
    public boolean hitBy(Bullets bullet) {
   
        int x = bullet.getX();									//子弹x 坐标
        int y = bullet.getY();
        return x>this.x && x<this.x+this.weight && y>this.y && y<this.y+this.height;				//子弹与敌人相撞的临界状态
    }
}


小敌机

package PlaneFight;


import java.awt.image.BufferedImage;

public class Small extends prototype{
   
    private int speed,score;
    BufferedImage[] imgss={
   JPanelDemo.small2,JPanelDemo.small3,JPanelDemo.small4,JPanelDemo.small5};
    public int getSpeed() {
   
        return speed;
    }

    public void setSpeed(int speed) {
   
        this.speed = speed;
    }

    public int getScore() {
   
        return score;
    }

    public void setScore(int score) {
   
        this.score = score;
    }
    public Small(){
   
        this.setX((int)(Math.random()*(Test.WIN_WIDTH-JPanelDemo.big.getWidth())));
        this.setY(0-JPanelDemo.small.getHeight());
        this.setHeight(JPanelDemo.small.getHeight());
        this.setWeight(JPanelDemo.small.getWidth());
        this.setHp(3);
        this.setSpeed(4);
        this.setScore(1);
        this.setImg(JPanelDemo.small);
        this.setExplosion(JPanelDemo.airplaneEmber);
    }

    public void move(){
   
        this.setY(this.getY()+this.getSpeed());
    }
}


大敌机

package PlaneFight;

public class Big extends prototype{
   
    private int speed,score;
    public Big(){
   
        this.setX((int)(Math.random()*(Test.WIN_WIDTH-JPanelDemo.big.getWidth())));
        this.setY(0-JPanelDemo.big.getHeight());
        this.setHeight(JPanelDemo.big.getHeight());
        this.setWeight(JPanelDemo.big.getWidth());
        this.setHp(10);
        this.setSpeed(3);
        this.setScore(5);
        this.setImg(JPanelDemo.big);
        this.setExplosion(JPanelDemo.bigPlaneEmber);
    }

    public int getSpeed() {
   
        return speed;
    }

    public void setSpeed(int speed) {
   
        this.speed = speed;
    }

    public int getScore() {
   
        return score;
    }

    public void setScore(int score) {
   
        this.score = score;
    }

    public void move(){
   
        this.setY(this.getY()+this.getSpeed());
    }
}

小蜜蜂

package PlaneFight;

public class Bee extends prototype{
   
    private int speed,score;
    public Bee(){
   
        this.setX((int)(Math.random()*(Test.WIN_WIDTH-JPanelDemo.big.getWidth())));
        this.setY(0-JPanelDemo.bee.getHeight());
        this.setHeight(JPanelDemo.bee.getHeight());
        this.setWeight(JPanelDemo.bee.getWidth());
        this.setHp(5);
   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值