通过Java语言来实现飞机大战(附带源码)

目录

首先是程序的主类GameWin

游戏父类GameObj

工具类GameUtils

飞机类PlaneObj

背景类BgObj

反派boss类BossObj

子弹类BulletObj

敌机类EnemyObj

爆炸类ExplodeObj

最后是己方子弹ShellObj类

图片资源


大家好,我是小刘!  

本篇文章将向各位介绍一个Java游戏项目,本次项目可以应用与毕业设计以及简历等!!!

运行结果如下

接下来就让我们进入代码的实现吧

首先新建一个项目,然后在项目里面新建一个存放图片的文件夹,将本次项目所需要的图片存放进去,图片资源在文章最后。(按照小刘下面这个方式去创建就可以啦,!!一定要在项目中去创建!!)

首先是程序的主类GameWin

package ld1;

import Game.*;
import Game.Utils.GameUtils;
import Game.Utils.PlaneObj;

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 {

    public static int state=0;
    public static int score=0;
    Image offScreenImage=null;
    int width=600;
    int height=600;
    int count=1;
    int enemyCount=0;

    BgObj bgobj=new BgObj(GameUtils.bgImg,0,-2000,2);
    public PlaneObj planeObj=new PlaneObj(GameUtils.planeImg,290,550,20,30,0,this);
    public BossObj bossObj=null;
    public void lunch(){
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width,height);
        //设置窗口位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("飞机大战");

        GameUtils.gameObjList.add(bgobj);
        GameUtils.gameObjList.add(planeObj);




        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1 && state==0){
                    state=1;
                    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:break;
                    }
                }
            }
        });
        while (true){
            if (state==1){
                createObj();
                repaint();
            }

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

    @Override
    public void paint(Graphics g) {
        if (offScreenImage==null){
            offScreenImage=createImage(width,height);
        }
        Graphics gImage= offScreenImage.getGraphics();
        gImage.fillRect(0,0,width,height);
        if (state==0){
            gImage.drawImage(GameUtils.bgImg,0,0,null);
            gImage.drawImage(GameUtils.bossImg,220,120,null);
            gImage.drawImage(GameUtils.explodeImg,270,350,null);
            GameUtils.drawWord(gImage,"点击开始游戏",Color.yellow,40,180,300);

        }
        if (state==1){
            GameUtils.gameObjList.addAll(GameUtils.explodeObjList);

            for (int i=0;i<GameUtils.gameObjList.size();i++){
                GameUtils.gameObjList.get(i).paintSelf(gImage);
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeObjList);

        }
        if (state==3){
            gImage.drawImage(GameUtils.explodeImg,bossObj.getX()+30,planeObj.getY()-50,null);
            GameUtils.drawWord(gImage,"GAME OVER",Color.red,50,180,300);
        }
        if (state==4){
            gImage.drawImage(GameUtils.explodeImg,planeObj.getX()-35,bossObj.getY(),null);
            GameUtils.drawWord(gImage,"游戏通关",Color.green,50,180,300);
        }

        GameUtils.drawWord(gImage,score+"分",Color.green,40,30,100);
        g.drawImage(offScreenImage,0,0,null);
        count++;
        System.out.println(GameUtils.gameObjList.size());
    }

    void createObj(){
        if (count%15==0) {
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, planeObj.getX() + 3, planeObj.getY() - 16, 14, 29, 5, this));
            GameUtils.gameObjList.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,32,24,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.shellObjList.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.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));

        }
        if (enemyCount>100 && bossObj==null){
            bossObj=new BossObj(GameUtils.bossImg,250,35,155,100,5,this);
            GameUtils.gameObjList.add(bossObj);
        }
        }

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

游戏父类GameObj

package Game;

import ld1.GameWin;

import java.awt.*;

public class GameObj {
    Image img;
     int x;
     int y;
    int width;
    int height;
    double speed;
    GameWin frame;

    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 GameWin getFrame() {
        return frame;
    }

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

    }

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

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

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


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

    }
    public Rectangle getRec(){
        return new Rectangle(x,y,width,height);
    }
}

工具类GameUtils

package Game.Utils;

