Java飞机大战教程(大一课程设计)

 

      包含知识点:窗口绘制 画图及双缓冲 碰撞检测 集合类的应用 鼠标键盘监听 碰撞检测

窗口绘制 画图及双缓冲 作用:显示游戏画面

集合类的应用: 用来存取需要画的元素(如飞机、敌机、子弹、飞机技能、爆炸效果等)

鼠标键盘监听:主要用来对游戏的控制(如飞机的移动,游戏的开始与暂停等)

碰撞检测:可判断飞机与敌机,子弹等的碰撞来做进一步的处理

然后来看代码及注释吧

绘制窗口的代码

public class Win extends JFrame {//建一个类继承JFrame来画图

     Win frame ;//声明对象frame
public void lunch(){
    this.setSize(123,322);//设置窗口大小
    this.setLocationRelativeTo(null);//设置窗口为中央位置
    this.setTitle("设置窗口");//设置窗口标题
    this.setVisible(true);//使窗口显现
}

}

beijinglei

stati表是游戏状态

  //游戏状态 0未开始 1游戏运行 2暂停 3失败 4成功
static    int state=0;
    public void paint(Graphics g){
        count++;//绘制次数
        if(offScreenImage==null){//双缓冲图片为空就给图片添加图象

            offScreenImage=this.createImage(700,900);//创建双缓冲图片的大小
        }
        //获得该图片的画布
       Graphics gImage=offScreenImage.getGraphics();
        //填充每个画布
        gImage.fillRect(0,0,700,900);
        if(state==0){
            gImage.drawImage(aaa,0,0,null);
            //改变字体颜色
            gImage.setColor(Color.WHITE);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("开始游戏",260,450);
        }
        if(state==1){
            objectList.removeAll(removeList);//删除集合removeAll里的烁有元素
            for (Gameplane object:objectList){
                object.paintSet(gImage);//用画笔画集合中的所有元素
            }
        }
        if(state==3){
            //改变字体颜色
            gImage.setColor(Color.RED);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏结束",260,450);
        }
        if(state==4){
            gImage.drawImage(aaa,0,0,null);//画结束时的画面
            //改变字体颜色
            gImage.setColor(Color.GREEN);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏通关",260,450);
        }
        //绘画爆炸效果图
//            gImage.drawImage(bbb, b0mb_x, b0mb_y, null);
        if(state==2){
            //改变字体颜色
            gImage.setColor(Color.WHITE);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏暂停",260,450);
        }
        //将缓冲区绘制好的图形整个绘制到·画布中
    g.drawImage(offScreenImage,0,0,null);}

碰撞检测段

/*检测矩形是否相交之后在花括号做具体处理,如将碰撞物移除,划爆炸等
具体在后面的代码里会有解释
*/
 if(this.getRec().intersects(enemyObj.getRec())){
}

public  Rectangle getRec(){//先获得对象(如和飞机、子弹等一样大的矩形)

        return new Rectangle(x,y,25,25);//定义矩形大小及坐标
    }
}

鼠标键盘监听主要代码

//键盘按键添加例子
  this.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    super.keyPressed(e);
                    if(e.getKeyCode()==81){
if(PlaneSkill.skill==20)
{ planeSkill.speed=12;
                        PlaneSkill.skill=0;
                        objectList.add(planeSkill);}
                    }
                }
            });AAA 

//返回键盘值e.getKeyCode()
//鼠标按键添加例子
this.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
     }
});
//返回鼠标按键值mouseClicked.getButton

主类win的代码

import javax.swing.*;
import java.awt.*;
import java.math.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
public class win extends JFrame {
   static int score=0;//所得分数
    int enemyCount=0;//产生的敌机数量
    //游戏重绘次数
    int count=1;
    //游戏状态 0未开始 1游戏运行 2暂停 3失败 4成功
 static    int state=0;

   List<Drop> dropList=new ArrayList<>();
    //被删除物体的集合
    List<Gameplane> removeList=new ArrayList<>();
    //敌方战机集合
    List<EnemyObj> enemyObjs=new ArrayList<>();
    //技能集合
    List<PlaneSkill> planeSkills=new ArrayList<>();
    //我方子弹集合
    List<Gameplane> objectList=new ArrayList<>();
    List<BulletObj> bulletObjList=new ArrayList<>();
    List<BulletObj2> bulletObj2List=new ArrayList<>();
    //boss的子弹的集合
    List<BulletObj1> bulletObj1List=new ArrayList<>();
    //技能集合
    List<PlaneSkill> planeSkillList=new ArrayList<>();
    //boss
    EnemyObj1 enemyObj1=null;
    //飞机个体
PlaneSkill planeSkill=null;
        PlaneObj planeobj = new PlaneObj("D:/Plane/Plane/src/image/plane.png", 150, 400, 0, this);

