前言:
飞机大战:是一款经典飞行射击类游戏,整体环境围绕太空,通过控制飞机,击落敌方战斗机,最终达到胜利,为玩家呈现一场不一样射击体验。
Java技术:变量、j数据类型、判断语句、循环结构、数组、集合、简单窗口创建、图形图片绘制、双缓存技术、事件-鼠标和键盘事件、物体的碰撞检测。
非常适合在同学Java学习结束后,作为不错的结课作业或者练手项目。
代码已经在文章结束语后打包,有兴趣的同学可以免费下载,创作不易,点个免费的赞支持一下!
运行环境:jdk-17.05
操作方法:鼠标控制小鱼方向
游戏主要框架:
游戏素材:
游戏代码:
(1)BgObj类
import java.awt.*;
public class BgObj extends GameObj {
@Override //实现背景循环播放
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
y += speed;
if (y >= 0) {
y = -2000;
}
}
public BgObj() {
super();
}
public BgObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
}
(2)BossObj类
import java.awt.*;
public class BossObj extends GameObj {
//Boss生命值
int life = 15;
@Override
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
if (x > 550 || x < -50) {
speed = -speed;
}
x += speed;
for (ShellObj shellObj : GameUtils.shellObjList) {
//我方子弹与Boss进行碰撞检测
if (this.getRec().intersects(shellObj.getRec())) {
shellObj.setX(-100);
shellObj.setY(100);
GameUtils.removeList.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 / 20, 10);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
public BossObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
}
(3)BulletObj类
import java.awt.*;
public class BulletObj extends GameObj {
@Override
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
y += speed;
//敌方子弹与我方飞机的碰撞检测
if (this.getRec().intersects(this.frame.planeObj.getRec())) {
GameWin.state = 3;
}
//敌方子弹的越界消失 条件 y > 600 改变后的坐标(-300,300)
if (y > 600) {
this.x = -300;
this.y = 300;
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
public BulletObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
}
(4)EnemyObj类
import java.awt.*;
public class EnemyObj extends GameObj {
@Override
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
y += speed;
//敌我飞机碰撞检测
if (this.getRec().intersects(this.frame.planeObj.getRec())) {
GameWin.state = 3;
}
//敌方的越界消失 判断条件 y > 600 改变后的坐标(-200,-200)
if (y > 600) {
this.x = -200;
this.y = 200;
GameUtils.removeList.add(this);
}
//敌方消失前移动到(-200,-200) 我方子弹(-100,100)
for (ShellObj shellObj : GameUtils.shellObjList) {
if (this.getRec().intersects(shellObj.getRec())) {
ExplodeObj explodeObj = new ExplodeObj(x, y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
System.out.println("碰撞了");
shellObj.setX(-100);
shellObj.setY(100);
this.x = -200;
this.y = -200;
GameUtils.removeList.add(shellObj);
GameUtils.removeList.add(this);
GameWin.score++;
}
}
}
@Override
public Rectangle getRec() {
return new Rectangle(x, y, width, height);
}
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);
}
}
(5)ExplodeObj类
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("src/imgs/explode/e" + (i + 1) + ".gif");
}
}
public ExplodeObj(int x, int y) {
super(x, y);
}
@Override
public void painSelf(Graphics gImage) {
if (explodeCount < 16) {
super.img = pic[explodeCount];
super.painSelf(gImage);
explodeCount++;
}
}
}
(6)GameObj类
import java.awt.*;
public class GameObj {
Image img;
int x;
int y;
int width;
int height;
double speed;
GameWin frame;
public void painSelf(Graphics gImage) {
gImage.drawImage(img, x, y, null);
}
public Rectangle getRec() {
return new Rectangle(x, y, width, height);
}
/*
各种子对象的实例方法
*/
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 Image getImg() {
return 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;
}
}
(7)GameUtils类
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
public class GameUtils {
//背景图片
public static Image bgImg = Toolkit.getDefaultToolkit().getImage("src/imgs/bg.jpg");
//boss图片
public static Image bossImg = Toolkit.getDefaultToolkit().getImage("src/imgs/boss.png");
//爆炸图片
public static Image explodeImg = Toolkit.getDefaultToolkit().getImage("src/imgs/explode/e6.gif");
//我方战斗机图片
public static Image planeImg = Toolkit.getDefaultToolkit().getImage("src/imgs/plane.png");
//敌方战斗机图片
public static Image enemyImg = Toolkit.getDefaultToolkit().getImage("src/imgs/enemy.png");
//我方子弹图片
public static Image shellImg = Toolkit.getDefaultToolkit().getImage("src/imgs/shell.png");
//敌方子弹图片
public static Image bulletImg = Toolkit.getDefaultToolkit().getImage("src/imgs/bullet.png");
//要删除元素的集合
public static List<GameObj> removeList = new ArrayList<>();
//所有游戏物体的集合
public static List<GameObj> gameObjList = new ArrayList<>();
//我方子弹的集合
public static List<ShellObj> shellObjList = new ArrayList<>();
//敌方子弹的集合
public static List<BulletObj> bulletObjList = 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);
}
}
(8)GameWin类
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;
//双缓存的图片
Image offScreenImage = null;
//窗口的宽度
int width = 600;
int height = 600;
//背景图对象
BgObj bgobj = new BgObj(GameUtils.bgImg, 0, -2000, 2);
//我方飞机对象
PlaneObj planeObj = new PlaneObj(GameUtils.planeImg, 290, 550, 20, 30, 0, this);
//敌方boss对象
public BossObj bossObj = null;
//游戏的重绘次数
int count = 1;
//敌机出现的数量
int enemyCount = 0;
public void launch() {
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(width, height);
//关闭窗口立即终止程序
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置窗口位置
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:
}
}
}
});
//重复绘制
while (true) {
if (state == 1) {
creatObj();
repaint();
}
try {
Thread.sleep(25);
} catch (Exception 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.pink, 40, 180, 300);
}
if (state == 1) {
GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
//运行中
for (int i = 0; i < GameUtils.gameObjList.size(); i++) {
GameUtils.gameObjList.get(i).painSelf(gImage);
System.out.println(GameUtils.gameObjList.size());
}
GameUtils.gameObjList.removeAll(GameUtils.removeList);
}
if (state == 3) {
//失败
gImage.drawImage(GameUtils.explodeImg, planeObj.getX() + 15, planeObj.getY() + 10, null);
GameUtils.drawWord(gImage, "GAME OVER!", Color.RED, 50, 180, 300);
}
if (state == 4) {
//通关
gImage.drawImage(GameUtils.explodeImg, bossObj.getX() + 30, bossObj.getY(), null);
GameUtils.drawWord(gImage, "游戏通关!", Color.pink, 50, 190, 300);
}
GameUtils.drawWord(gImage, score + " 分", Color.white, 35, 25, 90);
g.drawImage(offScreenImage, 0, 0, null);
count++;
}
void creatObj() {
if (count % 15 == 0) {
GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, planeObj.getX() + 35, planeObj.getY(), 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, 49, 36, 5, this));
GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size() - 1));
enemyCount++;
}
if (count % 20 == 0 && bossObj != null) {
GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg, bossObj.getX() + 83, bossObj.getY() + 83, 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, 4, this);
GameUtils.gameObjList.add(bossObj);
}
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
(9)PlaneObj类
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PlaneObj extends GameObj {
@Override
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
//我方飞机与敌方boss的碰撞检测
if (this.frame.bossObj != null && this.getRec().intersects(this.frame.bossObj.getRec())) {
GameWin.state = 3;
}
}
@Override //重写我方战斗机矩形图片的大小
public Rectangle getRec() {
return new Rectangle(x + 28, y + 10, width + 5, height + 5);
}
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.frame.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
PlaneObj.super.x = e.getX() - 42;
PlaneObj.super.y = e.getY() - 35;
}
});
}
@Override
public Image getImg() {
return super.getImg();
}
}
(10)ShellObj类
import java.awt.*;
public class ShellObj extends GameObj {
@Override
public void painSelf(Graphics gImage) {
super.painSelf(gImage);
y -= speed;
if (y < 0) {
this.x = -100;
this.y = 100;
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
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 Image getImg() {
return super.getImg();
}
}