前言

使用java语言编写一个小游戏,要先理解java是一款面向对象的语言,和C语言不同,所以编写飞机大战时要有工程化思想。

框架



具体工程框架

敌机对象

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;import batecy.GameWin;
import batecy.utils.GameUtils;public class EnemyObj extends GameObj{public EnemyObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);}@Overridepublic void paintSelf(Graphics gImage) {super.paintSelf(gImage);this.setY(this.getY() + this.getSpeed());if (this.getY() >= 650) {GameUtils.removeList.add(this);}if (this.getRectangle().intersects(this.getFrame().planeObj.getRectangle())) {//敌机和我方飞机发生碰撞扣血PlaneObj.life=PlaneObj.life-20;if(PlaneObj.life<=0) {GameWin.state = 3;}}for (GameObj gameObj: GameUtils.shellObjList) {if (this.getRectangle().intersects(gameObj.getRectangle())) {GameWin.score ++;ExplodeObj explodeObj = new ExplodeObj(this.getX(), this.getY());GameUtils.explodeObjList.add(explodeObj);this.setX(-100);this.setY(100);gameObj.setX(-100);gameObj.setY(200);//将发生碰撞的敌机和我方子弹添加到要删除的元素的数组中GameUtils.removeList.add(this);GameUtils.removeList.add(gameObj);//当分数超过50时,游戏通关if(GameWin.score>=50){GameWin.state=4;}}}}}

敌机boss对象

package batecy.obj;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;import batecy.GameWin;
import batecy.utils.GameUtils;public class BossObj extends GameObj{static int life = 100;public BossObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);}@Overridepublic void paintSelf(Graphics gImage) {super.paintSelf(gImage);//添加boss的血条gImage.setColor(Color.white);gImage.fillRect(20,40,100,10);gImage.setColor(Color.red);gImage.fillRect(20,40,life*100/100,10);this.setX(this.getX() + this.getSpeed());if (this.getX() > 550  || this.getX() < -50) {this.setSpeed(-this.getSpeed());}for (GameObj gameObj : GameUtils.shellObjList) {if (this.getRectangle().intersects(gameObj.getRectangle())) {life --;gameObj.setX(-100);gameObj.setY(400);if (life <= 0) {GameWin.state = 4;}GameUtils.removeList.add(gameObj);}}}}

英雄机对象

package batecy.obj;import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;import batecy.GameWin;public class PlaneObj extends GameObj{static int life=100;public PlaneObj() {super();// TODO Auto-generated constructor stub}public PlaneObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);this.getFrame().addMouseMotionListener(new MouseAdapter() {@Overridepublic void mouseMoved(MouseEvent e) {PlaneObj.super.setX(e.getX() - 11);PlaneObj.super.setY(e.getY() - 16);}});}@Overridepublic void paintSelf(Graphics gImage) {super.paintSelf(gImage);//添加自身的血条gImage.setColor(Color.white);gImage.fillRect(460,40,120,10);gImage.setColor(Color.green);gImage.fillRect(460,40,life*120/100,10);//我方飞机和敌方boss 碰撞检测if (this.getRectangle().intersects(this.getFrame().boss.getRectangle())) {GameWin.state = 3;}}
}

飞行物通用

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;import batecy.GameWin;public class GameObj {private  Image img;private  int x;private int y;private int width;private int height;private int speed;private GameWin frame;public GameObj() {super();// TODO Auto-generated constructor stub}public GameObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super();this.img = img;this.x = x;this.y = y;this.width = width;this.height = height;this.speed = speed;this.frame = frame;}public GameObj(int x, int y) {super();this.x = x;this.y = y;}public Rectangle getRectangle() {return new Rectangle(x, y, width, height);}public void paintSelf(Graphics graphics) {graphics.drawImage(img, x, y, null);}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 int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public GameWin getFrame() {return frame;}public void setFrame(GameWin frame) {this.frame = frame;}}

英雄机子弹

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;import batecy.GameWin;
import batecy.utils.GameUtils;public class ShellObj extends GameObj{public ShellObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);}@Overridepublic void paintSelf(Graphics graphics) {super.paintSelf(graphics);this.setY(this.getY() - this.getSpeed());if (this.getY() <= 0) {GameUtils.removeList.add(this);}}}

敌机子弹

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;import batecy.GameWin;
import batecy.utils.GameUtils;public class BulletObj extends GameObj{public BulletObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);// TODO Auto-generated constructor stub}@Overridepublic void paintSelf(Graphics gImage) {super.paintSelf(gImage);this.setY(this.getY() + this.getSpeed());if (this.getRectangle().intersects(this.getFrame().planeObj.getRectangle())) {GameWin.state = 3;this.setX(-100);this.setY(100);GameUtils.removeList.add(this);GameUtils.removeList.add(this.getFrame().planeObj);}if (this.getY() >= 650) {GameUtils.removeList.add(this);}}}

爆炸效果

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;import batecy.utils.GameUtils;public class ExplodeObj extends GameObj{static Image [] pic = new Image[16];int explodeCount = 0;public ExplodeObj(int x, int y) {super(x, y);}static {for (int i = 1; i < 17; i++) {pic[i-1] = Toolkit.getDefaultToolkit().getImage("imgs/explode/e"+i+".gif");}}@Overridepublic void paintSelf(Graphics gImage) {if (explodeCount < 16) {super.setImg(pic[explodeCount]);super.paintSelf(gImage);explodeCount ++;}else {this.setX(-100);GameUtils.removeList.add(this);}}}

背景位移

package batecy.obj;import java.awt.Graphics;
import java.awt.Image;import batecy.GameWin;public class BgObj extends GameObj{public BgObj(Image img, int x, int y, int width, int height, int speed, GameWin frame) {super(img, x, y, width, height, speed, frame);}@Overridepublic void paintSelf(Graphics graphics) {super.paintSelf(graphics);this.setY(this.getY()+this.getSpeed());if (this.getY() >= 0) {this.setY(-2000);}}}

背景音乐

package batecy;import java.io.File;
import java.io.IOException;import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;public class Music extends Thread {private String fileName;private final int EXTERNAL_BUFFER_SIZE = 524288;public Music(String wavFile) {this.fileName = wavFile;}public void run() {File soundFile = new File(fileName); // 播放音乐的文件名if (!soundFile.exists()) {System.err.println("Wave file not found:" + fileName);return;}while (true) {      // 设置循环播放AudioInputStream audioInputStream = null; // 创建音频输入流对象try {audioInputStream = AudioSystem.getAudioInputStream(soundFile); // 创建音频对象} catch (UnsupportedAudioFileException e1) {e1.printStackTrace();return;} catch (IOException e1) {e1.printStackTrace();return;}AudioFormat format = audioInputStream.getFormat(); // 音频格式SourceDataLine auline = null; // 源数据线DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);try {auline = (SourceDataLine) AudioSystem.getLine(info);auline.open(format);} catch (LineUnavailableException e) {e.printStackTrace();return;} catch (Exception e) {e.printStackTrace();return;}if (auline.isControlSupported(FloatControl.Type.PAN)) {FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);}auline.start();int nBytesRead = 0;byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];try {while (nBytesRead != -1) {nBytesRead = audioInputStream.read(abData, 0, abData.length);if (nBytesRead >= 0)auline.write(abData, 0, nBytesRead);}} catch (IOException e) {e.printStackTrace();return;} finally {auline.drain();auline.close();}}}
}

游戏加载

package batecy.utils;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;import batecy.obj.ExplodeObj;
import batecy.obj.GameObj;public class GameUtils {public static Image bgImage = Toolkit.getDefaultToolkit().getImage("imgs/bg.jpg");public static Image bossImage = Toolkit.getDefaultToolkit().getImage("imgs/boss.png");public static Image planeImage = Toolkit.getDefaultToolkit().getImage("imgs/rocket.png");public static Image shellImage = Toolkit.getDefaultToolkit().getImage("imgs/shell.png");public static Image enemyImage = Toolkit.getDefaultToolkit().getImage("imgs/enemy.png");public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("imgs/explode/e9.gif");public static Image bulletImage = Toolkit.getDefaultToolkit().getImage("imgs/bullet.png");//存放所有出现在游戏界面的元素public static ArrayList<GameObj> gameObjList = new ArrayList<>();//存放所有子弹public static ArrayList<GameObj> shellObjList = new ArrayList<>();//存放所有子弹public static ArrayList<GameObj> enemyObjList = new ArrayList<>();//存放索要删除的元素public static ArrayList<GameObj> removeList  = new ArrayList<>();//存放索要删除的元素public static ArrayList<GameObj> bulletList  = new ArrayList<>();//存放爆炸效果的图片public static ArrayList<ExplodeObj> explodeObjList = new ArrayList<>();public static void drawWord(Graphics gImage,String str,Color color,int size,int x,int y) {gImage.setFont(new Font("仿宋", Font.BOLD, 40));gImage.setColor(color);gImage.drawString(str, x, y);}}

游戏界面

package batecy;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;import batecy.obj.BgObj;
import batecy.obj.BossObj;
import batecy.obj.BulletObj;
import batecy.obj.EnemyObj;
import batecy.obj.GameObj;
import batecy.obj.PlaneObj;
import batecy.obj.ShellObj;
import batecy.utils.GameUtils;//游戏说明:
//玩法:移动战机打小飞机和BOSS,在攻击过程中要注意躲避子弹,每击落一架飞机得一分
//通关条件:得分超过50或击败BOSSpublic class GameWin extends JFrame {//游戏状态 0未开始 1游戏中 2暂停 3.通关失败 4.通关成功public static int state = 0;//记录窗口重绘的次数public static int count = 0;public static int score = 0;boolean life = true;public PlaneObj planeObj = null;public BossObj boss = null;public GameWin() {BgObj bg = new BgObj(GameUtils.bgImage, 0, -2000, 600, 500, 5, this);boss = new BossObj(GameUtils.bossImage, 250, 35, 155, 100, 5, this);planeObj = new PlaneObj(GameUtils.planeImage, 290, 550, 20, 30, 0, this);GameUtils.gameObjList.add(bg);GameUtils.gameObjList.add(planeObj);GameUtils.gameObjList.add(boss);}public void launch() {this.setSize(600, 600);this.setVisible(true);this.setLocationRelativeTo(null);this.setTitle("2022终极雷电");//背景音乐启动Music audioPlayWave = new Music("12240.wav");audioPlayWave.start();//鼠标监测游戏开始this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {if (life) {state = 1;life = false;}}});while (true) {try {Thread.sleep(25);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}repaint();}}@Overridepublic void paint(Graphics g) {Image offScreenImage = null;if (offScreenImage == null) {offScreenImage = this.createImage(600, 600);}Graphics gImage = offScreenImage.getGraphics();gImage.drawImage(GameUtils.bgImage, 0, 0, null);if (state == 0) {gImage.drawImage(GameUtils.bossImage, 220, 120, null);GameUtils.drawWord(gImage, "单击开始游戏", Color.white, 40, 180, 300);}if (state == 3) {gImage.drawImage(GameUtils.explodeImg, 260, 220, null);GameUtils.drawWord(gImage, "GAME OVER!", Color.white, 40, 180, 300);GameUtils.drawWord(gImage, "您的得分" + score, Color.red, 40, 200, 360);}if (state == 4) {gImage.drawImage(GameUtils.explodeImg, 260, 220, null);GameUtils.drawWord(gImage, "游戏通关", Color.white, 40, 220, 400);GameUtils.drawWord(gImage, "您的得分" + score, Color.red, 40, 200, 360);}if (state == 1) {createObj();GameUtils.gameObjList.addAll(GameUtils.explodeObjList);for (GameObj obj : GameUtils.gameObjList) {obj.paintSelf(gImage);}GameUtils.drawWord(gImage, "分数:" + score, Color.green, 30, 30, 100);}GameUtils.gameObjList.removeAll(GameUtils.removeList);g.drawImage(offScreenImage, 0, 0, null);}public void createObj() {count++;if (GameWin.count % 3 == 0) {ShellObj shellObj = new ShellObj(GameUtils.shellImage, planeObj.getX() + 4, planeObj.getY() - 29, 14, 29, 12, this);GameUtils.shellObjList.add(shellObj);GameUtils.gameObjList.add(shellObj);}if (GameWin.count % 10 == 0) {EnemyObj enemyObj = new EnemyObj(GameUtils.enemyImage, (int) (Math.random() * 7) * 87 + 10, 40, 49, 36, 5, this);GameUtils.enemyObjList.add(enemyObj);GameUtils.gameObjList.add(enemyObj);}if (GameWin.count % 10 == 0) {BulletObj bulletObj = new BulletObj(GameUtils.bulletImage, boss.getX() + 76, boss.getY() + 86, 15, 25, 7, this);GameUtils.bulletList.add(bulletObj);GameUtils.gameObjList.add(bulletObj);}}public static void main(String[] args) {new GameWin().launch();}
}

运行效果



结语

以上是框架及代码部分的分享,需要素材的朋友可以私信。

java飞机大战小游戏相关推荐

  1. 写一个JAVA飞机大战小游戏AirPlaneWar

    纷争开始了 文章目录 开发环境 前言 一.代码部分 1.项目结构及下载方法 2.代码 1.AirPlane.java 2.Award.java 3.Bee.java 4.BigAirPlane.jav ...

  2. Java飞机大战小游戏练习

    (根据尚学堂学习制作,侵删) 飞机游戏的主窗口: package cn.ldd.game;import java.awt.Color; import java.awt.Font; import jav ...

  3. 基于Java语言在窗体上实现飞机大战小游戏

    全套资料下载地址:https://download.csdn.net/download/sheziqiong/85594271 项目介绍 飞机大战:用 Java 语言在窗体上实现飞机大战小游戏,运行程 ...

  4. 【Java代码实现飞机大战小游戏】简单理解

    飞机大战 飞机大战小游戏历经10天完成,主要用到的就是我们面向对象部分的知识:类,封装,继承,多态,静态代码块等等内容+swing部分内容.所以即使你是java小白,也不用担心欧! 游戏说明:游戏有3 ...

  5. 【Java】Java基础飞机大战小游戏完整代码

    Java基础飞机大战小游戏完整代码 先来展示一下代码实现结果图 主函数ShootGame 初始化游戏原始背景图片,游戏人物图片,游戏开始结束图片:构建产生敌人算法:产生英雄机算法:发射子弹算法:判断是 ...

  6. 点击list view中一行内容可以在combox中显示_java版飞机大战小游戏详细教程(零基础小白也可以分分钟学会!)...

    一:游戏展示 飞机大战小游戏我们都玩过,通过移动飞机来打敌机,这里给大家展示一下游戏成果:呜呜呜由于gif只能上传5M大小,所以就不能给大家展示操作了,如果大家有兴趣可以自己自己做出来再玩哟. 这里面 ...

  7. Java 飞机射击小游戏

    文章目录 前言 一.系统分析 问题描述 总体设计 功能流程图 二.程序和算法的介绍 FlyingObject(本机和所有敌机父类) Airplane(小敌机类) Bee(小蜜蜂敌机类) Bullet( ...

  8. python 飞机大战小游戏

    飞机大战小游戏,这里需要下载pygame模块 这是需要的素材,需要的自取: 上代码: import time import pygame from pygame.locals import *#检测事 ...

  9. 飞机大战小游戏(超详细)

    偷学Python之最后的项目二:飞机大战小游戏(超详细) 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志.--苏轼 甜甜先说 这次用Python中的pygame模块来完成一个飞机大战的小游戏:基本思 ...

  10. matlab飞机大战小游戏(第二版)

    第一版链接:https://blog.csdn.net/slandarer/article/details/88025006 游戏截图: ------------------------ 游戏动图: ...

最新文章

  1. 一:搭建一套免费的serverless网站
  2. Power Law and Exponential Decay of Inter Contac...
  3. HDU 1084 - What Is Your Grade?
  4. 第十六届全国大学生智能汽车竞赛安徽赛区赛事指南
  5. python绘制3维图-Python 画出来六维图
  6. mysql 开启profiles_MySQL中show profiles的开启
  7. JavaScript 是传值调用还是传引用调用?
  8. 操作系统线程互斥,锁死机制的简单介绍
  9. 【中秋快乐】求问meta-learning和few-shot learning的关系是什么?
  10. react-redux-express异步前后端数据交互(面向初学者,高手勿进)
  11. win7操作系统练习题(带答案,有问题可直接在博客或公众号中问)
  12. javaWeb—9.Git
  13. 二叉树叶子结点个数统计
  14. MySQL数据库表结构设计优化技巧总结
  15. 嵌入式开发需要学习哪些东西
  16. 【C系列6.6】数组训练之金鱼
  17. 2022年执法资格通用法律知识考试判断题专项训练题及答案
  18. KPPW仿一品威客威客模板
  19. 如何把生活过程升级打怪?
  20. PID控制及整定算法

热门文章

  1. android手机命令,使用adb命令操控Android手机(adb命令)
  2. ssl教程易语言代码
  3. Java并发:整理自《Java并发编程实战》和《Java并发编程的艺术》
  4. Python 大数据的进行信用卡欺诈检测(附源码与注释)
  5. Android SDK 下载安装及配置
  6. HTML转义字符最全集合
  7. 基于kali linux 跑字典暴力破解wifi教程
  8. java获取当前上一周、上一月、上一年的时间dxl
  9. 全球IP地址规则和分配
  10. linux命令学习之---- file