    //游戏背景
    BgObj bgobj=new BgObj("D:/Plane/Plane/src/image/bg3.jpg",0,-800,2.0,this);
    //子弹
    BulletObj bulletobj=new BulletObj("D:/Plane/Plane/src/image/die04.png",planeobj.x+30,planeobj.y-40,3,this);
BulletObj bulletObj1;
    /*在学习绘图中,如果需要绘制一张图片,则需要获取这张图片通过Toolkit进行获取
    在getImage()方法中,需要填写图片的路径*/
    Image aaa=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/bg3.jpg");//导入图片

    //游戏失败图片
    Image ccc=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/bg11.webp");
    //定义双缓冲图片
    Image offScreenImage=null;

    public void lunch(){
        //将飞机及背景加入到集合中
    objectList.add(bgobj);
    objectList.add(planeobj);

        this.setTitle("飞机大战");
//为窗口添加开始鼠标事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //System.out.println(e.getButton());
                //鼠标左键为1
                if(e.getButton()==1&&state==0) {//e.getButton()返回的为按键的数值   如果游戏状态为·0,且按下鼠标左键就会开始游戏
                    state = 1;
                }
if((e.getButton()==3)&&(state==3||state==4)) {//如果游戏失败或游戏通关·点击鼠标右键就会将游戏状态初始化
    {
      win.score=0;//重置游戏分数】‘
         win.state=0;//重置游戏状态
    enemyCount=0;
    count=0;
planeobj.x=150;
planeobj.y=400;
    planeobj.life=3;
    planeSkill.skill=0;
    Drop.num1=0;
        for (BulletObj bulletObj: bulletObjList )//将屏幕上的我方飞机子弹移除并移除集合
        {
            bulletObj.x=-100;
            removeList.add(bulletobj);
        }
        for (BulletObj1 bulletObj1: bulletObj1List )//将屏幕上的敌方飞机子弹移除并移除集合
        {
            bulletObj1.x=-100;
            removeList.add((enemyObj1));
        }

        for (EnemyObj enemyObj: enemyObjs )//将屏幕上的敌方飞机移除并移除集合
        {
            enemyObj.x=-100;
        }
        enemyObj1=null;//使敌方boss为空不出场
        bulletObj1List.clear();//清空子弹集合

        objectList.removeAll(removeList);//在·大集合上清除元素


}}
repaint();//进入画图方法
            }
        });
        //为游戏添加一个暂停事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                //空格按下
                if(e.getKeyCode()==32){
                    switch (state){
                        case 1:
                            state=2;
                            break;
                        case 2:
                            state=1;
                            break;
                    }
                }
            }
        });
