飞机大战脚本组成
1.有一个所有物体的父物体GameObject,然后就是一堆物体继承于他,等到他的属性于函数。
2.窗口组件
3.工具脚本(负责做一些杂事和存放图片)

代码
GameObject
package object;

import utils.GameWindow;

import java.awt.*;

public class GameObject {
    public Image image;
    public int x;
    public int y;
    public int width;
    public int height;
    public double speed;
    public GameWindow fram;

public Image getImage() {
        return image;
    }

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

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 GameWindow getFram() {
        return fram;
    }

public void setFram(GameWindow fram) {
        this.fram = fram;
    }

public GameObject(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        this.image = image;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.fram = fram;
    }

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

public GameObject(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

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

public GameObject() {//无参构造

}

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

}

背景
package object;

import utils.GameWindow;

import java.awt.*;

public class bgObj extends GameObject {
    public bgObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

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

public bgObj() {
        super();
    }

@Override
    public Image getImage() {
        return super.getImage();
    }

@Override
    public void paintSelf(Graphics gImage) {
        super.paintSelf(gImage);
        //y+=speed;
    }
}
玩家
package object;

import utils.GameWindow;

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

public class Plane extends GameObject{
    @Override
    public Image getImage() {
        return super.getImage();
    }

public Plane(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
        fram.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {//跟随鼠标移动
                Plane.super.x=e.getX()-11;//鼠标位置减去飞机位置的一半相当于鼠标在飞机中间
                Plane.super.y=e.getY()-16;
            }
        });
    }

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

public Plane() {
        super();
    }

@Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(image,x,y,width,height,null);
        //与敌方boss碰撞死亡
        if (this.fram.bossObj!=null&&this.getRec().intersects(this.fram.bossObj.getRec())){
            this.fram.state=3;
            this.fram.repaint();
        }
    }

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

子弹
package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class shellObj extends GameObject{
    @Override
    public Image getImage() {
        return super.getImage();
    }

public shellObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

public shellObj() {
        super();
    }

@Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(image,x,y,width,height,null);
        y-=speed;
        if (y<0){
            this.x=100;
            this.y=-100;
            GameUtils.removeObjList.add(this);
        }
    }

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

敌机
package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class enemyObj extends GameObject{
    @Override
    public Image getImage() {
        return super.getImage();
    }

public enemyObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

public enemyObj() {
        super();
    }

@Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(image,x,y,width,height,null);
        y+=speed;
        if (this.getRec().intersects(this.fram.planeObj.getRec())){
            GameWindow.state=3;
            this.fram.repaint();//重画一帧
        }
        for (shellObj item: GameUtils.shellObjList) {
            if (this.getRec().intersects(item.getRec())){
                ExplodeObj obj=new ExplodeObj(x,y,40,40);
                GameUtils.explodeObjList.add(obj);
                GameUtils.removeObjList.add(obj);
                item.setX(100);
                item.setY(-100);
                this.x=-100;
                this.y=-100;
                GameUtils.removeObjList.add(item);
                GameUtils.removeObjList.add(this);
                GameWindow.score++;
            }
        }
        if (y>600){
            this.x=-100;
            this.y=-100;
            GameUtils.removeObjList.add(this);
        }
    }

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

敌方boss
package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class BossObj extends GameObject{
    int life=10;
    @Override
    public Image getImage() {
        return super.getImage();
    }

public BossObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

public BossObj() {
        super();
    }

@Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(image,x,y,width,height,null);
        if (x>485||x<-40){
            speed=-speed;
        }
        x+=speed;
        for (shellObj item: GameUtils.shellObjList) {
            if (this.getRec().intersects(item.getRec())){
                item.setX(100);
                item.setY(-100);
                GameUtils.removeObjList.add(item);
                life--;
            }
            if (life<=0){
                GameUtils.removeObjList.add(this);
                GameWindow.state=4;
                this.fram.repaint();//重画一帧
            }
        }
        //血条的白色背景
        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();
    }
}

