学习JAVA.day05

学习JAVA.day05
--------------------------------飞机大战(二)----------------------------------
效果图:
图一
图二
图三

---------------------------------------------------------------------------------------
附源码:
功能还有待完善,距离完美的产品还有很大差距,不过希望对于初次接触JAVA的同学有所帮助;
定义的类:
定义的类
资源链接:
链接:https://pan.baidu.com/s/1UDvO2TnoQHt-dOr-qj4jDA
提取码:lisr
复制这段内容后打开百度网盘手机App,操作更方便哦

源码:
------------------------------------StartGame-------------------------------------

package gamepack;

import javax.swing.*;
import java.awt.*;

public class StartGame {
    public static void main(String[] args) {
        //1.构建窗体
        JFrame jf = new JFrame();
        //加窗体标题
        jf.setTitle("Plane Wars");
        //调用setBounds方法设置窗体的弹出位置,窗体大小
        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        int jfWidth = 490;
        int jfHeight = 720;
        jf.setBounds((screenWidth - jfWidth) / 2, (screenHeight - jfHeight) / 2, jfWidth, jfHeight);
        //将窗口设定为不可调节大小
        jf.setResizable(false);
        //窗口关闭的同时结束程序
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //2.创建一个面板
        GamePanel gp=new GamePanel();
        //将面板添加到窗体中
        jf.add(gp);
        //显示窗口
        jf.setVisible(true);
    }
}

---------------------------------------------------------------------------------------
------------------------------------GamePanel------------------------------------

package gamepack;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

/*
游戏面板:是一个透明的面板,所有的内容都是画在该面板中
继承了JPanel的功能
 */
public class GamePanel extends JPanel {
    int backgroundX;
    int backgroundY;

    PlaneClass myPlane=new MyPlane();

    int[] bombXs = new int[10];
    int[] bombYs = new int[10];

    //定义每一个炮弹的弧度
    //double[] degrees=new double[10];

    //游戏的两个状态
    boolean isStart = false;//默认游戏是暂停的

    //定义飞机飞的方向
    boolean left, right, up, down;
    //飞机是否碰撞
    boolean isDead = false;
    //定义游戏开始时间
    long startTime;
    //定义游戏结束时间
    long endTime;

    //定义飞机形态变化时间
    long changTime;

    //定义一个定时器
    Timer timer;

    ArrPlaneBox planeBox=new ArrPlaneBox();
    BulletBox bulletBox=new BulletBox();

    //初始化方法
    public void init() {
        //游戏开始
        startTime = System.currentTimeMillis();
        //初始化背景坐标
        backgroundX = 0;
        backgroundY = 0;

        planeBox.destroyAllPlane();
        bulletBox.destroyAllBullet();

        //初始化炮弹坐标
//        for (int i = 0; i < 10; i++) {
//            bombXs[i]=100;
//            bombYs[i]=100;
//            //给每一个炮弹设置随机的弧度
//            degrees[i]=(int)(Math.random()*2*Math.PI);
//        }
        planeBox.createPlane(3);
    }