//        this.setLocationRelativeTo(null);//使窗口在屏幕中央位置
        this.setSize(700,900);//设置窗口大小
        this.setVisible(true);//使窗口显现
        while(true){

            if(state==1){
                createObj();

                repaint();//调用update()清除当前显示并再调用paint()方法
            }
            try{//使循环刷新页面减速
                Thread.sleep(25);
            }catch (Exception e){
e.printStackTrace();
            }
        }
    }
    @Override
    public void paint(Graphics g){
        count++;//绘制次数
        if(offScreenImage==null){//双缓冲图片为空就给图片添加图象

            offScreenImage=this.createImage(700,900);//创建双缓冲图片的大小
        }
        //获得该图片的画布
       Graphics gImage=offScreenImage.getGraphics();
        //填充每个画布
        gImage.fillRect(0,0,700,900);
        if(state==0){
            gImage.drawImage(aaa,0,0,null);
            //改变字体颜色
            gImage.setColor(Color.WHITE);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("开始游戏",260,450);
        }
        if(state==1){
            objectList.removeAll(removeList);//删除集合removeAll里的烁有元素
            for (Gameplane object:objectList){
                object.paintSet(gImage);//用画笔画集合中的所有元素
            }
        }
        if(state==3){
            //改变字体颜色
            gImage.setColor(Color.RED);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏结束",260,450);
        }
        if(state==4){
            gImage.drawImage(aaa,0,0,null);//画结束时的画面
            //改变字体颜色
            gImage.setColor(Color.GREEN);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏通关",260,450);
        }
        //绘画爆炸效果图
//            gImage.drawImage(bbb, b0mb_x, b0mb_y, null);
        if(state==2){
            //改变字体颜色
            gImage.setColor(Color.WHITE);
            //改变文字格式
            gImage.setFont(new Font("仿宋",Font.BOLD,40));
            //显示文字
            gImage.drawString("游戏暂停",260,450);
        }
        //将缓冲区绘制好的图形整个绘制到·画布中
    g.drawImage(offScreenImage,0,0,null);}
   // 装填子弹或敌机
   boolean un=true;
    public void createObj() {
        if(Drop.num1>=2&&un) {
            objectList.remove(planeobj);
            planeobj = new PlaneObj("D:/Plane/Plane/src/image/plane1.png", 150, 400, 0, this);
            objectList.add(planeobj);
            un=false;
        }
        if(Drop.num1<2&&!un)
        {
            objectList.remove(planeobj);
            planeobj = new PlaneObj("D:/Plane/Plane/src/image/plane.png", 150, 400, 0, this);
            objectList.add(planeobj);
            un=true;
        }
        if(count%400==0){

            dropList.add(new Drop("D:/Plane/Plane/src/image/sky.png",4,this));
            objectList.add(dropList.get(dropList.size()-1));
        }
        //控制子弹生成速度
        if (count % 8 == 0) {
            if(Drop.num1<2) {
                bulletObjList.add(new BulletObj("D:/Plane/Plane/src/image/die04.png", planeobj.x + 30, planeobj.y - 35, 8, this));
                objectList.add(bulletObjList.get(bulletObjList.size() - 1));
            }
            else
            {
                bulletObjList.add(new BulletObj("D:/Plane/Plane/src/image/die04.png", planeobj.x+10 , planeobj.y - 35, 8, this));
                objectList.add(bulletObjList.get(bulletObjList.size() - 1));

                bulletObj2List.add(new BulletObj2("D:/Plane/Plane/src/image/die04.png", planeobj.x+90 , planeobj.y - 35, 8, this));

                objectList.add(bulletObj2List.get(bulletObj2List.size() - 1));
            }
        }
        if (count % 15 == 0&&enemyObj1==null) {//控制敌方飞机生成
            int p=(int)(1+Math.random()*5);
            enemyObjs.add(new EnemyObj("D:/Plane/Plane/src/image/enemy0"+p+".png", 4, this));
            objectList.add(enemyObjs.get(enemyObjs.size() - 1));
            enemyCount++;
//            System.out.println(enemyCount);

        }
        if (PlaneSkill.skill == 20) {//技能产生
          planeSkill =(new PlaneSkill("D:/Plane/Plane/src/image/bomb12.png",0,700,0,this));

            this.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    super.keyPressed(e);
                    if(e.getKeyCode()==81){
if(PlaneSkill.skill==20)
{ planeSkill.speed=12;
                        PlaneSkill.skill=0;
                        objectList.add(planeSkill);}
                    }
                }
            });
        }
        if (count % 24 == 0 && enemyObj1 != null&&enemyObj1.life>0){//控制boss子弹产生
            bulletObj1List.add(new BulletObj1("D:/Plane/Plane/src/image/bluecircleimg.png", enemyObj1.x + 111, enemyObj1.y + 206, 10, this));
            objectList.add(bulletObj1List.get(bulletObj1List.size() - 1));}
                if (enemyCount >= 80) {
                    if(enemyObj1==null){
                     enemyObj1 = new EnemyObj1("D:/Plane/Plane/src/image/enemy09.png", 0, 50, 3, this);
                    objectList.add(enemyObj1);}
                }
    }
    public static void main(String[] args){
        win game=new win();
        game.lunch();
    }
}

所有物体图象(飞机、敌机、boss、子弹、技能等)的父类GamePlane类

GamePlane是个抽象类主要是规定了物体图像的相关属性(如图象、位置、速度、矩形大小)

