JAVA小游戏**飞机大战**

        这个一个使用Java适合新手练手的小游戏;主要锻炼面向对象系列知识,存储数据市面大多使用的是集合,本文使用的为数组,更加贴近初学者!作者也是刚入门,有问题望指出并多见谅,让我们一起加油!

飞行物类

public abstract class FlyingObject {
    protected int width, height;    //宽和高
    protected int x, y;             //x轴y轴
    protected double speed;         //速度
    protected FlyingObject[] flys = new FlyingObject[]{};

    //构造方法
    public FlyingObject() {
    }

    //天空、子弹和英雄机的构造
    public FlyingObject(int width, int height, int x, int y) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    //小敌机、大敌机和英雄机的构造
    public FlyingObject(int width, int height) {
        this.width = width;
        this.height = height;
        this.x = (int) (Math.random() * (World.WIDTH - this.width));
        this.y = -this.height;
    }

    //飞行物移动
    public abstract void step();

    //添加对象方法
    public void addFly() {
        for (int i = 0; i < 2; i++) {
            //  0-小蜜蜂/1.2.3-小敌机/4.5-大敌机
            int num = (int) (Math.random() * 8);
            flys = Arrays.copyOf(flys, flys.length + 1);

            if (num == 0) {
                flys[flys.length - 1] = new Bee();
            } else if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5) {
                flys[flys.length - 1] = new Airplane();
            } else {
                flys[flys.length - 1] = new BigAirplane();
            }
        }
    }
    public void delete(){
        for (int i = 0; i < flys.length; i++) {
            if (flys[i] != null) {
                if (flys[i].y >= 700) {
                    flys[i] = null;
                }
            }
        }
    }

    //读取图片
    public static BufferedImage loadImg(String fileName) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(FlyingObject.class.getResource(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return img;
    }

    //画笔(绘制)方法
    public abstract void paintSelf(Graphics g);

 英雄机类

//英雄机
public class Hero extends FlyingObject {
    int life;               //血条
    int doubleFire;         //火力(大于0则双倍火力)
    private static BufferedImage[] img;
    private boolean fla = false;
    int score;                      //分数

    static {
        img = new BufferedImage[2];
        img[0] = loadImg("hero0.png");//调用父类方法,获取图片
        img[1] = loadImg("hero1.png");//调用父类方法,获取图片
    }


    //get set方法
    public int getLife() {
        return life;
    }
    public void setLife(int life) {
        this.life = life;
    }
    public int getDoubleFire() {
        return doubleFire;
    }
    public void setDoubleFire(int doubleFire) {
        this.doubleFire = doubleFire;
    }
    public static BufferedImage[] getImg() {
        return img;
    }
    public static void setImg(BufferedImage[] img) {
        Hero.img = img;
    }

    public Hero() {
        super(97, 124, 151, 525);
        life = 3;
        score = 0;
        //doubleFire = 20;
        doubleFire = 0;
        //doubleFire = 50;
    }

    //让英雄机移动到鼠标位置
    public void moveToMouse(int mx,int my){
        x=mx-48;
        y=my-62;
    }

    //重写父类移动方法
    public void step() {
        fla = !fla;
    }

    //重写画笔(绘制)方法
    public void paintSelf(Graphics g) {
        if (fla == true) {
            g.drawImage(img[0], x, y, null);
        } else {
            g.drawImage(img[1], x, y, null);
        }
    }
    //绘制得分方法
    public void paintScore(Graphics g) {
        g.setColor(new Color(0,153,153));
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
        g.drawString("SCORE:" + score, 15, 25);
        g.drawString("LIFT:" + life, 160, 25);
        g.drawString("火力值:" + doubleFire, 270, 25);
    }
}

 小蜜蜂类

//蜜蜂
public class Bee extends FlyingObject {
    private int xSpeed;         //x轴速度
    private int ySpeed;         //y轴速度
    private int awardType;      //奖励类型
    private static BufferedImage img;
    static {
        img=loadImg("bee0.png");    //调用父类方法,获取图片
    }
    //无参构造
    public Bee() {
        super(60,50);
        xSpeed = 2;
        ySpeed = 1;
    }

    //get set方法
    public int getxSpeed() {
        return xSpeed;
    }
    public void setxSpeed(int xSpeed) {
        this.xSpeed = xSpeed;
    }
    public int getySpeed() {
        return ySpeed;
    }
    public void setySpeed(int ySpeed) {
        this.ySpeed = ySpeed;
    }
    public int getAwardType() {
        return awardType;
    }
    public void setAwardType(int awardType) {
        this.awardType = awardType;
    }
    public static BufferedImage getImg() {
        return img;
    }
    public static void setImg(BufferedImage img) {
        Bee.img = img;
    }

    //重写画笔(绘制)方法
    public void paintSelf(Graphics g){
        g.drawImage(img,x,y,null);
    }
    //重写父类移动方法
    public void step() {
        x+=xSpeed;      //左右跑
        y+=ySpeed;      //往下跑
        if (x<0 || x>(World.WIDTH-this.width)){
            xSpeed*=-1;
        }
    }
}

小敌机类

//小敌机
public class Airplane extends FlyingObject {
    private static BufferedImage img;

    //get set方法
    public static BufferedImage getImg() {
        return img;
    }
    public static void setImg(BufferedImage img) {
        Airplane.img = img;
    }

    static {
        img = loadImg("airplane0.png");//调用父类方法,获取图片
    }
    //构造方法
    Airplane() {
        super(49, 36);
        speed = 2;
    }

    //重写父类移动方法
    public void step() {
        y += speed;
    }
    //重写画笔(绘制)方法
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
    }
}

 大敌机