    public GamePanel() {
        init();
        //将焦点放入面板
        this.setFocusable(true);
        //加入鼠标监听
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //获取鼠标点击
                int clickX=e.getX();
                int clickY=e.getY();
                if(clickX>0&&clickX<480&&clickY>10&&clickY<700){
                    if (isDead) {
                        init();
                        isDead = false;
                    } else {
                        isStart = !isStart;
                        //重新绘制页面
                        repaint();
                    }
                }
            }
        });
        //加入监听效果
        this.addKeyListener(new KeyAdapter() {
            //监听键盘的按压
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                //获取按键
                int keyCode = e.getKeyCode();
                //System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_SPACE) {//检测监听的是否是空格
                    if (isDead) {
                        init();
                        isDead = false;
                    } else {
                        isStart = !isStart;
                        //重新绘制页面
                        repaint();
                    }
                }
                if (keyCode == KeyEvent.VK_LEFT) {
                    left = true;
                }
                if (keyCode == KeyEvent.VK_RIGHT) {
                    right = true;
                }
                if (keyCode == KeyEvent.VK_UP) {
                    up = true;
                }
                if (keyCode == KeyEvent.VK_DOWN) {
                    down = true;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                //获取按键
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_LEFT) {
                    left = false;
                }
                if (keyCode == KeyEvent.VK_RIGHT) {
                    right = false;
                }
                if (keyCode == KeyEvent.VK_UP) {
                    up = false;
                }
                if (keyCode == KeyEvent.VK_DOWN) {
                    down = false;
                }
            }
        });
        //初始化定时器:每50ms执行一下actionPerformed中的逻辑
        timer = new Timer(50, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isStart && !isDead) {
                    backgroundY += 10;
                    if (backgroundY > 700) {
                        backgroundY = 0;
                    }
                    if (left) {
                        myPlane.planeX -= 7;
                    }
                    if (right) {
                        myPlane.planeX += 7;
                    }
                    if (up) {
                        myPlane.planeY -= 7;
                    }
                    if (down) {
                        myPlane.planeY += 7;
                    }

                    boolean flag=planeBox.isHitPlayerPlane(myPlane);
                    if(flag){
                        endTime=System.currentTimeMillis();
                        isDead=true;
                    }

                    planeBox.allPlaneMove();
                    planeBox.renewPlaneBlood(bulletBox.arrBulletList,bulletBox.bulletCount);
                    bulletBox.allBulletMove();
                    //改变坐标后,重新绘制
                    repaint();
                }
            }
        });
        timer.start();
    }

    //重写一个方法,用来在画板中添加图层
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //设置背景颜色
        //this.setBackground(new Color(255, 255, 255));
        changTime = System.currentTimeMillis();
        //给面板添加背景图片
        Images.groundImg.paintIcon(this, g, backgroundX, backgroundY);
        Images.groundImg.paintIcon(this, g, backgroundX, backgroundY - 700);
        if ((changTime - startTime) % 2 == 0) {
            myPlane.planeShow(this,g,1);
        } else {
            myPlane.planeShow(this,g,2);
        }
        planeBox.allPlaneShow(this,g,startTime,changTime);
        bulletBox.allBulletShow(this,g);

        if((changTime - startTime) /50 % 51 == 0){
            int index=new Random().nextInt(10);
            int planeSize=0;
            if(index<6){
                planeSize=3;
            }else if(index<9){
                planeSize=2;
            }else if(index<10){
                planeSize=1;
            }
            planeBox.createPlane(planeSize);
        }
        if((changTime - startTime) /50 % 2 == 0){
            bulletBox.createBullet(myPlane,true);
        }
//        for (int i = 0; i < 10; i++) {
//            Images.bombImg.paintIcon(this,g,bombXs[i],bombYs[i]);
//        }
        //销毁所有越界或则消亡飞机
        planeBox.destroyAllUselessPlane();
        bulletBox.destroyAllUselessBullet();

        if (isStart == false) {
            g.setColor(new Color(69, 91, 134));
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("点击开始游戏", 120, 330);
        }
        if (isDead == true) {
            g.setColor(new Color(69, 91, 134));
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("游戏结束" + (endTime - startTime) / 1000 + "秒", 100, 330);
        }
    }
}

---------------------------------------------------------------------------------------
--------------------------------------Images----------------------------------------

package gamepack;

import javax.swing.*;
import java.net.URL;

/*
图片工具类
 */