敌方子弹
package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class BulletObj extends GameObject{
    @Override
    public Image getImage() {
        return super.getImage();
    }

public BulletObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

public BulletObj() {
        super();
    }

@Override
    public void paintSelf(Graphics gImage) {
        gImage.drawImage(image,x,y,width,height,null);
        y+=speed;
        if (this.getRec().intersects(this.fram.planeObj.getRec())){
            this.fram.state=3;
            this.fram.repaint();
        }
        if (y>600){
            this.x=-300;
            this.y=-300;
            GameUtils.removeObjList.add(this);
        }
    }

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

敌方的爆炸效果
package object;

import utils.GameUtils;
import utils.GameWindow;

import java.awt.*;

public class ExplodeObj extends GameObject {
    //调用的帧数
    int count=0;

Image explode=Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
    @Override
    public Image getImage() {
        return super.getImage();
    }

public ExplodeObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
        super(image, x, y, width, height, speed, fram);
    }

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

public ExplodeObj() {
        super();
    }

public ExplodeObj(int x, int y, int width, int height) {
        super(x, y, width, height);
    }

@Override
    public void paintSelf(Graphics gImage) {
        if (count<16){
            super.image=explode;
            gImage.drawImage(image,x,y,width,height,null);//要可以设置大小
            count++;
        }
        //super.paintSelf(gImage);
    }

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

工具类
package utils;

import object.*;

import javax.tools.Tool;

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

public class GameUtils {
    //背景图片
    public static Image bgImage = Toolkit.getDefaultToolkit().getImage("Images/backGroup.jpg");
    //BOSS图片
    public static Image bossImage = Toolkit.getDefaultToolkit().getImage("Images/boss.png");
    //动态爆炸图片
    public static Image boomImage = Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
    //我方飞机爆炸图片
    public static Image boom_2Image = Toolkit.getDefaultToolkit().getImage("Images/boom2.png");
    //敌方飞机爆炸图片
    public static Image boom_3Image = Toolkit.getDefaultToolkit().getImage("Images/boom3.png");
    //己方飞机
    public static Image planeImage=Toolkit.getDefaultToolkit().getImage("Images/plane2.png");
    //我方子弹
    public static Image shellImage=Toolkit.getDefaultToolkit().getImage("Images/shell.png");
    //敌方飞机
    public static Image enemyImage=Toolkit.getDefaultToolkit().getImage("Images/enemy.png");
    //敌方子弹
    public static Image bulletImage=Toolkit.getDefaultToolkit().getImage("Images/bullet.png");
    //物体的集合
    public static List<GameObject> gameObjList=new ArrayList<>();
    //子弹的集合
    public static List<shellObj> shellObjList=new ArrayList();
    //敌方飞机集合
    public static List<enemyObj> enemyObjList=new ArrayList();
    //Boss子弹的集合
    public static List<BulletObj> bulletObjList=new ArrayList();
    //需要移除的目标的集合
    public static List<GameObject> removeObjList=new ArrayList<>();
    //敌机爆炸的集合
    public static List<ExplodeObj> explodeObjList=new ArrayList<>();

//写字
    public static void DrawWord(Graphics gGrafics,String word,Color color,int size,int x,int y){
        gGrafics.setColor(color);//设置字体颜色
        gGrafics.setFont(new Font("仿宋",Font.BOLD,size));
        gGrafics.drawString(word,x,y);
    }
}

窗口类
package utils;

import object.*;

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 GameWindow extends JFrame {
    //0未开始,1游戏中,2暂停,3通关失败,4,通关成功
    public static int state=0;
    //分数
    public static int score=0;
    //生成多少飞机后生成boss
    int num=100;
    int width=600;
    int height=600;
    //画布重画的次数
    int count=1;
    //敌机生成数目
    int enemy_count=0;
    //开双缓存的图片
    Image offImage=null;
    //背景
    bgObj bgOb =new bgObj(GameUtils.bgImage,0,0,2);
    //玩家飞机
    public Plane planeObj =new Plane(GameUtils.planeImage,100,100,30,40,0,this);
    //Boss
    public BossObj bossObj=null;
    public void Start(){
        this.setVisible(true);
        this.setSize(width,height);//大小
        this.setLocationRelativeTo(null);//设置在屏幕中间
        this.setTitle("飞机大战之Boss决战篇");
        //关掉后清理缓存
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //添加一个监听器MouseAdapter,然后重写他的方法
        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()==' '){//按下空格就暂停,暂停就相当于不再重画画布
                    switch (state){
                        case 1:
                            state=2;
                            break;
                        case 2:
                            state=1;
                            break;
                        default:
                    }
                }
            }
        });
        GameUtils.gameObjList.add(bgOb);
        GameUtils.gameObjList.add(planeObj);

