java之飞机大战的记分标准

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

import javax.swing.ImageIcon;

public class Plane {

    public Image image;
    public int y = 300, x = 100, width = 50, height = 50;
    private String imageName;
    private boolean flag = true;// true表示子弹往下走,false表示子弹往上走
    private int score;
    private Timer timer;

    private PlaneDemo pd;

    public void setPD(PlaneDemo pd) {
        this.pd = pd;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }

    public Plane(int x, int y, int width, int height, String imageName,
            boolean flag, int score) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.imageName = imageName;
        this.flag = flag;
        this.score = score;
        image = new ImageIcon(this.getClass().getResource(imageName))
                .getImage();
    }

    public void move(ArrayList<Plane> list) {
        if (imageName.equals("plane.jpg")) {
            if (flag) {
                y += 3;
                if (y >= 400)
                    list.remove(this);
            } else {
                y -= 3;
                if (y <= 40)
                    y = 300;
            }
        } else if (imageName.equals("bullet.png")) {
            if (flag)
                y += 3;
            else
                y -= 4;
            if (y >= 400)
                list.remove(this);
            else if (y + height <= 0)
                list.remove(this);
        } else if (imageName.equals("background.jpg")) {
            y += 2;
            if (y >= 0)
                y = -226;
        }
    }

    /**
     * 检测当前飞机对象和其他的子弹或者飞机对象是否发生碰撞
     * 
     * @param list就是存储了所有敌方飞机和子弹的对象
     *            (包括自己的)
     */
    public void collision(ArrayList<Plane> list) {
        for (int i = 1; i < list.size(); i++) {
            Plane plane = list.get(i);// 获取队列中的飞机或者子弹对象
            if (plane != this && plane.flag != flag) {// 检测不是自己本身
                // 计算两个圆的距离
                double distance = Math.sqrt((plane.x - this.x)
                        * (plane.x - this.x) + (plane.y - this.y)
                        * (plane.y - this.y));
                // 判断两个图片是否发生了碰撞
                if (distance <= plane.height / 2 + this.height / 2) {
                    // 销毁这两个图片
                    list.remove(i);
                    list.remove(this);

                    // 判断是否要增加这一局游戏的分数
                    if (plane.imageName.equals("bullet.png") && !plane.flag) {
                        if (this.imageName.equals("plane.jpg")) {
                            PlaneDemo.count += this.score;
                            System.out.println(">>>>>>>>>>>1");
                        }
                    } else if (this.imageName.equals("bullet.png")
                            && !this.flag) {
                        if (plane.imageName.equals("plane.jpg")) {
                            PlaneDemo.count += plane.score;
                            System.out.println(">>>>>>>>>>>2");
                        }
                    }

                    // 停止我方飞机的自动生成子弹的线程对象
                    if ((!plane.flag && plane.imageName.equals("plane.jpg"))) {
                        plane.timer.cancel();
                        new SaveInfo(plane.pd);
                    } else if (!this.flag && this.imageName.equals("plane.jpg")) {
                        timer.cancel();
                        new SaveInfo(this.pd);

                    }

                }
            }
        }
    }
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.JFrame;

public class PlaneDemo extends JFrame {

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

    public static int count = 0;
    private ArrayList<Plane> list;
    private Image image;

    public PlaneDemo() {
        list = new ArrayList<Plane>();
        initUI();

        Plane background = new Plane(0, -226, 1024, 626, "background.jpg",
                false,0);
        list.add(background);
    }

    private void initUI() {
        this.setTitle("线程控制图片移动");
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        this.setVisible(true);

        PlaneListener pl = new PlaneListener(list, this);

        this.addMouseListener(pl);

        Thread t = new Thread(pl);
        t.start();

    }

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

        for (int i = 0; i < list.size(); i++) {
            Plane plane = list.get(i);
            g.drawImage(plane.image, plane.x, plane.y, plane.width,
                    plane.height, this);
        }
        //设置要绘制文字的字体
        g.setFont(new Font("正楷",Font.BOLD,50));
        g.setColor(Color.WHITE);
        //绘制分数
        g.drawString("分数:"+count, 20, 60);
    }

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

import javax.swing.JFrame;

public class PlaneListener extends MouseAdapter implements Runnable {

