java飞机大战之子弹的自动生成

import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PlaneMain extends JPanel {

    public static void main(String[] args) {
        new PlaneMain();
    }

    private ArrayList<View> list;

    public PlaneMain() {
        list = new ArrayList<View>();
        View background = new View("background.jpg", 0, -60, 700, 460, 2, this);
        list.add(background);
        initUI();
    }

    private void initUI() {
        JFrame frame = new JFrame("飞机大战");
        frame.setSize(700, 400);
        frame.setDefaultCloseOperation(3);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);

        frame.add(this);

        frame.setVisible(true);

        AddListener al = new AddListener(this, list);

        this.addMouseListener(al);

        Thread t = new Thread(al);
        t.start();// 启动线程
    }

    /**
     * 重写JPanel的重绘方法
     */
    public void paint(Graphics g) {
        super.paint(g);

        for (int i = 0; i < list.size(); i++) {
            View v = list.get(i);
            g.drawImage(v.getBackground(), v.getX(), v.getY(), v.getWidth(),
                    v.getHeight(), this);
        }
    }

}
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class View {

    private Image background;
    private int x = 0, y = -60, moveY, width, height;
    private JPanel panel;
    private String imageName;
    private boolean flag;
    /**
     * 构造方法
     * 
     * @param background背景图片的对象
     * @param x起始X坐标
     * @param y起始Y坐标
     */
    public View(String imageName, int x, int y, int width, int height,
            int moveY, JPanel panel) {
        this.imageName = imageName;
        this.background = new ImageIcon(this.getClass().getResource(imageName))
                .getImage();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.moveY = moveY;
        this.panel = panel;
    }
    /**
     * 构造方法
     * 
     * @param background背景图片的对象
     * @param x起始X坐标
     * @param y起始Y坐标
     */
    public View(String imageName, int x, int y, int width, int height,
            int moveY, JPanel panel, boolean flag) {
        this.imageName = imageName;
        this.background = new ImageIcon(this.getClass().getResource(imageName))
                .getImage();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.moveY = moveY;
        this.panel = panel;
        this.flag = flag;
    }

    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 Image getBackground() {
        return background;
    }

    public void setBackground(Image background) {
        this.background = background;
    }

    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 getMoveY() {
        return moveY;
    }

    public void setMoveY(int moveY) {
        this.moveY = moveY;
    }

    public JPanel getPanel() {
        return panel;
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }

    public void move(ArrayList<View> list) {
        if (imageName.equals("background.jpg")) {
            y += moveY;
            if (y == 0)
                y = -60;
        } else if (imageName.equals("bullet.png")) {
            if (flag) {//地方的子弹
                y += moveY;
                if (y >= 400)
                    y = 0;
            }else {
                y -= moveY;
                if (y <= 0)
                    list.remove(this);
            }
        } else if (imageName.equals("plane.jpg")) {
            y -= moveY;
            if (y <= 0)
                y = 400;
        }
    }

    /**
     * 碰撞方法
     */
    public void collisions(ArrayList<View> list) {
        for (int i = 1; i < list.size(); i++) {
            View v = list.get(i);
            if (this != v) {
                double distance = Math.sqrt((this.x - v.x) * (this.x - v.x)
                        + Math.pow(this.y - v.y, 2));
                if (distance <= this.height + v.height) {
                    System.out.println(v.imageName + "和" + this.imageName
                            + "发生了碰撞");
                }
            }
        }
    }

}
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer;

import javax.swing.JPanel;

public class AddListener extends MouseAdapter implements Runnable {

    private int count = 0;

    private JPanel panel;
    private ArrayList<View> list;

    public AddListener(JPanel panel, ArrayList<View> list) {
        this.panel = panel;
        this.list = list;
    }

    public void mouseReleased(MouseEvent e) {
        if (count == 0) {
            View plane = new View("plane.jpg", e.getX(), e.getY(), 50, 50, 3,
                    panel, false);
            list.add(plane);

            //实例化定时任务类的对象(任务对象)
            BulletAI task = new BulletAI(panel, list, plane);
            //创建时间类的对象
            Timer timer = new Timer();
            //启动任务,第一次执行时在2秒之后,之后每一次执行都间隔2秒
            timer.schedule(task, 2000, 2000);
            
            //timer.cancel();//取消时间对象中所有定时任务
            
            //bai.start();
            count++;
        } else {
            View bullet = new View("bullet.png", e.getX(), e.getY(), 10, 20, 5,
                    panel, true);
            list.add(bullet);
        }
    }

    public void run() {
        while (true) {
            for (int i = 0; i < list.size(); i++) {
                View v = list.get(i);
                v.move(list);
                if (i != 0)
                    v.collisions(list);
            }

            panel.repaint();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}
import java.util.ArrayList;
import java.util.TimerTask;

import javax.swing.JPanel;

public class BulletAI extends TimerTask { // Thread {

    private JPanel panel;// 绘制飞机和子弹的面板对象
    private ArrayList<View> list;// 存储飞机和子弹的数组队列
    private View plane;// 发生子弹的飞机对象

    public BulletAI(JPanel panel, ArrayList<View> list, View plane) {
        this.panel = panel;
        this.list = list;
        this.plane = plane;
    }

    public void run() {
        //
        // try {
        // Thread.sleep(2000);
        // } catch (InterruptedException e1) {
        // e1.printStackTrace();
        // }
        //
        // while (true) {
        View v = new View("bullet.png", plane.getX(), plane.getY(), 10, 20, 5,
                panel, false);// 最后一个参数false表示的是我方飞机发射的子弹

        list.add(v);

        // try {
        // Thread.sleep(1000);
        // } catch (InterruptedException e) {
        // e.printStackTrace();
        // }
        //
        // }
    }

}

 

1.如何自动生成子弹
使用线程来控制子弹的生成。
BulletAI.java

2.Java的定时任务
TimerTask implements Runnable
Timer
启动定时任务

 

转载于:https://www.cnblogs.com/chang1203/p/5863165.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值