while (true){
            if (state==1||state==0){
                if (state==1){
                    creatGameObj();
                }
                repaint();
            }
            try {
                Thread.sleep(25);//每秒调用40次
            } catch (InterruptedException ex) {
                System.out.println("图片解析出错,错误原因:"+ex.getMessage());
            }
        }
    }

@Override
    public void paint(Graphics g) {
        if (offImage==null){
            offImage=createImage(width,height) ;
        }
        Graphics gGraphic=offImage.getGraphics();//使用两张图片用双缓存来画防止闪烁
        gGraphic.fillRect(0,0,width,height);
        //游戏未开始时绘图
        if (state==0){
            try {
                MediaTracker mediaTracker = new MediaTracker(new Container());
                mediaTracker.addImage(GameUtils.bgImage, 0);
                mediaTracker.addImage(GameUtils.bossImage, 1);
                mediaTracker.addImage(GameUtils.boomImage, 2);
                mediaTracker.waitForAll();
                gGraphic.drawImage(GameUtils.bgImage,0,0,null);//让图片加载完以后再显示
                gGraphic.drawImage(GameUtils.bossImage,225,100,150,150,null);//boss
                gGraphic.drawImage(GameUtils.boomImage,250,380,100,100,null);//爆炸
                GameUtils.DrawWord(gGraphic,"点击此处游戏开始",Color.blue,35,170,320);//文字标识
            } catch (InterruptedException ex) {
                System.out.println("图片解析出错,错误原因:"+ex.getMessage());
            }
        }
        //游戏开始时绘图
        else if (state==1){
            GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
            //将物件集合中所有东西画出
            for (int i=0;i<GameUtils.gameObjList.size();i++){
                GameUtils.gameObjList.get(i).paintSelf(gGraphic);
            }
            //分数显示
            GameUtils.DrawWord(gGraphic,"得分: "+score+" 分",Color.green,30,30,100);//文字标识
            //将所有要移除的物件移除
            GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
        }
        //暂停
        else if (state==2){
            //不做任何事就是暂停
        }
        //游戏结束
        else if (state==3){
            try {
                //让场景保持不变
                for (int i=0;i<GameUtils.gameObjList.size();i++){
                    GameUtils.gameObjList.get(i).paintSelf(gGraphic);
                }
                //额外画出一些字
                MediaTracker mediaTracker = new MediaTracker(new Container());
                mediaTracker.addImage(GameUtils.boom_2Image, 0);
                mediaTracker.waitForID(0);
                gGraphic.drawImage(GameUtils.boom_2Image,planeObj.getX()-20, planeObj.getY()-15, null);//爆炸
                GameUtils.DrawWord(gGraphic,"菜",Color.red,80,270,320);//文字标识
            } catch (InterruptedException ex) {
                System.out.println("图片解析出错,错误原因:"+ex.getMessage());
            }
        }
        //游戏通关
        else if (state==4){
            try {
                //先让不该出现的东西消失
                GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
                //让场景保持不变
                for (int i=0;i<GameUtils.gameObjList.size();i++){
                    GameUtils.gameObjList.get(i).paintSelf(gGraphic);
                }
                //额外画出一些字
                MediaTracker mediaTracker = new MediaTracker(new Container());
                mediaTracker.addImage(GameUtils.boom_3Image, 0);
                mediaTracker.waitForID(0);
                gGraphic.drawImage(GameUtils.boom_3Image,bossObj.getX()+30, bossObj.getY(), null);//爆炸
                GameUtils.DrawWord(gGraphic,"虽然你赢了,但你还是菜",Color.red,30,140,320);//文字标识
            } catch (InterruptedException ex) {
                System.out.println("图片解析出错,错误原因:"+ex.getMessage());
            }
        }
        g.drawImage(offImage,0,0,null);
        count++;
    }

