首页 > 编程语言 >java-飞机大战(源代码)

java-飞机大战(源代码)

时间:2024-03-31 18:05:02浏览次数:16  
标签:java img int frame 大战 new 源代码 speed public

今天来更新我的飞机大战了,是参考尚学堂写的,有需要的小伙伴可以直接来取,关于state=2时以及state=3时的运行时可能不太优化,下周我会更新代码的.

adb80b33d94d41d19e959d55c950a930.png

 

1.整个游戏的主窗口以及游戏方法

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//-----飞机大战的游戏状态: 0.未开始 1.游戏中 2.通关失败 3.通关成功-----

public class planeWin extends JFrame {
    //添加背景图片
    static ImageIcon backGround=new ImageIcon("D:\\idea.java\\plane\\background.png");
    //添加子弹图片
    static ImageIcon shall=new ImageIcon("D:\\idea.java\\plane\\myBullet.png");
    //添加敌方boss图片
    static ImageIcon boss=new ImageIcon("D:\\idea.java\\plane\\blueEnemyPlane.png");
    //添加我方飞机图片
    static ImageIcon myPlane=new ImageIcon("D:\\idea.java\\plane\\myPlane.png");
    //添加敌方飞机的图片
    static ImageIcon enemyPlane=new ImageIcon("D:\\idea.java\\plane\\enemyPlane.png");
    //添加飞机一个爆炸图片
    static ImageIcon explore=new ImageIcon("D:\\idea.java\\plane\\explore.png");
    //添加敌方boss的子弹
    static ImageIcon bossShall=new ImageIcon("D:\\idea.java\\plane\\bossShall.png");
    //添加灰色飞机图片
    static ImageIcon geryPlane=new ImageIcon("D:\\idea.java\\plane\\greyEnemyPlane.png");


    //创建集合
    //整个物体的集合
    ArrayList<gameObj> gameObjList=new ArrayList<>();
    //子弹物体的集合
    ArrayList<shallObj> shallObjList=new ArrayList<>();
    //创建敌方飞机的集合
    ArrayList<enemyPlaneObj> enemyPlaneObjList=new ArrayList<>();
    //创建消失集合
    ArrayList<gameObj> removeList=new ArrayList<>();
    //给敌方boss添加集合
    ArrayList<bossShallObj> bossShallList=new ArrayList<>();
    //添加爆炸集合的集合
    ArrayList<exploreObj> exploreObjList=new ArrayList<>();
    //创建灰色飞机的集合
    ArrayList<geryPlaneObj> geryEnemyPlaneList=new ArrayList<>();


    //解决图片闪动,实现双缓存
    Image offScreenImage=null;

    //创建背景类的对象
    bgObj bg=new bgObj(backGround.getImage(),0,-2000,2);
    //创建我方飞机的对象
    public planeObj myplane=new planeObj(myPlane.getImage(),296,500,37,41,0,this);
    //创建敌方BOSS
    bossObj bossobj=null;


