java项目飞机大战

项目说明

此项目总共代码量为400行左右,在b站有教程(时长1h),适合于刚刚学完了java的同学做第一个项目。

程序运行效果图

玩法说明

玩家鼠标左键开始游戏,空格键暂停,用鼠标控制我方飞机的移动,当出现的小飞机总数达到50时,敌方boss出现,击败敌方boss游戏胜利。

最终打包可运行文件下载

点击下载

以下为项目实现部分------------------------------------------------

文件组织形式

源码部分

bgObj.java

package com.sqm;

import java.awt.*;

public class bgObj extends GamObj
{

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

    @Override
    public void paintself(Graphics g)
    {
        super.paintself(g);
        y += speed;

        if (y >= 0) y = -2000;//背景图片的循环播放
    }
}

BossObj.java

package com.sqm;

import java.awt.*;

public class BossObj extends GamObj
{

    int life = 10;//敌方boss血条

    public BossObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {
        super(image, x, y, width, height, speed, frame);
    }

    @Override
    public void paintself(Graphics g) {
        super.paintself(g);
        //防止走出屏幕
        if(x >= 550 || x < -50)
        {
            speed = -speed;
        }
        x += speed;
        for (ShellObj shellObj:GameUtils.shellObjList)
        {
            if(this.getRec().intersects(shellObj.getRec()))
            {
                shellObj.setX(-100);
                GameUtils.removeList.add(shellObj);
                life --;
            }
            if (life <= 0)
            {
                GameWin.state = 4;
            }
        }
        //血条的白色背景
        g.setColor(Color.white);
        g.fillRect(20,40,100,10);
        //血条的绘制
        g.setColor(Color.red);
        g.fillRect(20,40,life * 100 / 10,10);
    }

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

}

BulletObj.java

package com.sqm;

import java.awt.*;

public class BulletObj extends GamObj
{
    public BulletObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {
        super(image, x, y, width, height, speed, frame);
    }

    @Override
    public void paintself(Graphics g) {
        super.paintself(g);
        y += speed;
        //敌方子弹与我方子弹的碰撞检测
        if(this.getRec().intersects(this.frame.plane.getRec()))
        {
            GameWin.state = 3;
        }
        //判断敌方子弹是否出界
        if(y > 600)
        {
            GameUtils.bulletObjList.remove(this);
            GameUtils.removeList.add(this);
        }
    }

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

EnemyObj.java

package com.sqm;

import java.awt.*;

public class EnemyObj extends GamObj
{
    public EnemyObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {
        super(image, x, y, width, height, speed, frame);
    }

    @Override
    public void paintself(Graphics g) {
        super.paintself(g);
        y += speed;

        //判断敌机是否与我方飞机碰撞
        if(this.getRec().intersects(this.frame.plane.getRec()))
        {
            GameWin.state = 3;
        }

        //判断敌机和我方子弹是否碰撞
        for (ShellObj shell:GameUtils.shellObjList)
        {
            //敌机被我方子弹击落
            if(this.getRec().intersects(shell.getRec()))
            {
                Explode explode = new Explode(x,y);
                GameUtils.explodeList.add(explode);
                GameUtils.removeList.add(explode);

                this.setX(-100);
                shell.setX(-100);
                GameUtils.removeList.add(this);
                GameUtils.removeList.add(shell);
                GameWin.score ++;
            }
        }

        //判断敌方飞机是否出界
        if(y > 600)
        {
            GameUtils.removeList.add(this);
            GameUtils.EnemyObjList.remove(this);
        }
    }

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

Explode.java

package com.sqm;

import java.awt.*;

public class Explode extends GamObj
{

    static Image[] pic = new Image[16];

    int explodeCount = 0;//让图片只显示一次

    static
    {
        for (int i = 0; i < pic.length; i++)
        {
            pic[i] = GameUtils.readImage("imgs/explode/e" + (i + 1) + ".gif");
        }
    }

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