public class Images {
    //1.炸弹
    //先把图片的地址封装为一个具体的对象
    static URL bombURL=Images.class.getResource("/images/bomb.png");
    //把图片封装为一个对象
    static ImageIcon bombImg=new ImageIcon(bombURL);//定义为静态,直接通过Images直接调用
    //2.背景
    static URL groundURL=Images.class.getResource("/images/background.png");
    static ImageIcon groundImg=new ImageIcon(groundURL);
    //3.我的飞机
    static URL planeURL=Images.class.getResource("/images/me1.png");
    static ImageIcon planeImg=new ImageIcon(planeURL);
    static URL planeURL2=Images.class.getResource("/images/me2.png");
    static ImageIcon planeImg2=new ImageIcon(planeURL2);
    //4.敌人飞机
    static URL planeBigURL=Images.class.getResource("/images/enemy3_n1.png");
    static ImageIcon planeBigImg=new ImageIcon(planeBigURL);
    static URL planeBigURL2=Images.class.getResource("/images/enemy3_n2.png");
    static ImageIcon planeBigImg2=new ImageIcon(planeBigURL2);
    static URL planeBigURL3=Images.class.getResource("/images/enemy3_hit.png");
    static ImageIcon planeBigImg3=new ImageIcon(planeBigURL3);
    static URL planeBigURL4=Images.class.getResource("/images/enemy3_down1.png");
    static ImageIcon planeBigImg4=new ImageIcon(planeBigURL4);
    static URL planeBigURL5=Images.class.getResource("/images/enemy3_down2.png");
    static ImageIcon planeBigImg5=new ImageIcon(planeBigURL5);
    static URL planeBigURL6=Images.class.getResource("/images/enemy3_down3.png");
    static ImageIcon planeBigImg6=new ImageIcon(planeBigURL6);
    static URL planeBigURL7=Images.class.getResource("/images/enemy3_down4.png");
    static ImageIcon planeBigImg7=new ImageIcon(planeBigURL7);
    static URL planeBigURL8=Images.class.getResource("/images/enemy3_down5.png");
    static ImageIcon planeBigImg8=new ImageIcon(planeBigURL8);
    static URL planeBigURL9=Images.class.getResource("/images/enemy3_down6.png");
    static ImageIcon planeBigImg9=new ImageIcon(planeBigURL9);

    static URL planeMidURL=Images.class.getResource("/images/enemy2.png");
    static ImageIcon planeMidImg=new ImageIcon(planeMidURL);
    static URL planeMidURL2=Images.class.getResource("/images/enemy2_hit.png");
    static ImageIcon planeMidImg2=new ImageIcon(planeMidURL2);
    static URL planeMidURL3=Images.class.getResource("/images/enemy2_down1.png");
    static ImageIcon planeMidImg3=new ImageIcon(planeMidURL3);
    static URL planeMidURL4=Images.class.getResource("/images/enemy2_down2.png");
    static ImageIcon planeMidImg4=new ImageIcon(planeMidURL4);
    static URL planeMidURL5=Images.class.getResource("/images/enemy2_down3.png");
    static ImageIcon planeMidImg5=new ImageIcon(planeMidURL5);
    static URL planeMidURL6=Images.class.getResource("/images/enemy2_down4.png");
    static ImageIcon planeMidImg6=new ImageIcon(planeMidURL6);

    static URL planeSmallURL=Images.class.getResource("/images/enemy1.png");
    static ImageIcon planeSmallImg=new ImageIcon(planeSmallURL);
    static URL planeSmallURL2=Images.class.getResource("/images/enemy1_down1.png");
    static ImageIcon planeSmallImg2=new ImageIcon(planeSmallURL2);
    static URL planeSmallURL3=Images.class.getResource("/images/enemy1_down2.png");
    static ImageIcon planeSmallImg3=new ImageIcon(planeSmallURL3);
    static URL planeSmallURL4=Images.class.getResource("/images/enemy1_down3.png");
    static ImageIcon planeSmallImg4=new ImageIcon(planeSmallURL4);
    static URL planeSmallURL5=Images.class.getResource("/images/enemy1_down4.png");
    static ImageIcon planeSmallImg5=new ImageIcon(planeSmallURL5);
    //5.子弹加载
    static URL bulletURL=Images.class.getResource("/images/bullet1.png");
    static ImageIcon bulletImg=new ImageIcon(bulletURL);
    static URL bulletURL2=Images.class.getResource("/images/bullet2.png");
    static ImageIcon bulletImg2=new ImageIcon(bulletURL2);
}

---------------------------------------------------------------------------------------
------------------------------------ArrPlaneBox-----------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;

public class ArrPlaneBox {
    public int planeCount = 0;
    public ArrayList<PlaneClass> arrPlaneList = new ArrayList<>();

    public void allPlaneShow(GamePanel p, Graphics g, long startTime, long changeTime) {
        //int arrSize= arrPlaneList.size();
        for (int i = 0; i < planeCount; i++) {
            if (arrPlaneList.get(i).speed / 2 == 1) {
                if ((changeTime - startTime) % 2 == 0) {
                    arrPlaneList.get(i).planeShow(p, g, 1);
                } else {
                    arrPlaneList.get(i).planeShow(p, g, 2);
                }
            } else {
                arrPlaneList.get(i).planeShow(p, g, 1);
            }
        }
    }