    //得到图片更新的次数
    int count=1;
    //统计敌方飞机生成的数量
    int enemyNumber=0;
    //设置分数
    public static int score=0;
    //设置游戏初始状态为0
    public static int state=0;
    //创建窗口
    public void JFrame(){
        //设置标题
        this.setTitle("飞机大战 v1.0");
        //设置窗口关闭事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置画布大小
        this.setSize(600,600);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置窗口可见
        this.setVisible(true);
        gameObjList.add(bg);
        gameObjList.add(myplane);

        //添加鼠标点击事件
        this.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent m){
                if(m.getButton()==1&&state==0){
                    state=1;
                    repaint();
                }
            }
        });
        //添加空格鼠标按键,实现游戏的暂停功能
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==32){
                    switch (state){
                        case 0:
                            state=1;
                            break;
                        case 1:
                            state=0;
                            break;
                        default:
                    }
                }
            }
        });
        //创建时间管理计时器
        new Thread(() -> {
            while(true){
                if(state==1){
                    creatObj();
                    repaint();
                }
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void paint(Graphics g){
        if(offScreenImage==null){
            offScreenImage=createImage(600,600);
        }
        Graphics gImage=offScreenImage.getGraphics();
        gImage.fillRect(0,0,600,600);
        //游戏未开始
        if(state==0){
            backGround.paintIcon(this,gImage,0,0);
            myPlane.paintIcon(this,gImage,296,500);
            shall.paintIcon(this,gImage,309,450);
            boss.paintIcon(this,gImage,230,80);
            drawWord(gImage,"游戏开始",254,300,30,Color.yellow);
        }
        //游戏进行中
        else if(state==1){
            gameObjList.addAll(exploreObjList);
            for(int i=0;i<gameObjList.size();i++){
                gameObjList.get(i).paintSelf(gImage);
             }
            gameObjList.removeAll(removeList);

        }
        //游戏通关失败
        else if(state==2){
            backGround.paintIcon(this,gImage,0,0);
            explore.paintIcon(this,gImage, myplane.getX()-25,myplane.getY()-34);
            drawWord(gImage,"GAME OVER",225,300,40,Color.red);
        }
        //游戏通过成功
        else if(state==3){
            backGround.paintIcon(this,gImage,0,0);
            drawWord(gImage,"挑战成功!",230,300,40,Color.green);
        }
        drawWord(gImage,"Score: "+score,30,80,20,Color.white);
        g.drawImage(offScreenImage,0,0,null);
        count++;

    }
    void creatObj(){
        //创建我方子弹
        if(count%15==0) {
            shallObj newShallObj = new shallObj(shall.getImage(), myplane.getX() + 5, myplane.getY() - 16, 30, 30, 5, this);
            gameObjList.add(newShallObj);
            shallObjList.add(newShallObj);
        }
        //创建敌方飞机
        if(count%20==0) {
            enemyPlaneObj newEnemyObj=new enemyPlaneObj(enemyPlane.getImage(),(int)(Math.random()*12)*50,0,49,36,5,this);
            gameObjList.add(newEnemyObj);
            enemyPlaneObjList.add(newEnemyObj);
            enemyNumber++;
        }
        //创建敌方灰色飞机
        if(count%50==0){
            geryPlaneObj newGeryPlane=new geryPlaneObj(geryPlane.getImage(),(int)(Math.random()*12)*50,0,44,67,6,this) ;
            gameObjList.add(newGeryPlane);
            geryEnemyPlaneList.add(newGeryPlane);
        }
        //创建boss的集合
        if(count%20==0&&bossobj!=null){
            bossShallObj newBossShall=new bossShallObj(bossShall.getImage(), bossobj.getX()+75,bossobj.getY()+150,40,40,5,this);
            gameObjList.add(newBossShall);
            bossShallList.add(newBossShall);
        }
        //创建boss
        if(enemyNumber>=15&&bossobj==null){
            bossobj=new bossObj(boss.getImage(),50,50,150,100,4,this);
            gameObjList.add(bossobj);
        }

    }

    public void drawWord(Graphics gImage,String str,int x,int y,int size,Color color) {
        gImage.setColor(color);
        gImage.setFont(new Font("仿宋", Font.BOLD, size));
        gImage.drawString(str, x, y);
    }

    public static void main(String[] args) {
        planeWin win=new planeWin();
        win.JFrame();
    }

}

2.类:

1.整个游戏中的物体需要继承的父类

import java.awt.*;

public class gameObj {
    Image img;
    int x;
    int y;
    int width;
    int height;
    double speed;
    planeWin frame;

    public gameObj() {
    }
    public gameObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.frame = frame;
    }

    public gameObj(Image img, int x, int y, double speed) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public gameObj(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Image getImg() {
        return img;
    }

    public void setImg(Image img) {
        this.img = img;
    }

    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 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 double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public planeWin getFrame() {
        return frame;
    }

    public void setFrame(planeWin frame) {
        this.frame = frame;
    }

    public void paintSelf(Graphics gImage){
        gImage.drawImage(img,x,y,null);
    }

    //用于碰撞事件
    public Rectangle getRct(){
        return new Rectangle(x,y,width,height);
    }
}

2.背景类

import java.awt.*;

public class bgObj extends gameObj {
    public bgObj() {
    }

    public bgObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    public bgObj(Image img, int x, int y, double speed) {
        super(img, x, y, speed);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=2;
        //实现背景的反复应用
        if(y>=0){
            y=-2000;
        }
    }
}

3.我方飞机类

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

public class planeObj extends gameObj{

    public planeObj() {
    }

    public planeObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);

        //添加鼠标移动事件控制飞机的移动,随鼠标移动而移动
        this.frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                //让飞机的坐标跟随鼠标的位置
                planeObj.super.x=e.getX()-11;
                planeObj.super.y=e.getY()-16;

            }
        });
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);

    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