import java.awt.*;
public abstract class Gameplane {
  Image img;
  //游戏元素的横坐标
  int x;
  //游戏元素的纵坐标
  int y;
  //游戏元素的宽;
  int width;
  //游戏元素的高
  int height;
  //游戏元素的速度
  double speed;
    //引入主界面
  win frame;
  //无参构造方法
  public Gameplane(){

  }
  //构造方法
  public Gameplane(String img,int x,int y,double speed,win frame){
    this.img=Toolkit.getDefaultToolkit().getImage(img);
    this.x=x;
    this.y=y;
    this.speed=speed;
    this.frame=frame;
  }
//构造方法
  public Gameplane(String img,win frame){
    this.img=Toolkit.getDefaultToolkit().getImage(img);
    this.frame=frame;
  }
  //构造方法
  public Gameplane(String img,int x,int y,int width,int height,double speed){
    this.img=Toolkit.getDefaultToolkit().getImage(img);
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height;
    this.speed=speed;
  }
  //返回图片
  public Image getImg(){
    return img;

  }
  //用img获取图片
public void setImg(String img){
    this.img=Toolkit.getDefaultToolkit().getImage(img);
}
//继承元素绘制自己的方法
public abstract  void paintSet(Graphics g);
  //获取当前元素矩形,是为碰撞检测而写
  public abstract Rectangle getRec();


}

之后的游戏背景类、飞机类、敌机类、子弹类、技能类都是GamePlane的之类,对其进行实例化·

背景类:bgObj类

import java.awt.*;

public class BgObj extends Gameplane {
    public BgObj() {
        super();
    }
//构造方法
    public BgObj(String img, int x, int y, double speed, win frame) {
        super(img, x, y, speed, frame);
    }
//画图方法主要是对图像的处理
    public void paintSet(Graphics g) {
g.drawImage(img,x,y,null);//将背景图像画在画布上
y+=1;//图象y坐标向前移动,即背景后移,由相对位置可得等于飞机在向前移动
if(y>=-10)//图片的循环移动
   y=-800;
/*在屏幕上显示得分情况+

 */
        g.setFont(new Font("仿宋",Font.BOLD,20));

g.setColor(Color.green);
g.drawString("得分:"+this.frame.score,10,50);
    }
//获取物体矩形的方法
  public Rectangle getRec() {
return null;//背景不需要碰撞检测所以返回值为空
   }

}

飞机类PlanObj类

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PlaneObj extends Gameplane{
    int life=3;//设置飞机的生命值
    public PlaneObj(){
        super();
    }
    public PlaneObj(String img,int x,int y,double speed,win frame){
        this.img=Toolkit.getDefaultToolkit().getImage(img);
        this.x=x;
        this.y=y;
        this.speed=speed;
        this.frame=frame;
    }
    public PlaneObj(String img, int x, int y, int width,int height,double speed) {
        super(img, x, y, width,height,speed);
    }
    public   void paintSet(Graphics g){
        g.drawImage(img,x,y,null);//画出飞机图片
        //飞机随着鼠标的;移动
        this.frame.addMouseMotionListener(new MouseAdapter() {
           @Override
            public void mouseMoved(MouseEvent e){//鼠标监听事件

                x=e.getX()-52;//e.getX()返回值为鼠标坐标
              //两个if作用为防止飞机随鼠标移出窗口
                if(x<=0)
                    x=0;
                if(x>580)
                    x=600;
                y=e.getY()-41;//e.getY()返回值为鼠标坐标

            }

        });
        //在画布上显示飞机生命值PH以及飞机吃到的空头数量Num
        g.setFont(new Font("仿宋",Font.BOLD,20));
        g.setColor(Color.green);
        g.drawString("Plane HP:"+this.life,10,130);
g.drawString("Drop Num:"+Drop.num1,10,150);


for (BulletObj1 bulletObj1: this.frame.bulletObj1List){//遍历boss的每个子弹
    if(this.getRec().intersects(bulletObj1.getRec()))//如果boss子弹和我方飞机碰撞结果为true
    {

bulletObj1.x=-100;//将子弹移除窗口
        this.frame.removeList.add(bulletObj1);//将子弹从画布上删除
        /*飞机与敌方子弹碰撞后,如果有空投空投数量减一
        如果空投数为零那么飞机生命减一
         */
        if(Drop.num1>0)
            Drop.num1--;
        else
life--;
        if(life==0)
            frame.state=3;
    }
}
    }
    //获取当前元素矩形,是为碰撞检测而写
    public Rectangle getRec(){
        return new Rectangle(x,y,100,80);//矩形的坐标即飞机坐标,矩形大小和飞机大小一样

    }

}

敌机EnemyObj类

import java.math.*;
import java.awt.*;

public class EnemyObj extends Gameplane{

    int y=40;