//大敌机
public class BigAirplane extends FlyingObject {
    private static BufferedImage img;
    static {
        img = loadImg("bigplane0.png"); //调用父类方法,获取图片
    }

    //构造方法
    public BigAirplane() {
        super(70, 90);
        speed = 2;
    }

    //get set方法
    public static BufferedImage getImg() {
        return img;
    }
    public static void setImg(BufferedImage img) {
        BigAirplane.img = img;
    }

    @Override
    //重写父类移动方法
    public void step() {
        y += speed;
    }

    //重写画笔(绘制)方法
    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
    }
}

 天空类(背景)

//背景天空
public class Sky extends FlyingObject {
    private int y1;             //第二张图片的y坐标
    private static BufferedImage img;

    static {
        img = loadImg("background.png");//调用父类方法,获取图片
    }

    //构造
    public Sky() {
        super(World.WIDTH, World.HEIGHT, 0, 0);
        speed = 1;
        y1 = -World.HEIGHT;
    }

    //get set方法
    public int getY1() {
        return y1;
    }
    public void setY1(int y1) {
        this.y1 = y1;
    }
    public static BufferedImage getImg() {
        return img;
    }
    public static void setImg(BufferedImage img) {
        Sky.img = img;
    }

    //重写父类移动方法
    public void step() {
//        if (y == 0) {
//            y = -200;
//        }
//        y += speed;
        y += speed;
        y1 += speed;
        if (y == height) {
            y = -height;
        }
        if (y1 == height) {
            y1 = -height;
        }
    }
    //重写画笔(绘制)方法
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
        g.drawImage(img, x, y1, null);
    }
}

 子弹类

//子弹
public class Bullet extends FlyingObject {
    private static BufferedImage img;
    int dir;  //0的时候左上角飞;1的时候往上飞;2的时候右上角飞;3的时候大敌机的往下飞
    Bullet[] bull = new Bullet[]{};     //英雄机子弹
    Bullet[] bull1 = new Bullet[]{};    //大敌机子弹

    static {
        img = loadImg("bullet.png");//调用父类方法,获取图片
    }

    //构造方法
    public Bullet() {
        speed = 3;
    }

    public Bullet(int x, int y, int dir) {
        super(8, 14, x, y);
        Hero hero = new Hero();
        speed = 3;
        this.dir = dir;
    }

    //get set方法
    public static BufferedImage getImg() {
        return img;
    }

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

    //重写父类移动方法
    public void step() {
        if (dir == 0) {         //左飞
            y -= speed;
            x -= 1;
        } else if (dir == 1) {  //往上飞
            y -= speed;
        } else if (dir == 2) {  //往右边飞
            y -= speed;
            x += 1;
        } else {                //往下飞
            y += speed;
        }
    }

    //重写画笔(绘制)方法
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, null);
    }

    //英雄机子弹数组添加方法
    public void addBull(int hx, int hy, int dir) {
        bull = Arrays.copyOf(bull, bull.length + 1);
        bull[bull.length - 1] = new Bullet(hx, hy, dir);
    }
    //大敌机子弹数组添加方法
    public void addBull1(int hx, int hy, int dir) {
        bull1 = Arrays.copyOf(bull1, bull1.length + 1);
        bull1[bull1.length - 1] = new Bullet(hx, hy, dir);
    }
}

 游戏世界类(窗口)

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值