    private ArrayList<Plane> list;
    private PlaneDemo frame;
    private int count = 0;

    private Plane plane = null;

    public PlaneListener(ArrayList<Plane> list, PlaneDemo frame) {
        this.list = list;
        this.frame = frame;
    }

    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        if (count == 0) {
            plane = new Plane(x, 300, 50, 50, "plane.jpg",false,0);
            plane.setPD(frame);
            //实例化定时任务类的对象
            BulletAI task = new BulletAI(plane,list);
            //实例化时间类的对象
            Timer timer = new Timer();
            //让时间类的对象来启动任务,第一个参数表示要启动的任务,第二个参数表示第一次执行的间隔时间,第三个之后每次执行的间隔时间
            timer.schedule(task, 1000, 2000);
            
            plane.setTimer(timer);
            
            count++;
        } else {
            plane = new Plane(x, 40, 50, 50, "plane.jpg",true,5);
        }

        list.add(plane);
    }

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

            frame.repaint();

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

}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class SaveInfo extends JFrame {

    private PlaneDemo pd;

    public SaveInfo(PlaneDemo pd) {
        this.pd = pd;
        initUI();
    }

    private void initUI() {
        this.setTitle("保存信息");
        this.setSize(200, 200);
        this.setDefaultCloseOperation(2);
        this.setLocationRelativeTo(pd);
        this.setResizable(false);
        this.setLayout(new FlowLayout());

        JLabel labName = new JLabel("用户名:");
        this.add(labName);
        textName = new JTextField();
        textName.setPreferredSize(new Dimension(100, 30));
        this.add(textName);
        JLabel labScore = new JLabel("分  数:");
        this.add(labScore);

        JButton butSave = new JButton("保存");
        this.add(butSave);

        JButton butReset = new JButton("取消");
        this.add(butReset);

        this.setVisible(true);

        SaveListener l = new SaveListener(this);

        butSave.addActionListener(l);
        butReset.addActionListener(l);
    }

    public JTextField textName;

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream(
                    "src/xxj/thread0908/score.txt");
            // 创建一个数据输出流对象
            DataInputStream dis = new DataInputStream(fis);

            int length = dis.readByte();

            StringBuffer strB = new StringBuffer();

            for (int i = 0; i < length; i++) {
                char c = dis.readChar();
                strB.append(c);
            }
            int score = dis.readInt();
            System.out.println("名字:"+strB.toString()+"   分数:"+score);
            dis.close();
            fis.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class SaveListener implements ActionListener {

    private SaveInfo si;
    
    public SaveListener(SaveInfo si){
        this.si = si;
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("取消"))
            si.dispose();
        else{
            try {
                FileOutputStream fos = new FileOutputStream("src/xxj/thread0908/score.txt");
                //创建一个数据输出流对象
                DataOutputStream dos = new DataOutputStream(fos);
                
                String name = si.textName.getText();
                
                dos.writeByte(name.length());//写入名字的长度
                //遍历字符串
                for(int i=0;i<name.length();i++){
                    dos.writeChar(name.charAt(i));//将字符写入到文件中
                }
                
                dos.writeInt(PlaneDemo.count);//写入分数
                
                dos.flush();//强制写入
                //关闭流
                dos.close();
                fos.close();
                si.dispose();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            
        }
    }
}
import java.util.ArrayList;
import java.util.TimerTask;

/**
 * 定义自动生成子弹的线程类
 * 
 * @author 熊哥
 * 
 */
public class BulletAI extends TimerTask {

    private Plane bullet;// 声明子弹对象名
    private Plane plane;// 声明飞机对象名
    private ArrayList<Plane> list;// 声明存储子弹的数组队列对象名

    /**
     * 构造方法
     * 
     * @param plane飞机对象
     * @param list存储要绘制的飞机和子弹对象
     */
    public BulletAI(Plane plane, ArrayList<Plane> list) {
        this.plane = plane;
        this.list = list;
    }

    /**
     * 线程的运行方法
     */
    public void run() {
        // 实例化子弹类的对象
        bullet = new Plane(plane.x + plane.width / 2 - 2, plane.y, 4, 8,
                "bullet.png", false,0);
        // 将bullet对象添加到list中
        list.add(bullet);
    }

}
原文地址:https://www.cnblogs.com/chang1203/p/5919471.html