    @Override
    public void paintself(Graphics g)
    {
        if(explodeCount < 16)
        {
            image = pic[explodeCount];
            explodeCount ++;
            super.paintself(g);
        }

    }
}

GameUtils.java

package com.sqm;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class GameUtils
{
    public static int WIDTH = 600;
    public static int HEIGHT = 600;
    //获取图片
    public static Image readImage(String path)
    {
        Image image = null;
        try
        {
            //返回当前运行文件的目录
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
            image = ImageIO.read(resourceAsStream);
            resourceAsStream.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return image;
    }

    //绘制字符串的工具类
    public static void draw(Graphics gImage,String str, Color color, int size, int x, int y)
    {
        gImage.setColor(color);
        gImage.setFont(new Font("仿宋",Font.BOLD,size));
        gImage.drawString(str,x,y);
    }

    //背景图
    public static Image bgImg = readImage("imgs/bg.jpg");
    //boss图
    public static Image bossImg = readImage("imgs/boss.png");
    //爆炸图
    public static Image explodImg = readImage("imgs/explode/e6.gif");
    //我方飞机图
    public static Image planeImg = readImage("imgs/plane.png");
    //我方子弹图片
    public static Image shellImg = readImage("imgs/shell.png");
    //敌机图片
    public static Image enemyImg = readImage("imgs/enemy.png");
    //敌方子弹图片
    public static Image bulletImg = readImage("imgs/bullet.png");

    //我方子弹的集合
    public static List<ShellObj> shellObjList = new ArrayList<>();
    //我方游戏物体的集合
    public static List<GamObj> gamObjList = new ArrayList<>();
    //敌机的集合
    public static List<EnemyObj> EnemyObjList = new ArrayList<>();
    //要移除对象的集合
    public static List<GamObj> removeList = new ArrayList<>();
    //敌方子弹集合
    public static List<BulletObj> bulletObjList = new ArrayList<>();
    //爆炸集合
    public static List<Explode> explodeList = new ArrayList<>();
}

GameWin.java

package com.sqm;

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;

public class GameWin extends JFrame
{

    //游戏状态设置 0未开始 1游戏中 2暂停 3通关失败 4通过成功
    public static int state = 0;
    //游戏得分
    public static int score = 0;
    //开挂
    public static boolean kaigua = false;
    //游戏重绘次数
    int count = 1;
    //敌机生成数量
    int enemyCount = 0;

    //双缓冲罐防闪屏
    Image offScreenImage = null;
    //背景
    bgObj bg = new bgObj(GameUtils.bgImg,0,-2000,2);
    //我方飞机
    PlaneObj plane = new PlaneObj(GameUtils.planeImg,290,590,20,30,0,this);
    //boss飞机
    BossObj bossObj = null;

    public void lanch()
    {
        this.setVisible(true);
        this.setSize(GameUtils.WIDTH,GameUtils.HEIGHT);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("飞机大战");

        //鼠标点击事件
        this.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (e.getButton() == 1)//按下鼠标左键并且游戏状态为尚未开始时,开始游戏
                {
                    if(state == 0)
                    {
                        state = 1;
                        repaint();
                    }
                    else if(state == 3)
                    {
                        state = 1;
                        GameUtils.gamObjList.clear();
                        GameUtils.bulletObjList.clear();
                        GameUtils.removeList.clear();
                        GameUtils.EnemyObjList.clear();
                        GameUtils.shellObjList.clear();
                        GameUtils.explodeList.clear();
                        bossObj = null;
                        GameUtils.gamObjList.add(bg);
                        GameUtils.gamObjList.add(plane);
                        score = 0;
                        repaint();
                    }

                }
            }
        });