    public void allPlaneMove() {
        //int arrSize= arrPlaneList.size();
        for (int i = 0; i < planeCount; i++) {
            arrPlaneList.get(i).move();
        }
    }

    public void createPlane(int planeId) {
        planeCount++;
        switch (planeId) {
            case 1:
                arrPlaneList.add(new PlaneBig());
                break;
            case 2:
                arrPlaneList.add(new PlaneMid());
                break;
            default:
                arrPlaneList.add(new PlaneSmall());
                break;
        }
    }

    public void destroyAllUselessPlane() {
        //int arrSize= arrPlaneList.size();
        for (int i = 0; i < planeCount; i++) {
            if (arrPlaneList.get(i).planeY > 700 || arrPlaneList.get(i).isPlaneDead) {
                planeCount--;
                arrPlaneList.get(i).finalize();
                arrPlaneList.remove(i);
            }
        }
    }

    public boolean isHitPlayerPlane(PlaneClass plane) {
        for (int i = 0; i < planeCount; i++) {
            if (arrPlaneList.get(i).isHitPlayerPlane(plane)) {
                return true;
            }
        }
        return false;
    }

    public void renewPlaneBlood(ArrayList<BulletClass> bulletList, int bulletCount) {
        for (int i = 0; i < planeCount; i++) {
            if (arrPlaneList.get(i).isBulletHitPlane(bulletList, bulletCount)) {
                arrPlaneList.get(i).blood--;
                if (arrPlaneList.get(i).blood < 1) {
                    arrPlaneList.get(i).isPlaneDead = true;
                }
            }
        }
    }

    public void destroyAllPlane() {
        //int arrSize= arrPlaneList.size();
        for (int i = 0; i < planeCount; i++) {
            planeCount--;
            arrPlaneList.get(i).finalize();
            arrPlaneList.remove(i);
        }
    }
}

---------------------------------------------------------------------------------------
------------------------------------PlaneClass-------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;

public abstract class PlaneClass {
    //位置坐标
    public int planeX;
    public int planeY;
    //尺寸参数
    public int width;
    public int height;
    //行为参数
    public int speed;    //timer采集频率为50ms
    //属性参数
    public int blood;
    public boolean isPlaneDead;

    protected void finalize(){
    }
    public abstract void init();
    public abstract void move();
    public abstract void planeShow(GamePanel p,Graphics g,int showId);
    public abstract boolean isHitPlayerPlane(PlaneClass plane);
    public abstract boolean isBulletHitPlane(ArrayList<BulletClass> bulletList,int bulletCount);
}

---------------------------------------------------------------------------------------
-------------------------------------MyPlane---------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class MyPlane extends PlaneClass{
    public MyPlane() {
        init();
    }

    @Override
    public void init() {
        //位置坐标
        planeX=200;
        planeY=580;
        //尺寸参数
        width=102;
        height=126;
        //行为参数
        //speed=4;    //timer采集频率为50ms
        //属性参数
        blood=15;
        isPlaneDead=false;
    }
    @Override
    protected void finalize(){
        init();
    }

    @Override
    public void move() {

    }

    @Override
    public void planeShow(GamePanel p, Graphics g, int showId) {
        if (showId==1) {
            Images.planeImg.paintIcon(p, g, planeX, planeY);
        } else {
            Images.planeImg2.paintIcon(p, g, planeX, planeY);
        }
    }

    @Override
    public boolean isHitPlayerPlane(PlaneClass plane) {
        return false;
    }

    @Override
    public boolean isBulletHitPlane(ArrayList<BulletClass> bulletList,int bulletCount) {
        return false;
    }
}