import Game.*;

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

public class GameUtils {

    public static Image bgImg=Toolkit.getDefaultToolkit().getImage("imgs/bgImg.png");

    public static Image bossImg=Toolkit.getDefaultToolkit().getImage("imgs/boss.png");
    public static Image explodeImg=Toolkit.getDefaultToolkit().getImage("imgs/e6.png");
    public static Image planeImg=Toolkit.getDefaultToolkit().getImage("imgs/planeImg.png");
    public static Image shellImg=Toolkit.getDefaultToolkit().getImage("imgs/shellImg.png");
    public static Image bulletImg=Toolkit.getDefaultToolkit().getImage("imgs/bulletImg.png");
    public static Image enemyImg=Toolkit.getDefaultToolkit().getImage("imgs/enemyImg.png");




    public static List<GameObj> gameObjList=new ArrayList<>();
    public static List<GameObj> removeObjList=new ArrayList<>();
    public static List<BulletObj> bulletObjList=new ArrayList<>();
    public static List<ShellObj> shellObjList=new ArrayList<>();
    public static List<EnemyObj> enemyObjList=new ArrayList<>();
    public static List<ExplodeObj> explodeObjList=new ArrayList<>();

    public static void drawWord(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);
    }

}

飞机类PlaneObj

package Game.Utils;

import Game.GameObj;
import ld1.GameWin;

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

public class PlaneObj extends GameObj {
    @Override
    public Image getImg() {
        return super.getImg();
    }

    public PlaneObj() {
        super();
    }

    public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
        this.getFrame().addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                PlaneObj.super.setX(e.getX() - 19);
                PlaneObj.super.setY(e.getY() - 20);
            }
        });

    }

    @Override
    public void paintSelf(Graphics gImage) {

        super.paintSelf(gImage);
        if (this.getFrame().bossObj!=null&&this.getRec().intersects(this.getFrame().bossObj.getRec())){
            GameWin.state=3;

        }
    }

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

背景类BgObj

package Game;

import java.awt.*;

public class BgObj extends GameObj{
    public BgObj(){
        super();

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


    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=speed;
        if (y>=0){
            y=-2000;
        }
    }
}

反派boss类BossObj

package Game;

import Game.Utils.GameUtils;
import ld1.GameWin;

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, GameWin frame) {
        super(img, x, y, width, height, speed, frame);
    }

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        if (x>550 || x<-50){
            speed=-speed;
        }
        x+=speed;
        for (ShellObj shellObj: GameUtils.shellObjList){
            if (this.getRec().intersects(shellObj.getRec())){
                shellObj.setX(-100);
                shellObj.setY(100);
                GameUtils.removeObjList.add(shellObj);
                life--;
            }
            if (life <= 0) {
                GameWin.state=4;

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

    }

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

子弹类BulletObj

package Game;

import Game.Utils.GameUtils;
import ld1.GameWin;

import java.awt.*;

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

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=speed;
        if (this.getRec().intersects(this.frame.planeObj.getRec())){
            GameWin.state=3;
        }
        if (y>600){
            this.x=-300;
            this.y=300;
            GameUtils.removeObjList.add(this);
        }
    }

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

敌机类EnemyObj

package Game;

import Game.Utils.GameUtils;
import ld1.GameWin;

import java.awt.*;

public class EnemyObj extends GameObj{
    public EnemyObj() {
        super();
    }

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

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y+=speed;
        if (this.getRec().intersects(this.frame.planeObj.getRec())){
            GameWin.state=3;
        }
        if (y>600){
            this.x=-200;
            this.y=200;
            GameUtils.removeObjList.add(this);
        }

        for (ShellObj shellObj: GameUtils.shellObjList) {
            if (this.getRec().intersects(shellObj.getRec())){
                ExplodeObj explodeObj=new ExplodeObj(x,y);
                GameUtils.explodeObjList.add(explodeObj);
                GameUtils.removeObjList.add(explodeObj);
                shellObj.setX(-100);
                shellObj.setY(100);
                this.x=-200;
                this.y=200;
                GameUtils.removeObjList.add(shellObj);
                GameUtils.removeObjList.add(this);
                GameWin.score++;
            }

        }
    }

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

