飞机大战第二版

前言

第二版是对第一版对完善,第二版基本实现了大小敌机、蜜蜂等飞行物的移动,以及自己操控的战斗机已经实现了子弹的射出,还未完成有战斗机跟随鼠标移动功能和子弹击中敌机的碰撞效果。

Bee类

package PlaneGame.plane;

public class Bee extends Father{
    private int speedy;
    private int speedx=3;
    private int blood;

    public Bee() {
        super((int)(Math.random()*Test.width-Test.beepicture.getWidth()),0,Test.beepicture);
        this.blood = 1;
        this.speedy = 3;
    }

    @Override
    public void move() {
        setX(getX()+speedx);
        if (getX() <=  0){
            speedx = 1;
        }
        if (getX() >= Test.width-Test.beepicture.getWidth()){
            speedx = -1;
        }
        setY(getY()+speedy);
    }
}

BigPlane类

package PlaneGame.plane;

public class BigPlane extends Father{
    private int speed;
    private int blood;
    private int score;

    public BigPlane() {
        super((int)(Math.random()*Test.width-Test.bigpicture.getWidth()),0,Test.bigpicture);
        this.blood = 2;
        this.score = 5;
        this.speed = 1;
    }

    @Override
    public void move() {
        setY(getY()+speed);
    }
}

Father 类

package PlaneGame.plane;

import java.awt.image.BufferedImage;

public abstract class Father {
    private int x;
    private int y;
    private int width;
    private int height;
    private BufferedImage img;
    public Father(){

    }
    public Father(int x,int y,BufferedImage img){
        this.x = x;
        this.y = y;
        this.img = img;
    }
    public BufferedImage getImg() {
        return img;
    }

    public void setImg(BufferedImage img) {
        this.img = img;
    }
    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 abstract void move();
}

Hero 类

package PlaneGame.plane;

import java.awt.image.BufferedImage;
import java.time.Year;

public class Hero extends Father{
    private int score;
    private int blood;
    private Bullet bullet;

    public Hero() {
       super(200,400,Test.heropicture1);
        this.blood = 3;
        this.score = 0;
        //img = Test.hero;
    }
    private int count=0;
    private BufferedImage[] images = {Test.heropicture2,Test.heropicture1};
    @Override
    public void move() {
        count++;
        if(count%2==0){
            this.setImg(Test.heropicture1);
        }else{
            this.setImg(Test.heropicture2);
        }
        this.setImg(images[count%2]);
    }
    public Bullet[] shoot(){
        Bullet[] bullets =new Bullet[1];
        bullets[0] = new Bullet(this.getX()+this.getWidth()/2,this.getY());
        return bullets;
    }
}

SmallPlane 类

package PlaneGame.plane;

public class SmallPlane extends Father{
    int speed;
    int blood;

    public SmallPlane() {
        super((int)(Math.random()*Test.width-Test.smallpicture.getWidth()),0,Test.smallpicture);
        this.speed = 2;
        this.blood = 1;
    }

    @Override
    public void move() {
        setY(getY()+speed);
    }
}

Bullet 类

package PlaneGame.plane;

public class Bullet extends Father{
    int speed;

    public Bullet(int x,int y) {
        super(200,400,Test.bulletpicture);
        //this.speed = 5;
        speed = -5;
    }

    @Override
    public void move() {
        setY(getY()+speed);
    }
}

Test 测试类

package PlaneGame.plane;
import PlaneGame.JPanelImpl;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TimerTask;

