zoukankan      html  css  js  c++  java
  • 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.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    
    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 % 2 == 0) {
                View plane = new View("plane.jpg", e.getX(), e.getY(), 50, 50, 3,
                        panel);
                list.add(plane);
                count++;
            } else {
                View bullet = new View("bullet.png", e.getX(), e.getY(), 10, 20, 5,
                        panel);
                list.add(bullet);
                count++;
            }
        }
    
        public void run() {
            while (true) {
                for (int i = 0; i < list.size(); i++) {
                    View v = list.get(i);
                    v.move();
                    if(i!=0)
                        v.collisions(list);
                }
    
                panel.repaint();
    
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
    }
    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;
    
        /**
         * 构造方法
         * 
         * @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;
        }
    
        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() {
            if (imageName.equals("background.jpg")) {
                y += moveY;
                if (y == 0)
                    y = -60;
            } else if (imageName.equals("bullet.png")) {
                y += moveY;
                if (y >= 400)
                    y = 0;
            } 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
                                + "发生了碰撞");
                    }
                }
            }
        }
    
    }
  • 相关阅读:
    深入理解Java内存(图解堆栈)
    java堆栈区别
    Java之堆栈的区别
    【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片
    【微信小程序】转载:微信小程序实战篇-下拉刷新与加载更多
    【微信小程序】转载:微信小程序之购物车功能
    【微信小程序】loading标签使用,可自定义时长
    【微信小程序】日历插件,适用于酒店订房类小程序
    【微信小程序】微信小程序wx.previewImage预览图片
    【微信小程序】小程序和公众号 退款功能教程(含申请退款和退款回调,退款回调地址在商户后台配置或者代码自定义)
  • 原文地址:https://www.cnblogs.com/chang1203/p/5863038.html
Copyright © 2011-2022 走看看