爆炸类ExplodeObj

package Game;

import java.awt.*;

public class ExplodeObj extends GameObj{

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

    int explodeCount=0;
    static{
        for (int i = 0; i < pic.length ; i++) {
            pic[i]=Toolkit.getDefaultToolkit().getImage("imgs/explode/e1"+i+".png");

        }

    }
    public ExplodeObj(int x,int y){
        super(x,y);
    }
    @Override
    public void paintSelf(Graphics gImage) {


        if (explodeCount<16){
            super.img=pic[explodeCount];
            super.paintSelf(gImage);
            explodeCount++;
        }
    }
}

最后是己方子弹ShellObj类

package Game;

import Game.Utils.GameUtils;
import ld1.GameWin;

import java.awt.*;

public class ShellObj extends GameObj{
    @Override
    public Image getImg() {
        return super.getImg();
    }

    public ShellObj() {
        super();
    }

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

    @Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        y-=speed;
        if (y<0){
            this.x=-100;
            this.y=100;
            GameUtils.removeObjList.add(this);
        }
    }

    @Override
    public Rectangle getRec() {
        return super.getRec();
    }
}
图片资源

最后希望大家多多点赞支持小刘,下一个项目是贪吃蛇游戏的实现,大家也可以支持一下!

背景图bgImg.png

敌方boss图boss.png

敌方子弹图bulletImg.png

敌方飞机图enemyImg.png

飞机图planeImg.png

子弹图shellImg.png

下面是爆炸图1-16

package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x;//物体的x坐标 protected double y;//物体的y坐标 protected double width;//物体的宽 protected double heigth;//物体的高 protected BufferedImage image;//当前正在显示的图片 protected int index = 0;//图片数组下标序号,子类中使用 protected double step;//飞行物每次(1/24秒)移动的距离 protected int life;//命 protected int state;//飞行物的状态 public static final int ACTIVE=0;//活着状态 public static final int DEAD=1;//死亡状态 public static final int REMOVE=2;//回收状态 //默认构造器 public FlyingObject() { life = 1; state = ACTIVE; } //有参构造器 public FlyingObject(double width,double heigth){ this();//调用无参数的构造器,必须写在第一行. this.x = (int)(Math.random()*(480-width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random()*3+0.8;//初始化step为[0.8,3.8)之间的数 } //重写toString方法 public String toString() { return x+","+y+","+width+","+heigth+","+image; } //重写paint,方便子类对象的使用 public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null);//绘制图片 } //飞行物移动的move方法 /** * 重构了move,方法实现播放销毁动画功能 */ public void move(){ if(state == ACTIVE){ y += step; return ; } if(state == DEAD){ //从子类对象中获取下一张照片 BufferedImage img = nextImage(); if(img == null){ state = REMOVE;//没有照片则回收 }else{ image = img;//否则把子类的图片传给image } //越界则销毁 if(y>=825){ state = REMOVE; } } } /** * 子类中必须有的方法,返回下一个要播放的照片引用, * 如果返回null表示没有可播放的照片了. */ protected abstract BufferedImage nextImage(); /** * 飞行物被打了一下 */ public void hit(){ if(life>0){ life--; } if(life==0){ state = DEAD; } } /** * 碰撞检测的方法 * 检测物体的位置是否在碰撞的范围内. * (子弹是否在飞行物的碰撞范围内) */ public boolean duang(FlyingObject obj){ //this(x,y,w,h) //obj(x,y,w,h) double x1 = this.x - obj.width; double x2 = this.x + this.width; double y1 = this.y - obj.width; double y2 = this.y + this.heigth; return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2; } /** 重构FlyingObject,添加了状态检查方法 */ /** 检查飞行物死了吗 */ public boolean isDead(){ return state == DEAD; } /** 检查飞行物是否活动的 */ public boolean isActive(){ return state == ACTIVE; } /** 检查飞行是否可以被删除*/ public boolean canRemove(){ return state == REMOVE; } /** 飞行物添加"去死"方法*/ public void goDead(){ if(isActive()){ state = DEAD; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值