public class Test extends JPanel{
    public static BufferedImage bg;
    public static int width=400;
    public static int height=600;
    public static BufferedImage bigpicture;
    public static BufferedImage beepicture;
    public static BufferedImage heropicture1;
    public static BufferedImage smallpicture;
    public static BufferedImage bulletpicture;
    public static BufferedImage heropicture2;
    static {
        try {
            bg = ImageIO.read(Test.class.getResourceAsStream("image/background.png"));
            bigpicture = ImageIO.read(Test.class.getResourceAsStream("image/bigplane.png"));
            beepicture = ImageIO.read(Test.class.getResourceAsStream("image/bee.png"));
            heropicture1 = ImageIO.read(Test.class.getResourceAsStream("image/hero0.png"));
            heropicture2 = ImageIO.read(Test.class.getResourceAsStream("image/hero1.png"));
            smallpicture = ImageIO.read(Test.class.getResourceAsStream("image/airplane.png"));
            bulletpicture = ImageIO.read(Test.class.getResourceAsStream("image/bullet.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private ArrayList<Father> flyall = new ArrayList<>();
    private ArrayList<Bullet> bulletall = new ArrayList<>();
    private BufferedImage[] heroimages = {Test.heropicture2,Test.heropicture1};
    java.util.Timer timer = new java.util.Timer();    //创建定时器
    public void action(){
        //鼠标监听器功能
        MouseAdapter mouse = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                for (int i = 0; i < heroimages.length; i++) {

                }
            }
        };
        this.addMouseListener(mouse);
        this.addMouseMotionListener(mouse);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
//                bigpalne.move();
//                bee.move();
//                smallplane.move();
                hero.move();
                createfly();
                createbullet();
                allmove();
                outfly();
                bulletmove();
                repaint();//页面的刷新
            }
        },100,50);
    }
//子弹的移动方法
    private int bulletflag=0;
    private void bulletmove() {
        for (int i = 0; i < bulletall.size(); i++) {
            Bullet bs = bulletall.get(i);
            bs.move();
        }
    }
//创建子弹
    private void createbullet() {
        bulletflag++;
        if (bulletflag%5==0){
            Bullet[] bullets = hero.shoot();
            for (int i = 0; i < bullets.length; i++) {
                bulletall.add(bullets[i]);
            }
        }

    }
//飞行物的越界问题
    private void outfly() {
        for (int i = 0; i < flyall.size(); i++) {
            Father fly = flyall.get(i);
            if(fly.getY()>=Test.height){
                flyall.remove(i);
                i--;
            }

        }
    }



//所有飞行物的移动问题
    private void allmove() {
        for (int i = 0; i < flyall.size(); i++) {
            Father flymove = flyall.get(i);
            flymove.move();
        }
    }
//创建三大飞行物:小蜜蜂、大敌机、小敌机的集合
    private int flyflag = 0;
    private void createfly() {
        Father fly;
        flyflag++;
        if (flyflag % 10 == 0){
            int ran = (int)(Math.random()*10);
            if (ran == 0 || ran ==1){
                fly = new Bee();
            }else if (ran ==2 ||ran == 3|| ran ==4||ran==5){
                fly = new BigPlane();
            }else {
                fly = new SmallPlane();
            }
            flyall.add(fly);
        }
    }


//    BigPlane bigpalne = new BigPlane();
//    Bee bee = new Bee();
    Hero hero = new Hero();
//    SmallPlane smallplane = new SmallPlane();
    //Bullet bullet = new Bullet();
//添加窗口上面的画板,使
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg,0,0,this);
        g.drawImage(hero.getImg(),hero.getX(),hero.getY(),this);
        //g.drawImage(bullet.getImg(),bullet.getX(),bullet.getY(),this);
        paintfly(g);
//        g.drawImage(smallplane.getImg(),smallplane.getX(),smallplane.getY(),this);
//        g.drawImage(bigpalne.getImg(),bigpalne.getX(),bigpalne.getY(),this);
//        g.drawImage(bee.getImg(),bee.getX(),bee.getY(),this);

        paintbullets(g);
    }
//把子弹画到画板上
    private void paintbullets(Graphics g) {
        for (int i = 0; i < bulletall.size(); i++) {
            Bullet bs = bulletall.get(i);
            g.drawImage(bs.getImg(),bs.getX(),bs.getY(),this);
        }
    }
//把创建的三大飞行物画到画板上
    private void paintfly(Graphics g) {
        for (int i = 0; i < flyall.size(); i++) {
            Father flyallpaint = flyall.get(i);
            g.drawImage(flyallpaint.getImg(),flyallpaint.getX(),flyallpaint.getY(),this);
        }
    }
//主函数
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("飞机大战");
        Test test = new Test();
        jFrame.add(test);
        jFrame.setSize(width,height);
        jFrame.setLocationRelativeTo(jFrame.getOwner());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        test.action();
    }
}

注意要点:

如想测试效果,记得需要把测试类中的图片链接进行更换(“image/background.png”),同时需要注意的是图片包必须跟创建的各个类处在同一个大包下才能加载成功,否则会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值