---------------------------------------------------------------------------------------
-------------------------------------PlaneBig--------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class PlaneBig extends PlaneClass{
    public PlaneBig() {
        init();
    }

    @Override
    public void init() {
        //位置坐标
        planeX=new Random().nextInt(480-169);
        planeY=-260;
        //尺寸参数
        width=169;
        height=258;
        //行为参数
        speed=2;    //timer采集频率为50ms
        //属性参数
        blood=10;
        isPlaneDead=false;
    }
    @Override
    protected void finalize(){
        init();
    }

    @Override
    public void move() {
        planeY+=speed;
    }

    @Override
    public void planeShow(GamePanel p,Graphics g,int showId) {
        switch (showId){
            case 1:
                Images.planeBigImg.paintIcon(p, g, planeX, planeY);
                break;
            case 2:
                Images.planeBigImg2.paintIcon(p, g, planeX, planeY);
                break;
            case 3:
                Images.planeBigImg3.paintIcon(p, g, planeX, planeY);
                break;
            case 4:
                Images.planeBigImg4.paintIcon(p, g, planeX, planeY);
                break;
            case 5:
                Images.planeBigImg5.paintIcon(p, g, planeX, planeY);
                break;
            case 6:
                Images.planeBigImg6.paintIcon(p, g, planeX, planeY);
                break;
            case 7:
                Images.planeBigImg7.paintIcon(p, g, planeX, planeY);
                break;
            case 8:
                Images.planeBigImg8.paintIcon(p, g, planeX, planeY);
                break;
            case 9:
                Images.planeBigImg9.paintIcon(p, g, planeX, planeY);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean isHitPlayerPlane(PlaneClass plane) {
        boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                new Rectangle(plane.planeX, plane.planeY, plane.width, plane.height));
        return flag;
    }

    @Override
    public boolean isBulletHitPlane(ArrayList<BulletClass> bulletList,int bulletCount) {
        for (int i = 0; i < bulletCount; i++) {
            boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                    new Rectangle(bulletList.get(i).BulletX, bulletList.get(i).BulletY,
                            bulletList.get(i).width, bulletList.get(i).height));
            if(flag) {
                bulletList.get(i).isHitPlane = true;
                return true;
            }
        }
        return false;
    }
}