void creatGameObj(){
        //我方子弹的生成
        if (count%15==0){
            GameUtils.shellObjList.add(new shellObj(GameUtils.shellImage,planeObj.getX()+8, 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.enemyImage,(int)(Math.random()*12)*50, 0,49,36,5,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
            enemy_count++;
        }
        //敌方子弹的生成
        if (bossObj!=null&&count%15==0){
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImage,bossObj.getX()+76, bossObj.getY()+85,14,29,5,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
        }
        if (enemy_count>num&&bossObj==null){
            bossObj=new BossObj(GameUtils.bossImage,250,35,155,100,5,this);
            GameUtils.gameObjList.add(bossObj);
        }
    }

}
主程序
import object.*;
import utils.*;
public class PlanWarMain {
    public static void main(String[] args) {
        GameWindow gWin=new GameWindow();
        gWin.Start();
    }
}

效果图
没有长的可以循环的背景就随便弄了个背景(当然,是懒得做,诶嘿)

Java打飞机小游戏相关推荐

  1. java打飞机小游戏(跟着网上视频写到)

    package 飞机小游戏; //如果觉得好的话,给一点积分哦,太痛苦了,平时下一点资料都得需要 import java.awt.Color; import java.awt.Font; import ...

  2. Java打飞机小游戏(附完整源码)

    写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐.代码写的很 ...

  3. JAVA实现飞机小游戏

    通过该项目让我对什么是面向对象思想有了深刻的认识,对监听器,多线程开发,缓冲区处理,锁等有了深入了解. 1.先将UI界面类构造出来 ```java public class UI {public vo ...

  4. 用JAVA写个飞机小游戏玩玩吧

    闲来无事写了个飞机小游戏. 设定很简单,子弹打到飞机就结束游戏. 实现步骤如下,有问题可以评论区讨论: 首先建立一个PlayGame项目和它之下的包: MyGameFrame(游戏实现)类: pack ...

  5. Java Swing 经典小游戏《飞机大战》———— (四)碰撞检测 游戏状态与得分 玩家升级

    前期回顾 Java Swing 经典小游戏<飞机大战>---- (一)获取素材,创建窗口,添加滚动背景,双缓冲 Java Swing 经典小游戏<飞机大战>---- (二)玩家 ...

  6. Java飞机小游戏制作简单实现详细小结

    567881@ftJava小游戏开发 Java飞机小游戏制作简单实现小结 本人原来是个技术小白,寒假我跟着高淇老师的Java300集开始自学Java,跟着做了一个飞机大战小游戏,已经实现了飞机的自由移 ...

  7. Java版打飞机小游戏

    放假写的一个Java端打飞机小游戏. 复习到的知识点有:java图形界面,多线程,集合框架等. 主要的收获是知道了处理图层的方式,即JLayeredPane层次面板,主要思路 如下: 1.       ...

  8. 编程语言用 Java 开发一个打飞机小游戏(附完整源码)

    编程语言用 Java 开发一个打飞机小游戏(附完整源码) 上图 写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过 ...

  9. 基于Java的飞机大战游戏的设计与实现论文

    源码下载 http://www.byamd.xyz/hui-zong-1/ 摘 要 现如今,随着智能手机的兴起与普及,加上4G(the 4th Generation mobile communicat ...

最新文章

  1. SVO学习笔记(一)
  2. MySQL和PostgreSQL数据库安全配置
  3. Linux 0.12内核的内存管理基础
  4. 相当全面:推荐系统干货总结
  5. .NET遗留应用改造——性能优化篇
  6. 在Oracle中利用SQL_TRACE跟踪SQL的执行
  7. Ecstore跳过后台激活验证和shopexId授权
  8. MySQL下bin-log的三种模式(ROW、Statement、Mixed)
  9. 关于类的sizeof问题
  10. 自定义圆形进度条ProgressBar
  11. coreldrawx4缩略图显示不出来_CDR缩略图不显示怎么办?CorelDRAW缩略图不显示解决办法 - 优优下载站...
  12. 高性能mysql之慎用BLOB与TEXT
  13. 炫酷的个人主页要怎么制作 ? |GitCode
  14. C语言编程从键盘输入n
  15. 谷歌 I/O 深度解析:Android Jetpack 最新变化
  16. switch-case的使用
  17. 数据可视化分析工具如何在国内弯道超车,迅速崛起?
  18. supplier java8_Java 8之 Supplier示例
  19. 软考初级程序员上午单选题(20)
  20. linux 知识摘要

热门文章

  1. Linux中的括号用法
  2. D - Denouncing Mafia DFS
  3. win10重置进度条不动了_win10重置卡在100%不动没反应怎么办
  4. Hyperf 热更新Watcher
  5. springBoot配置全局异常处理器
  6. java租车系统维熵科技_维熵租车系统数据库
  7. 使用python解密pdf
  8. 深入浅出Spring Aop
  9. 3.3V和1.8V电平双向转换
  10. Linux——用户的特殊shell与PAM模块