    public EnemyObj(){
        super();
    }
    public EnemyObj(String img,double speed,win frame){
        this.img= Toolkit.getDefaultToolkit().getImage(img);

        this.speed=speed;
        this.frame=frame;
    }
    public void setImg(String img){
        this.img=Toolkit.getDefaultToolkit().getImage(img);
    }
    //继承元素绘制自己的方法
    int x=(int)(Math.random()*5*100);
    public  void paintSet(Graphics g){
        g.drawImage(img,x,y,null);
        y+=speed;

            if(this.getRec().intersects(this.frame.planeobj.getRec())){//检测矩形是否相交
               this.x=-100;
               this.frame.removeList.add(this);
                if(Drop.num1>0)
                    Drop.num1--;
                else
                    this.frame.planeobj.life--;
            if(this.frame.planeobj.life<=0)//当飞机生名值为零是游戏状态state变为3游戏结束
           this.frame.state=3;
            }

    }
    //获取当前元素矩形,是为碰撞检测而写
    public  Rectangle getRec(){
        return new Rectangle(x,y,98,70);
    }

}

子弹类

import java.awt.*;

public class BulletObj extends Gameplane{


    public BulletObj(String img,int x,int y,double speed,win frame){
        this.img=Toolkit.getDefaultToolkit().getImage(img);
        this.x=x;
        this.y=y;
        this.speed=speed;
        this.frame=frame;
    }
    public   void paintSet(Graphics g){
g.drawImage(img,x,y,null);//画图
y-=speed;//子弹移动
        for (EnemyObj enemyObj:this.frame.enemyObjs){//遍历敌机
            if(this.getRec().intersects(enemyObj.getRec())){//检测矩形是否相交
             //画爆炸图
              for(int i=1;i<=7;i++)
              {this.img=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/explode/"+i+".gif");
                  g.drawImage(img,this.x,this.y-30,null);

              }
                this.x=-100;
                enemyObj.x=-200;
                this.frame.score++;//分数加1
                if(PlaneSkill.skill<20)
                PlaneSkill.skill++;//技能的能量条加一
                /*移除子弹和敌机

                 */
                this.frame.removeList.add(enemyObj);
                this.frame.removeList.add(this);
            }
        }
        g.setFont(new Font("仿宋",Font.BOLD,30));
        g.setColor(Color.green);
        g.drawString("SPR:",10,100);
        if(PlaneSkill.skill==20)
            g.setColor(Color.RED);
        else
            g.setColor(Color.YELLOW);
        System.out.println(PlaneSkill.skill);
        g.fillRect(80,80,PlaneSkill.skill*10,25);
    }
    //获取当前元素矩形,是为碰撞检测而写
    public  Rectangle getRec(){

        return new Rectangle(x,y,25,25);//定义矩形大小及坐标
    }
}

空投类(飞机吃两个及两个以上空头会进入升级形态)

import java.awt.*;

public class Drop   extends Gameplane{

    int speed1=4;
 static int  num1=0;
    int y=40;

    public Drop(){
        super();
    }
    public Drop(String img,double speed,win frame){
        this.img= Toolkit.getDefaultToolkit().getImage(img);

        this.speed=speed;
        this.frame=frame;
    }
    public void setImg(String img){
        this.img=Toolkit.getDefaultToolkit().getImage(img);
    }
    //继承元素绘制自己的方法
    int x=(int)(Math.random()*5*100);//空投随机产生的位置
    public  void paintSet(Graphics g){
        g.drawImage(img,x,y,null);
        y+=speed;
        /*空投会左右移动,到窗口边缘时会改变速度方向
       
         */
       if(x<0)
           speed1=-speed1;
       if(x>630)
           speed1=-speed1;
x+=speed1;
//碰撞检测
        if(this.getRec().intersects(this.frame.planeobj.getRec())){//检测矩形是否相交
            this.x=-100;
            this.frame.removeList.add(this);
num1++;


        }

    }
    //获取当前元素矩形,是为碰撞检测而写
    public  Rectangle getRec(){
        return new Rectangle(x,y,70,70);
    }

}


 下面的几个类和上面写过的几个类相似所以就没给注释

技能类

public class PlaneSkill extends Gameplane {
    static int skill=0;
    Image image2;
    public PlaneSkill() {
        super();
    }
    public PlaneSkill(String img, int x, int y, double speed, win frame) {
        super(img, x, y, speed, frame);
    }

    public void paintSet(Graphics g) {
        g.drawImage(img, x, y, null);
y-=speed;
        for ( EnemyObj enemyObj: this.frame.enemyObjs){
            if(this.getRec().intersects(enemyObj.getRec())) {
                for(int i=1;i<=7;i++)
                {this.image2=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/explode/"+i+".gif");
                    g.drawImage(image2,enemyObj.x+30,enemyObj.y+30,null);

                }
                enemyObj.x = -100;
                this.frame.removeList.add(enemyObj);
                if(this.y==-600)
                this.frame.removeList.add(this);
            }       }
    }