---------------------------------------------------------------------------------------
-------------------------------------PlaneMid--------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class PlaneMid extends PlaneClass{
    public PlaneMid() {
        init();
    }

    @Override
    public void init() {
        //位置坐标
        planeX=new Random().nextInt(480-69);
        planeY=-99;
        //尺寸参数
        width=69;
        height=99;
        //行为参数
        speed=4;    //timer采集频率为50ms
        //属性参数
        blood=5;
        isPlaneDead=false;
    }
    @Override
    protected void finalize(){
        init();
    }

    @Override
    public void move() {
        planeY+=speed;
    }

    @Override
    public void planeShow(GamePanel p,Graphics g,int showId) {
        switch (showId){
            case 1:
                Images.planeMidImg.paintIcon(p, g, planeX, planeY);
                break;
            case 2:
                Images.planeMidImg2.paintIcon(p, g, planeX, planeY);
                break;
            case 3:
                Images.planeMidImg3.paintIcon(p, g, planeX, planeY);
                break;
            case 4:
                Images.planeMidImg4.paintIcon(p, g, planeX, planeY);
                break;
            case 5:
                Images.planeMidImg5.paintIcon(p, g, planeX, planeY);
                break;
            case 6:
                Images.planeMidImg6.paintIcon(p, g, planeX, planeY);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean isHitPlayerPlane(PlaneClass plane) {
        boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                new Rectangle(plane.planeX, plane.planeY, plane.width, plane.height));
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean isBulletHitPlane(ArrayList<BulletClass> bulletList,int bulletCount) {
        for (int i = 0; i < bulletCount; i++) {
            boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                    new Rectangle(bulletList.get(i).BulletX, bulletList.get(i).BulletY,
                            bulletList.get(i).width, bulletList.get(i).height));
            if(flag) {
                bulletList.get(i).isHitPlane = true;
                return true;
            }
        }
        return false;
    }
}

---------------------------------------------------------------------------------------
-----------------------------------PlaneSmall--------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class PlaneSmall extends PlaneClass{
    public PlaneSmall() {
        init();
    }

    @Override
    public void init() {
        //位置坐标
        planeX=new Random().nextInt(480-57);
        planeY=-43;
        //尺寸参数
        width=57;
        height=43;
        //行为参数
        speed=6;    //timer采集频率为50ms
        //属性参数
        blood=2;
        isPlaneDead=false;
    }
    @Override
    protected void finalize(){
        init();
    }

    @Override
    public void move() {
        planeY+=speed;
    }

    @Override
    public void planeShow(GamePanel p,Graphics g,int showId) {
        switch (showId){
            case 1:
                Images.planeSmallImg.paintIcon(p, g, planeX, planeY);
                break;
            case 2:
                Images.planeSmallImg2.paintIcon(p, g, planeX, planeY);
                break;
            case 3:
                Images.planeSmallImg3.paintIcon(p, g, planeX, planeY);
                break;
            case 4:
                Images.planeSmallImg4.paintIcon(p, g, planeX, planeY);
                break;
            case 5:
                Images.planeSmallImg5.paintIcon(p, g, planeX, planeY);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean isHitPlayerPlane(PlaneClass plane) {
        boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                new Rectangle(plane.planeX, plane.planeY, plane.width, plane.height));
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean isBulletHitPlane(ArrayList<BulletClass> bulletList,int bulletCount) {
        for (int i = 0; i < bulletCount; i++) {
            boolean flag=new Rectangle(planeX,planeY,width,height).intersects(
                    new Rectangle(bulletList.get(i).BulletX, bulletList.get(i).BulletY,
                            bulletList.get(i).width, bulletList.get(i).height));
            if(flag) {
                bulletList.get(i).isHitPlane = true;
                return true;
            }
        }
        return false;
    }
}

---------------------------------------------------------------------------------------
-------------------------------------BulletBox--------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;

public class BulletBox {
    public int bulletCount = 0;
    public ArrayList<BulletClass> arrBulletList = new ArrayList<>();

    public void allBulletShow(GamePanel p, Graphics g) {
        for (int i = 0; i < bulletCount; i++) {
            arrBulletList.get(i).bulletShow(p, g);
        }
    }

    public void allBulletMove() {
        //int arrSize= arrBulletList.size();
        for (int i = 0; i < bulletCount; i++) {
            arrBulletList.get(i).move();
        }
    }

    public void createBullet(PlaneClass plane, boolean flag) {
        bulletCount++;
        arrBulletList.add(new BulletClass(plane, flag));
    }

    public void destroyAllUselessBullet() {
        for (int i = 0; i < bulletCount; i++) {
            if (arrBulletList.get(i).BulletY < -11 || arrBulletList.get(i).BulletY > 700 || arrBulletList.get(i).isHitPlane) {
                bulletCount--;
                arrBulletList.get(i).finalize();
                arrBulletList.remove(i);
            }
        }
    }

    public void destroyAllBullet() {
        for (int i = 0; i < bulletCount; i++) {
            bulletCount--;
            arrBulletList.get(i).finalize();
            arrBulletList.remove(i);
        }
    }
}

---------------------------------------------------------------------------------------
-----------------------------------BulletClass--------------------------------------

package gamepack;

import java.awt.*;
import java.util.ArrayList;

public class BulletClass {
    //位置坐标
    public int BulletX;
    public int BulletY;
    //尺寸参数
    public int width;
    public int height;
    //行为参数
    public int speed;    //timer采集频率为50ms
    //属性参数
    public boolean isHitPlane;
    public boolean isMyPlane;

    public BulletClass(PlaneClass plane,boolean flag) {
        init(plane,flag);
    }

    protected void finalize(){
        this.BulletX = -width;
        this.BulletY = -height;
    }
    public void init(PlaneClass plane,boolean flag){
        if(flag){
            this.BulletX = plane.planeX+ plane.width/2;
            this.BulletY = plane.planeY;
            this.speed = 45;
        }else{
            this.BulletX = plane.planeX+plane.width/2;
            this.BulletY = plane.planeY+plane.height;
            this.speed = -45;
        }
        this.width = 5;
        this.height = 11;
        this.isMyPlane = flag;
        this.isHitPlane = false;
    }
    public void move(){
        if(isMyPlane){
            this.BulletY -=speed;
        }else{
            this.BulletY +=speed;
        }
    }
    public void bulletShow(GamePanel p, Graphics g){
        if(isMyPlane){
            Images.bulletImg.paintIcon(p, g, BulletX, BulletY);
        }else{
            Images.bulletImg2.paintIcon(p, g, BulletX, BulletY);
        }
    }
}

---------------------------------------------------------------------------------------
逻辑和代码重复性的问题还有待优化,交给你们了,朋友们!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值