4.敌方飞机1类

import java.awt.*;

public class enemyPlaneObj extends gameObj{
    public enemyPlaneObj() {

    }

    public enemyPlaneObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(img, x, y, null);
        y+=speed;
        if(y>600){
            this.setX(-300);
            this.setY(300);
            frame.removeList.add(this);
        }
        if(frame.myplane.getRct().intersects(x,y,this.getWidth()/2,this.getHeight()/2)){
            planeWin.state =2;
            frame.repaint();

        }
        //遍历整个集合里面的类
        for(shallObj shallObj: frame.shallObjList){
            //如果发生碰撞
            if(this.getRct().intersects(shallObj.getRct())){
                //添加爆炸类
                exploreObj NewexploreObj=new exploreObj(x,y);
                frame.exploreObjList.add(NewexploreObj);
                frame.removeList.add(NewexploreObj);
                this.x=-100;
                this.y=100;
                shallObj.setX(-200);
                shallObj.setY(200);
                frame.removeList.add(this);
                frame.removeList.add(shallObj);
                planeWin.score+=1;
            }
        }


    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

5.敌方飞机2类

import java.awt.*;

public class geryPlaneObj extends gameObj{

    int life=2;
    public geryPlaneObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=speed;
        if(y>600){
            this.setX(-300);
            this.setY(300);
            frame.removeList.add(this);
        }
        if(frame.myplane.getRct().intersects(this.getRct())){
            planeWin.state=2;
            frame.repaint();
        }
        for(shallObj shallObj: frame.shallObjList){
            if(this.getRct().intersects(shallObj.getRct())){
                life--;
            }
            if(life<=0){
                //添加爆炸类
                exploreObj NewexploreObj=new exploreObj(x,y);
                frame.exploreObjList.add(NewexploreObj);
                frame.removeList.add(NewexploreObj);
                this.x=-100;
                this.y=100;
                shallObj.setX(-200);
                shallObj.setY(200);
                frame.removeList.add(this);
                frame.removeList.add(shallObj);
                planeWin.score+=2;
            }
        }
    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

6.我方子弹类

import java.awt.*;

public class shallObj extends gameObj{
    public shallObj() {
    }

    public shallObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(img, x, y, null);
        y-=speed;
        if(y<0){
            this.setX(-400);
            this.setY(400);
            frame.removeList.add(this);
        }
    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

7.敌方boss类

import java.awt.*;

public class bossObj extends gameObj{

    int life=10;
    public bossObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        if(x<-10||x>490){
            speed=-speed;
        }
        x+=speed;
        if(frame.myplane.getRct().intersects(x,y,getWidth()/2,getHeight()/2)){
            planeWin.state=2;
            frame.repaint(100);
        }
        for(shallObj shallObj: frame.shallObjList){
            if(this.getRct().intersects(shallObj.getRct())){
                life--;
                shallObj.setX(-200);
                shallObj.setY(200);
                frame.removeList.add(shallObj);
            }

            if(life<=0){
                this.setX(-500);
                this.setY(200);
                planeWin.state=3;
                frame.repaint();

            }
            gImage.setColor(Color.white);
            gImage.fillRect(10,40,100,10);
            gImage.setColor(Color.red);
            gImage.fillRect(10,40,life*100/10,10);

        }
    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

8.敌方boss子弹类

import java.awt.*;

public class bossShallObj extends gameObj{
    public bossShallObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=speed;
        if(y>600){
            this.setX(-300);
            this.setY(300);
            frame.removeList.add(this);
        }
        // 检测与 bossShallObj 的碰撞
        for (bossShallObj shallObj : frame.bossShallList) {
            if (shallObj.getRct().intersects(frame.myplane.getX()+frame.myplane.width/2,frame.myplane.getY()+frame.myplane.height/2,frame.myplane.width/4,frame.myplane.height /4)) {
                // 如果与玩家飞机相交,设置游戏状态为 2
                planeWin.state = 2;
                frame.repaint();
            }
        }
    }