    public Rectangle getRec() {

        return new Rectangle(x,y,700,103);
    }
}

敌方boss类

public class EnemyObj1 extends Gameplane {
   int life=50;
   Image image1;//另外声明一个接收图像的image1
    public EnemyObj1() {
        super();
    }
    public EnemyObj1(String img,int x,int y, double speed, win frame) {
        this.img = Toolkit.getDefaultToolkit().getImage(img);
this.x=x;
this.y=y;
        this.speed = speed;
        this.frame = frame;
    }
int b=0;
//定义表示boss血量的宽高
    int weight;
    int height=20;
    public void paintSet(Graphics g) {
        g.drawImage(img, x, y, null);

if(x>=450)
{
speed=-speed;
}
if(x<0)
{
    speed=-speed;
}
x+=speed;
if(life<0){
    speed=0;
    y-=5 ;
}
for (BulletObj bulletObj:this.frame.bulletObjList){
    if(this.getRec().intersects(bulletObj.getRec()))
    {for(int i=1;i<8;i++){
        this.image1=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/explode/"+i+".gif");
        g.drawImage(image1,bulletObj.x,bulletObj.y-50,null);
      }
        bulletObj.x=-100;
        this.frame.removeList.add(bulletObj);
        life--;
        if(life==1)
            b=this.frame.count;
        if(life<=0) {

         if(this.frame.count-b>120)
            this.frame.state = 4;
        }
    }
}
        for (BulletObj2 bulletObj:this.frame.bulletObj2List){
            if(this.getRec().intersects(bulletObj.getRec()))
            {for(int i=1;i<8;i++){
                this.image1=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/explode/"+i+".gif");
                g.drawImage(image1,bulletObj.x,bulletObj.y-50,null);
            }
                bulletObj.x=-100;
                this.frame.removeList.add(bulletObj);
                life--;
                if(life==1)
                    b=this.frame.count;
                if(life<=0) {

                    if(this.frame.count-b>120)
                        this.frame.state = 4;
                }
            }
        }
weight=life*5;
        g.setFont(new Font("仿宋",Font.BOLD,30));
        g.setColor(Color.GREEN);
g.drawString("boss HP",150,50);
        g.setColor(Color.green);
        g.fillRect(300,30,weight,height);

    }

    //获取当前元素矩形,是为碰撞检测而写
    public Rectangle getRec() {
        return new Rectangle(x, y, 248, 200);
    }

敌方子弹类

import java.awt.*;
public class BulletObj1 extends Gameplane{
    public BulletObj1(String img,int x,int y,double speed,win frame){
        this.img= Toolkit.getDefaultToolkit().getImage(img);
        this.x=x;
        this.y=y;
        this.speed=speed;
        this.frame=frame;
    }
    public   void paintSet(Graphics g){
        g.drawImage(img,x,y,null);
        y+=speed;
    }
    //获取当前元素矩形,是为碰撞检测而写
    public  Rectangle getRec(){
        return new Rectangle(x,y,25,25);
    }
}

飞机升级后新加的子弹类2

import java.awt.*;
public class BulletObj2 extends Gameplane {
    public BulletObj2(String img,int x,int y,double speed,win frame){
        this.img= Toolkit.getDefaultToolkit().getImage(img);
        this.x=x;
        this.y=y;
        this.speed=speed;
        this.frame=frame;
    }
    public   void paintSet(Graphics g){
        g.drawImage(img,x,y,null);//画图
        y-=speed;//子弹移动
        for (EnemyObj enemyObj:this.frame.enemyObjs){
            if(this.getRec().intersects(enemyObj.getRec())){//检测子弹2与敌机的矩形是否相交
                for(int i=1;i<=7;i++)
                {this.img=Toolkit.getDefaultToolkit().getImage("D:/Plane/Plane/src/image/explode/"+i+".gif");
                    g.drawImage(img,this.x,this.y-30,null);
                }

                this.x=-100;
                enemyObj.x=-200;
                this.frame.score++;
                this.frame.removeList.add(enemyObj);
                this.frame.removeList.add(this);
            }
        }
    }
    //获取当前元素矩形,是为碰撞检测而写
    public  Rectangle getRec(){
        return new Rectangle(x,y,25,25);//定义矩形大小及坐标
    }
}

到此代码部分就写完了

成品展示

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值