前言
使用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);
}
@Override
public 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);
}
@Override
public 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() {
@Override
public void mouseMoved(MouseEvent e) {
PlaneObj.super.setX(e.getX() - 11);
PlaneObj.super.setY(e.getY() - 16);
}
});
}
@Override
public 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);
}
@Override
public 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
}
@Override
public 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");
}
}
@Override
public 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);
}
@Override
public 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或击败BOSS
public 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() {
@Override
public void mouseClicked(MouseEvent e) {
if (life) {
state = 1;
life = false;
}
}
});
while (true) {
try {
Thread.sleep(25);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
repaint();
}
}
@Override
public 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();
}
}
运行效果
结语
以上是框架及代码部分的分享,需要素材的朋友可以私信。