        //暂停功能
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == 32)
                {
                    switch (state)
                    {
                        case 1:
                            state = 2;
                            break;
                        case 2:
                            state = 1;
                            break;
                        default:
                    }
                }
                //按下k键开挂
                if(e.getKeyCode() == 75)
                {
                   if(kaigua)kaigua = false;
                   else kaigua = true;
                }
            }
        });

        GameUtils.gamObjList.add(bg);
        GameUtils.gamObjList.add(plane);

        while(true)
        {
            if(state == 1)
            {
                createObj();
                repaint();
            }
            try
            {
                Thread.sleep(25);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

    }

    public void paint(Graphics g)
    {
        if(offScreenImage == null)
        {
            offScreenImage = createImage(GameUtils.WIDTH,GameUtils.HEIGHT);
        }
        //获取缓冲图片的画笔对象
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0,0,GameUtils.WIDTH,GameUtils.HEIGHT);
        //游戏尚未开始
        if(state == 0)
        {
            gImage.drawImage(GameUtils.bgImg,0,0,null);
            gImage.drawImage(GameUtils.bossImg,220,120,null);
            gImage.drawImage(GameUtils.explodImg,270,350,null);
            GameUtils.draw(gImage,"点击开始游戏",Color.yellow,40,180,300);
        }
        //游戏已经开始
        if(state == 1)
        {
            GameUtils.gamObjList.addAll(GameUtils.explodeList);
            for (int i = 0; i < GameUtils.gamObjList.size(); i++)
            {
                GameUtils.gamObjList.get(i).paintself(gImage);
            }
            GameUtils.gamObjList.removeAll(GameUtils.removeList);
        }
        //游戏失败
        if(state == 3)
        {
            gImage.drawImage(GameUtils.explodImg,plane.getX()-35,plane.getY()-50,null);
            GameUtils.draw(gImage,"GAME OVER",Color.red,40,180,300);
        }
        //游戏通关
        if(state == 4)
        {
            gImage.drawImage(GameUtils.explodImg,bossObj.getX()+30,bossObj.getY(),null);
            GameUtils.draw(gImage,"游戏通关",Color.green,50,190,300);
        }

        //计分面板
        GameUtils.draw(gImage,score + "分",Color.green,40,30,100);
        count ++;
        g.drawImage(offScreenImage,0,0,null);
    }

    void createObj()
    {
        if(count % 15 == 0 || kaigua)
        {
            //我方子弹
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,plane.getX()+3,plane.getY()-16,14,25,5,this));
            GameUtils.gamObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));
        }
        if(count % 15 == 0)
        {
            //敌机
            GameUtils.EnemyObjList.add(new EnemyObj(GameUtils.enemyImg,(int)(Math.random()*12) * 50,0,49,36,5,this));
            GameUtils.gamObjList.add(GameUtils.EnemyObjList.get(GameUtils.EnemyObjList.size() - 1));
            enemyCount ++;
        }
        if(count % 15 == 0 && bossObj != null)
        {
            //敌方子弹
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg, bossObj.getX() + 76, bossObj.getY() + 85, 15,25,5,this));
            GameUtils.gamObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size() - 1));
        }
        //小飞机出现100辆后出现boss
        if(enemyCount >= 50 && bossObj == null)
        {
            bossObj = new BossObj(GameUtils.bossImg,250,0,155,100,5,this);
            GameUtils.gamObjList.add(bossObj);
        }
    }

    public static void main(String[] args)
    {
        GameWin gameWin = new GameWin();
        gameWin.lanch();
    }

}

GameObj.java

package com.sqm;

import java.awt.*;

public class GamObj
{
    Image image;
    int x;
    int y;
    int width;
    int height;
    int speed;
    GameWin frame;

    public Image getImage() {
        return image;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getSpeed() {
        return speed;
    }

    public GameWin getFrame() {
        return frame;
    }

    public GamObj setImage(Image image) {
        this.image = image;
        return this;
    }

    public GamObj setX(int x) {
        this.x = x;
        return this;
    }

    public GamObj setY(int y) {
        this.y = y;
        return this;
    }

    public GamObj setWidth(int width) {
        this.width = width;
        return this;
    }

    public GamObj setHeight(int height) {
        this.height = height;
        return this;
    }

    public GamObj setSpeed(int speed) {
        this.speed = speed;
        return this;
    }

    public GamObj setFrame(GameWin frame) {
        this.frame = frame;
        return this;
    }

    public GamObj(Image image, int x, int y, int width, int height, int speed, GameWin frame)
    {
        this.image = image;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.frame = frame;
    }

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

    public GamObj(Image image, int x, int y, int speed)
    {
        this.image = image;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public void paintself(Graphics g)
    {
        g.drawImage(image,x,y,null);
    }
    public Rectangle getRec()
    {
        return new Rectangle(x,y,width,height);
    }
}

PlaneObj.java

package com.sqm;

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

public class PlaneObj extends GamObj
{
    @Override
    public Image getImage() {
        return super.getImage();
    }

    public PlaneObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {
        super(image, 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 g) {
        //我方飞机和敌方boss的检测
        if(this.frame.bossObj != null && this.getRec().intersects(this.frame.bossObj.getRec()))
        {
            GameWin.state = 3;
        }
        super.paintself(g);
    }

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

ShellObj.java

package com.sqm;

import java.awt.*;

public class ShellObj extends GamObj
{
    @Override
    public Image getImage() {
        return super.getImage();
    }

    public ShellObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {
        super(image, x, y, width, height, speed, frame);
    }

    @Override
    public void paintself(Graphics g) {
        super.paintself(g);
        y -= speed;
        //我方子弹的越界消失
        if (y < 0)
        {
            GameUtils.removeList.add(this);
            GameUtils.shellObjList.remove(this);
        }
    }

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

猜你喜欢

转载自blog.csdn.net/falldeep/article/details/121909781