    @Override
    public Rectangle getRct() {
        return super.getRct();
    }
}

9.爆炸图类

import java.awt.*;

public class exploreObj extends gameObj{

    int pictureNumber=0;

    static Image[] exploreImages=new Image[16];
    static {
        for (int i=0;i<15;i++){
            exploreImages[i]=Toolkit.getDefaultToolkit().getImage("D:\\idea.java\\plane\\img_"+(17+i)+".png");
        }
    }

    public exploreObj(int x, int y) {
        super(x, y);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        //循环16次添加爆炸图片
        if(pictureNumber<15){
            img=exploreImages[pictureNumber];
            super.paintSelf(gImage);
            pictureNumber++;
        }
    }
}

写到

 

标签:java,img,int,frame,大战,new,源代码,speed,public
From: https://blog.csdn.net/2301_81253185/article/details/137205095

相关文章

  • Java(2)之变量,数据类型,运算符
    1、标识符在java中标识符是由字母数字下划线,美元符号组成(其中数字没办法作首)在java中是区分大小写的。2、关键字关键字是在java中赋予了特殊的含义的字符,只能用于特定地方例如包package类class接口interface引用:this,supe,创建对象:new等等关键字引用需要注意大小写,且......
  • Java-数据类型
    前言:本文主要讲述八大数据类型以及使用规范,最后对知识进行一个拓展。强类型语言要求变量使用要严格符合规定,所有变量都必须先定义后才能使用弱类型语言基本类型(primitivetype)引用类型(referencetype)*不规范的*规范的*如下面这种定义变量是可以成功:*注意事项:......
  • Java常用API二
    BigDecimal用于解决浮点数运算时,出现结果失真的问题 传统时间:Date日期类//1、创建一个Date的对象,代表系统当前的时间信息Dated=newDate();System.out.println(d);//2、拿到时间毫秒值longtime=d.getTime();//3、把时间毫秒值转换成日期对象:2s后时间是多少time+=......
  • Java基础入门--第三章--面向对象(上)
    面向对象(上)1.1面向对象的思想2类与对象2.1类的定义2.2对象的创建与使用2.3对象的引用传递2.4访问控制权限3封装性3.1为什么要封装3.2如何实现封装4构造方法4.1定义构造方法4.2构造方法的重载5this关键字5.1使用this关键字调用本类中的属性5.2使用this......
  • 程序员/后端开发方向Java 跳槽注意事项(简历和面试经验分享)
    程序员/后端开发方向Java跳槽注意事项(简历和面试经验分享)应届生面试经验参考:https://www.cnblogs.com/rainbow-1/p/16779048.html简历:1、个人感觉还是要写真话,包装的内容要有一定的基础,问起来能够对答几个回合。2、基本信息最好直接写年龄,而不是出生年月。跳槽简历最好标明当......
  • [蓝桥杯] 管道 java题解
    importjava.util.*;/***管道*其实这道题核心根本不用管管道左边的如何,我们可以把左边当成注水口*/publicclassMain{staticintn;staticint[][]pipes;//阀门安排的地方staticintlen;//管道长度publicstaticvoidmain(String[]a......
  • 基于ssm+vue.js的酒店预约及管理系统附带文章和源代码设计说明文档ppt
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我成功案例代码参考数据库参考源码获取前言......
  • 基于ssm+vue.js的校园招聘系统附带文章和源代码设计说明文档ppt
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我成功案例代码参考数据库参考源码获取前言......
  • 基于ssm+vue.js的宠物医院管理系统附带文章和源代码设计说明文档ppt
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我成功案例代码参考数据库参考源码获取前言......
  • 基于ssm+vue.js的OA办公系统附带文章和源代码设计说明文档ppt
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我成功案例代码参考数据库参考源码获取前言......