http://【【尚学堂】Java飞机大战3.0重制版,1小时快速上手掌握,源码_课件_资料,基础小白必学项目,毕设练手项目经验_Java项目_java入门_游戏开发】https://www.bilibili.com/video/BV1aX4y167Y3?vd_source=fc0c1b2d853ba00c1f9a210df63c3e86 代码参考了尚学堂飞机大战3.0版本,同时在此基础上添加了自己的一些想法,游戏失败或者通关后游戏重新开始,添加背景音乐。其他的没做太大变动。代码主要是碰撞有点多,容易漏写,其他的还行。
目录
思维导图
游戏思维导图写的非常非常详细,图片有点放不下。可以看一下,对做游戏有很大帮助
游戏解析:
##我方子弹有三种形态,一级子弹shell,二级子弹doubleshell,三级子弹tripleshell。
##敌方有5种形态,敌机1enemy1,敌机2enemy2, littleboss1,littleboss2,最终大boss。
##敌机2,littleboss1,littleboss2,boss可以发射子弹,但是boss2发射的是追踪导弹。
##我方可以击毁littleboss2发射的导弹。
##击败littleboss1,littleboss2后会产生道具,吃到道具后会我方飞机会改变子弹形态。
##最终大boss出现前会有一个警示出现。
##击败敌方时,会产生爆炸图
##点击游戏,音乐开始;关闭游戏,音乐结束。
我的程序在IEDA就是这样,类没有分的很细,但是也做到了见名知意
一,主窗口类
import javax.sound.sampled.*;
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;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Scanner;
public class GameWin extends JFrame {
//记录游戏状态的变量
//0未开始 1游戏中,2暂停,3失败,4通关
public static int state = 0;
//背景图对象
BgObj bgObj = new BgObj(GameUtils.bgImg, 0, -1800, 2);
//定义一张画布
Image offScreenImage = null;
//我方飞机的对象
PlaneObj planeObj = new PlaneObj(GameUtils.planeImg, 37, 41, 290, 550, 0, this);
//记录游戏绘制的次数
int count = 0;
//敌方boss1的对象
LittleBoss1 littleBoss1 = new LittleBoss1(GameUtils.littleBoss1Img, 172, 112, -200, 350, 3, this);
//敌方boss2的对象
LittleBoss2 littleBoss2 = new LittleBoss2(GameUtils.littleBoss2Img, 172, 112, 300, -150, 2, this);
//获取敌方boss对象
BossObj bossObj = new BossObj(GameUtils.bossImg, 240, 174, 180, -180, 2, this);
//获取警示标志的对象
WarningObj waringObj = new WarningObj(GameUtils.warningImg, 599, 90, 0, 350, 0, this);
//定义一个变量来记录我方飞机的索引
public static int planeIndex = 0;
//定义一个变量记录游戏得分
public static int score = 0;
public void launch() {
//窗口是否可见
this.setVisible(true);
//窗口的大小
this.setSize(600, 800);
//窗口的位置
this.setLocationRelativeTo(null);
//窗口的标题
this.setTitle("飞机大战3.0");
//关闭窗口会自动结束进程
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//将所有要绘制的游戏物体全部放入所有元素集合中进行绘制
GameUtils.gameObjList.add(bgObj);
GameUtils.gameObjList.add(planeObj);
playMusic();
//这里拿到我方飞机的索引值
planeIndex = GameUtils.gameObjList.indexOf(planeObj);
//鼠标的点击事件
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 1 && state == 0) {//当我们游戏处于一个未开始的状态下点击才能有反应
state = 1;//游戏开始状态
}
//当游戏失败或者通关时重新开始游戏
if (e.getButton() == 3 && state == 3 || state == 4) {
count = 1;
GameUtils.enemy2ObjList.clear();
GameUtils.shellObjList.clear();
GameUtils.explodeObjList.clear();
GameUtils.littleBoss2BulletList.clear();
GameUtils.littleBoss1BulletList.clear();
GameUtils.tripleShellObjList.clear();
GameUtils.enemy2BulletObjList.clear();
GameUtils.giftObjList.clear();
GameUtils.doubleShellObjList.clear();
GameUtils.gameObjList.clear();
GameUtils.gameObjList.add(bgObj);
GameUtils.gameObjList.add(planeObj);
bossObj = null;
// planeObj=null;
//飞机装上道具的次数归0
planeObj.times = 0;
score = 0;
state = 1;
repaint();
}
}
});
//添加键盘监听事件
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 32) {
if (state == 1) {
state = 2;
} else if (state == 2) {
state = 1;
}
}
}
});
while (true) {
repaint();
try {
Thread.sleep(25);//25毫秒
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
//初始化双缓存图片对象
if (offScreenImage == null) {
offScreenImage = createImage(600, 800);//大小要和游戏窗口大小相同
}
//获取双缓存图片对象的画笔
Graphics gImage = offScreenImage.getGraphics();
//将画布画成一个矩形
gImage.fillRect(0, 0, 600, 800);
if (state == 0) {
gImage.drawImage(GameUtils.bgImg, 0, 0, null);
gImage.drawImage(GameUtils.explodeImg, 270, 350, null);
gImage.drawImage(GameUtils.planeImg, 280, 470, null);
gImage.drawImage(GameUtils.bossImg, 190, 70, null);
//绘制游戏开始界面的文字
gImage.setColor(Color.BLUE);
gImage.setFont(new Font("仿宋", Font.BOLD, 30));
gImage.drawString("鼠标左键开始游戏", 180, 300);
}
if (state == 1) {
createObj();
//bgObj.paintSelf(gImage);
//planeObj.paintSelf(gImage);
//shellObj.paintSelf(gImage);
//将爆炸集合添加到所有元素集合中
GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
//不再单独绘制某个游戏元素,因为所有游戏元素都放入了所有元素集合中,这里只需要将集合中所有元素遍历出来,然后绘制自身即可
for (int i = 0; i < GameUtils.gameObjList.size(); i++) {
GameUtils.gameObjList.get(i).paintSelf(gImage);
}
//将要移除的元素从所有元素集合删除,减小绘制的压力,
//gameObjList和removeList中有很多元素重合,
GameUtils.gameObjList.removeAll(GameUtils.removeList);
count++;
}
//游戏暂停
if (state == 2) {
gImage.drawImage(GameUtils.bgImg, 0, 0, null);
GameUtils.drawWord(gImage, "游戏暂停", Color.YELLOW, 30, 220, 300);
}
//失败
if (state == 3) {
gImage.drawImage(GameUtils.bgImg, 0, 0, null);
GameUtils.drawWord(gImage, "游戏失败", Color.RED, 30, 220, 300);
}
//通关
if (state == 4) {
gImage.drawImage(GameUtils.bgImg, 0, 0, null);
GameUtils.drawWord(gImage, "游戏通关", Color.GREEN, 30, 220, 300);
}
//绘制游戏的积分面板
GameUtils.drawWord(gImage, score + "分", Color.green, 40, 30, 100);
//将双缓存图片绘制在游戏窗口
g.drawImage(offScreenImage, 0, 0, null);
}
//整个方法是用来批量创建物体
void createObj() {
if (count % 15 == 0) {//这里控制子弹产生的速度
if (planeObj.times == 3) {//这里使用的是一级子弹
GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, 14, 29, planeObj.getX() + 12, planeObj.getY() - 20, 40, this));
GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));//添加到所有元素集合中的对象,是新new出来的子弹对象,并不是整个子弹集合
}
if (planeObj.times == 0) {//这里使用的是二级子弹
GameUtils.doubleShellObjList.add(new DoubleShellObj(GameUtils.doubleShellImg, 32, 64, planeObj.getX() + 5, planeObj.getY() - 20, 15, this));
GameUtils.gameObjList.add(GameUtils.doubleShellObjList.get(GameUtils.doubleShellObjList.size() - 1));
}
if (planeObj.times == 1) {//这里使用的是三级子弹
GameUtils.tripleShellObjList.add(new TripleShellObj(GameUtils.tripleShellImg, 64, 182, planeObj.getX() - 5, planeObj.getY() - 100, 15, this));
GameUtils.gameObjList.add(GameUtils.tripleShellObjList.get(GameUtils.tripleShellObjList.size() - 1));
}
}
//两种敌方飞机
if (count % 15 == 0) {//控制敌方小飞机的产生速度
GameUtils.enemy1ObjList.add(new Enemy1Obj(GameUtils.enemy1Img, 32, 24, (int) ((Math.random() * 10) * 60), 0, 5, this));
GameUtils.gameObjList.add(GameUtils.enemy1ObjList.get(GameUtils.enemy1ObjList.size() - 1));//76
}
if (count % 25 == 0) {
if (count % 100 == 0) {
GameUtils.enemy2ObjList.add(new Enemy2Obj(GameUtils.enemy2Img, 44, 67, (int) ((Math.random() * 10) * 60), 0, 3, this));
GameUtils.gameObjList.add(GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1));
}
//只有敌方大飞机子弹产生后,才能产生敌方大飞机子弹
if (GameUtils.enemy2ObjList.size() > 0) {
//这里x,y 产生的坐标就是最新敌方大飞机产生坐标的位置,然后用这个位置产生敌方大飞机的子弹
int x = (GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1)).getX();
int y = (GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1)).getY();
GameUtils.enemy2BulletObjList.add(new Enemy2BulletObj(GameUtils.enemy2BulletImg, 14, 25, x + 17, y + 55, 6, this));
GameUtils.gameObjList.add(GameUtils.enemy2BulletObjList.get(GameUtils.enemy2BulletObjList.size() - 1));
}
}
if (count == 800 && (!GameUtils.gameObjList.contains(bossObj))) {
GameUtils.gameObjList.add(bossObj);
}
if (count == 600 && (!GameUtils.gameObjList.contains(littleBoss2))) {
GameUtils.gameObjList.add(littleBoss2);
}
if (count == 600 && (!GameUtils.gameObjList.contains(littleBoss1))) {
GameUtils.gameObjList.add(littleBoss1);
}
if (count % 15 == 0) {
if (GameUtils.gameObjList.contains(littleBoss1)) {
GameUtils.littleBoss1BulletList.add(new LittleBoss1Bullet(GameUtils.littleBoss1BulletImg, 42, 42, littleBoss1.getX() + 75, littleBoss1.getY() + 100, 4, this));
GameUtils.gameObjList.add(GameUtils.littleBoss1BulletList.get(GameUtils.littleBoss1BulletList.size() - 1));
}
}
if (count % 40 == 0) {
if (GameUtils.gameObjList.contains(littleBoss2)) {
GameUtils.littleBoss2BulletList.add(new LittleBoss2Bullet(GameUtils.littleBoss2BulletImg, 21, 59, littleBoss2.getX() + 78, littleBoss2.getY() + 100, 4, this));
GameUtils.gameObjList.add(GameUtils.littleBoss2BulletList.get(GameUtils.littleBoss2BulletList.size() - 1));
}
}
if (count == 1300 && (!GameUtils.gameObjList.contains(bossObj))) {
GameUtils.gameObjList.add(bossObj);
}
if (count % 20 == 0) {
if (GameUtils.gameObjList.contains(bossObj)) {
//敌方1号boss子弹
GameUtils.littleBoss1BulletList.add(new LittleBoss1Bullet(GameUtils.littleBoss1BulletImg, 42, 42, bossObj.getX() + 10, bossObj.getY() + 130, 4, this));
GameUtils.gameObjList.add(GameUtils.littleBoss1BulletList.get(GameUtils.littleBoss1BulletList.size() - 1));
//敌方2号boss的子弹
//再次取余为了让导弹速度慢一些
if (count % 150 == 0) {
GameUtils.littleBoss2BulletList.add(new LittleBoss2Bullet(GameUtils.littleBoss2BulletImg, 21, 59, bossObj.getX() + 220, bossObj.getY() + 130, 1, this));
GameUtils.gameObjList.add(GameUtils.littleBoss2BulletList.get(GameUtils.littleBoss2BulletList.size() - 1));
}
//boss子弹
GameUtils.bossBulletList.add(new BossBullet(GameUtils.bossBulletImg, 51, 72, bossObj.getX() + 70, bossObj.getY() + 100, 8, this));
GameUtils.gameObjList.add(GameUtils.bossBulletList.get(GameUtils.bossBulletList.size() - 1));
}
}
//警示出现的时间
if (count == 750 && (!GameUtils.gameObjList.contains(waringObj))) {
GameUtils.gameObjList.add(waringObj);
}
//警示消失的时间
if (count == 780) {
GameUtils.removeList.add(waringObj);
}
}
static Clip clip;
public static void playMusic() {
try
{
//这里面放 绝对路径,音频必须是wav格式,用音频转换软件 把mp3 转成wav格式
File musicPath = new File("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\PlayMusic\\bgm.wav");
if(musicPath.exists())
{
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
clip = AudioSystem.getClip();
clip.open(audioInput);
FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-20.0f);//设置音量,范围为 -60.0f 到 6.0f
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else
{
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
二,工具类
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
//游戏元素的父类
public class GameUtils {
//获取背景图片
public static Image bgImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\bg.jpg");
//获取boss图片
public static Image bossImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\boss.png");
//获取爆炸图片
public static Image explodeImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\explode\\e6.gif");
//获取我方飞机图片
public static Image planeImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\plane.png");
//获取我方飞机子弹的图片
public static Image shellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\shell.png");
//获取大飞机子弹的图片
public static Image enemy2BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy2bullet.png");
//获取敌方小飞机的图片
public static Image enemy1Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy1.png");
//获取敌方大飞机的图片
public static Image enemy2Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\enemy2.png");
//获取敌方boss1的图片
public static Image littleBoss1Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss1.png");
//获取敌方boss2的图片
public static Image littleBoss2Img= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss2.png");
//获取敌方1号boss子弹的图片
public static Image littleBoss1BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss1bullet.png");
//获取敌方2号boss子弹的图片
public static Image littleBoss2BulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\littleboss2bullet.png");
//获取补给的图片
public static Image giftImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\gift.png");
//获取二级子弹的图片
public static Image doubleShellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\doubleshell.png");
//获取三级子弹的图片
public static Image tripleShellImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\tripleshell.png");
//获取boss子弹的图片
public static Image bossBulletImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\bossbullet.png");
//获取警示标志的图片
public static Image warningImg= Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\warning.gif");
//创建我方飞机子弹的集合
public static List<ShellObj> shellObjList=new ArrayList<>();
//创建敌方小飞机的集合
public static List<Enemy1Obj> enemy1ObjList=new ArrayList<>();
//创建敌方大飞机的集合
public static List<Enemy2Obj> enemy2ObjList=new ArrayList<>();
//创建大飞机子弹的集合
public static List<Enemy2BulletObj> enemy2BulletObjList=new ArrayList<>();
//所有元素的集合
public static List<GameObj> gameObjList=new ArrayList<>();
//爆炸集合
public static List<ExplodeObj> explodeObjList=new ArrayList<>();
//移除窗口元素的集合
public static List<GameObj> removeList=new ArrayList<>();
//1号boss子弹的集合
public static List<LittleBoss1Bullet> littleBoss1BulletList=new ArrayList<>();
//2号boss子弹的集合
public static List<LittleBoss2Bullet> littleBoss2BulletList=new ArrayList<>();
//补给的集合
public static List<GiftObj> giftObjList=new ArrayList<>();
//二级子弹的集合
public static List<DoubleShellObj> doubleShellObjList=new ArrayList<>();
//三级子弹的集合
public static List<TripleShellObj> tripleShellObjList=new ArrayList<>();
//boss子弹的集合
public static List<BossBullet> bossBulletList=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);
}
}
三, 所有游戏元素的父类
import java.awt.*;
//所有元素的父类
public class GameObj {
//元素的图片
Image img;
//游戏元素的大小
int width;
int height;
//游戏元素的位置
int x;
int y;
//元素的运动速度
double speed;
//窗口类
GameWin frame;
//set和get方法
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
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 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 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 width, int height, int x, int y, double speed, GameWin frame) {
this.img = img;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.speed = speed;
this.frame = frame;
}
//绘制元素自身的方法
public void paintSelf(Graphics g){
g.drawImage(img,x,y,null);
}
//获取自身矩形的方法,用来进行碰撞检测
public Rectangle getRec(){
return new Rectangle(x,y,width,height);
}
}
四,背景类
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 BgObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//背景图向下移动
y+=speed;
if(y>=0){
y=-1800;
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
五,Boss子弹类
import java.awt.*;
public class BossBullet extends GameObj{
public BossBullet() {
super();
}
public BossBullet(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public BossBullet(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public BossBullet(int x, int y) {
super(x, y);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y+=speed;
//越界判断
if(this.y>800){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
六, BossObj类
import java.awt.*;
public class BossObj extends GameObj{
int health=200;
public BossObj() {
super();
}
public BossObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public BossObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//与littleBoss2运动轨迹一样,先从上往下走,再左右移动
if(y<40){
y+=speed;
}else {
x+=speed;
if(x<0||x>360){
speed=-speed;
}
}
//我方一级子弹和boss进行碰撞检测
for(ShellObj shellObj: GameUtils.shellObjList){
if(this.getRec().intersects(shellObj.getRec())&&health>0){
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
health--;
} else if (this.getRec().intersects(shellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.state=4;
GameWin.score+=10;
}
}
//我方二级子弹和boss进行碰撞检测
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())&&health>0){
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
health-=3;
} else if (this.getRec().intersects(doubleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.state=4;
GameWin.score+=10;
}
}
//我方三级子弹和boss进行碰撞检测
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())&&health>0){
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
health-=5;
} else if (this.getRec().intersects(tripleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.state=4;
GameWin.score+=10;
}
}
//白色矩形
g.setColor(Color.WHITE);
g.fillRect(200,40,200,10);
//红色矩形
g.setColor(Color.RED);
g.fillRect(200,40,health*200/30,10);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
七, 我方飞机二级子弹类
import java.awt.*;
public class DoubleShellObj extends GameObj{
public DoubleShellObj() {
super();
}
public DoubleShellObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public DoubleShellObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public DoubleShellObj(int x, int y) {
super(x, y);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y-=speed;
//越界处理
if(y<0){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
八,敌机1类
import java.awt.*;
public class Enemy1Obj extends GameObj{
public Enemy1Obj() {
super();
}
public Enemy1Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public Enemy1Obj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y+=speed;
// 一级子弹和敌机碰撞检测
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);
shellObj.setX(-100);
shellObj.setY(-100);
this.setX(-100);
this.setY(-100);
GameUtils.removeList.add(shellObj);
GameUtils.removeList.add(this);
GameWin.score+=1;
}
}
//二级子弹和敌方小飞机的碰撞
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
this.setX(-200);
this.setY(-200);
GameUtils.removeList.add(doubleshellObj);
GameUtils.removeList.add(this);
GameWin.score+=1;
}
}
//三级子弹和敌方小飞机的碰撞
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
this.setX(-200);
this.setY(-200);
GameUtils.removeList.add(tripleshellObj);
GameUtils.removeList.add(this);
GameWin.score+=1;
}
}
//越界判断
if(this.y>800){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
九,敌机2子弹类
import java.awt.*;
public class Enemy2BulletObj extends GameObj{
public Enemy2BulletObj() {
super();
}
public Enemy2BulletObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public Enemy2BulletObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y+=speed;
//越界判断
if(this.y>800){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十, 敌机2类
import java.awt.*;
public class Enemy2Obj extends GameObj{
int health=10;
public Enemy2Obj() {
super();
}
public Enemy2Obj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public Enemy2Obj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y+=speed;
//一级子弹和敌方大飞机碰撞的代码
for (ShellObj shellObj: GameUtils.shellObjList) {
if(this.getRec().intersects(shellObj.getRec())&&health>0){
//子弹消失
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
health--;
}else if(this.getRec().intersects(shellObj.getRec())&&health<=0){
//碰撞之后出现爆炸动画
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
//爆炸播放完后需要消失
GameUtils.removeList.add(explodeObj);
//子弹消失,敌方大飞机也消失
shellObj.setX(-100);
shellObj.setY(-100);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(shellObj);
GameUtils.removeList.add(this);
GameWin.score+=2;
}
}
//二级子弹和敌方大飞机的碰撞检测
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())&&health>0){
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
health-=3;
} else if (this.getRec().intersects(doubleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
this.x=-100;
this.y=-100;
GameUtils.removeList.add(this);
GameWin.score+=2;
}
}
//三级子弹和敌方大飞机的碰撞检测
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())&&health>0){
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
health-=5;
} else if (this.getRec().intersects(tripleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
this.x=-100;
this.y=-100;
GameUtils.removeList.add(this);
GameWin.score+=2;
}
}
//越界判断
if(this.y>800){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十一,爆炸类
import java.awt.*;
public class ExplodeObj extends GameObj{
//定义一个image类型的静态数组,存放一串爆炸图
static Image[] explodePic=new Image[16];
//定义变量来记录爆炸图的次数
int explodeCount=0;
//定义一个静态代码块来将爆炸图片放到数组当中
static{
for (int i = 0; i < explodePic.length; i++) {
explodePic[i]=Toolkit.getDefaultToolkit().getImage("C:\\Users\\23839\\IdeaProjects\\PlaneWar\\imgs\\explode\\e"+(i+1)+".gif");
}
}
@Override
public void paintSelf(Graphics g) {
if(explodeCount<16){
super.img=explodePic[explodeCount];
super.paintSelf(g);
explodeCount++;
}
}
public ExplodeObj(int x, int y) {
super(x, y);
}
}
十二, 道具类
import java.awt.*;
public class GiftObj extends GameObj{
public GiftObj() {
super();
}
public GiftObj(int x, int y) {
super(x, y);
}
public GiftObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public GiftObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.img= GameUtils.giftImg;
super.width=64;
super.height=62;
super.paintSelf(g);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十三,LittleBoss1类
import java.awt.*;
public class LittleBoss1 extends GameObj{
int health=10;
@Override
public Image getImg() {
return super.getImg();
}
public LittleBoss1() {
super();
}
public LittleBoss1(int x, int y) {
super(x, y);
}
public LittleBoss1(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public LittleBoss1(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//从窗口外面不断地加载到游戏窗口中
x+=speed;
//如果碰到窗口,在慢慢原路返回
if(x>400){
speed=-1;
}
//我方一级子弹和敌方1号boss碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
//遍历我方飞机子弹
for(ShellObj shellObj: GameUtils.shellObjList){
if(this.getRec().intersects(shellObj.getRec())&&health>0){
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
health--;
} else if (this.getRec().intersects(shellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
//敌方1号boss和我方二级子弹碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())&&health>0){
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
health-=3;
} else if (this.getRec().intersects(doubleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
//敌方1号boss和我方三级子弹碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())&&health>0){
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
health-=5;
} else if (this.getRec().intersects(tripleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十四, LittleBoss1子弹类
import java.awt.*;
public class LittleBoss1Bullet extends GameObj{
public LittleBoss1Bullet() {
super();
}
public LittleBoss1Bullet(int x, int y) {
super(x, y);
}
public LittleBoss1Bullet(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public LittleBoss1Bullet(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y+=speed;
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十五,LittleBoss2类
import java.awt.*;
public class LittleBoss2 extends GameObj{
int health=10;
public LittleBoss2() {
super();
}
public LittleBoss2(int x, int y) {
super(x, y);
}
public LittleBoss2(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public LittleBoss2(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//boss2生成在窗口的上面,向下移动一定位置,然后左右移动
if(y<150){
y+=2;
}else{
x+=speed;
if(x>400||x<10){
speed=-speed;
}
}
//敌方2号boss和我方一级子弹碰撞之后,我方子弹消失,当2号boss血量为0的时候,2号boss也会消失,否则不会消失
for(ShellObj shellObj: GameUtils.shellObjList){
if(this.getRec().intersects(shellObj.getRec())&&health>0){
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
health--;
} else if (this.getRec().intersects(shellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
//创建补给品的对象,同时传入位置,位置为当前敌方小boss坠毁时候的最后一个位置
//将补给对象添加到补给集合当中,再在加入到所有元素集合当中绘制
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
shellObj.setX(-100);
shellObj.setY(-100);
GameUtils.removeList.add(shellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
//敌方2号boss和我方二级子弹碰撞之后,我方子弹消失,当2号boss血量为0的时候,1号boss也会消失,否则不会消失
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())&&health>0){
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
health-=3;
} else if (this.getRec().intersects(doubleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
doubleshellObj.setX(-100);
doubleshellObj.setY(-100);
GameUtils.removeList.add(doubleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
//敌方2号boss和我方三级子弹碰撞之后,我方子弹消失,当2号boss血量为0的时候,1号boss也会消失,否则不会消失
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())&&health>0){
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
health-=5;
} else if (this.getRec().intersects(tripleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
//当敌方小boss被击毁的时候才会出现补给品
GiftObj giftObj=new GiftObj(this.x,this.y);
GameUtils.giftObjList.add(giftObj);
GameUtils.gameObjList.addAll(GameUtils.giftObjList);
tripleshellObj.setX(-100);
tripleshellObj.setY(-100);
GameUtils.removeList.add(tripleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=5;
}
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十六, LittleBoss2子弹类
import java.awt.*;
public class LittleBoss2Bullet extends GameObj{
int health=2;
public LittleBoss2Bullet() {
super();
}
public LittleBoss2Bullet(int x, int y) {
super(x, y);
}
public LittleBoss2Bullet(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public LittleBoss2Bullet(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//实现追踪功能,子弹一直往下走,追踪只是x一直随着飞机移动
this.y+=speed;
//当敌方子弹的x坐标减去我方飞机的坐标,当这个值为负值时,说明子弹在飞机的左边,这个事时候子弹的x坐标应该变大,往飞机的反向靠拢。反之,同理
//这里的/30减小差值,,因为单纯的,差值比较大的话,子弹显得非常不自然
this.x = this.x - ((this.x - GameUtils.gameObjList.get(GameWin.planeIndex).getX())/30);
//敌方2号boss子弹和我方子弹碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
for(ShellObj shellObj: GameUtils.shellObjList){
if(this.getRec().intersects(shellObj.getRec())&&health>0){
shellObj.setX(-100);
shellObj.setX(-100);
GameUtils.removeList.add(shellObj);
health--;
} else if (this.getRec().intersects(shellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
shellObj.setX(-100);
shellObj.setX(-100);
GameUtils.removeList.add(shellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=3;
}
}
//敌方2号boss子弹和我方二级子弹碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
for(DoubleShellObj doubleshellObj: GameUtils.doubleShellObjList){
if(this.getRec().intersects(doubleshellObj.getRec())&&health>0){
doubleshellObj.setX(-100);
doubleshellObj.setX(-100);
GameUtils.removeList.add(doubleshellObj);
health-=3;
} else if (this.getRec().intersects(doubleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
doubleshellObj.setX(-100);
doubleshellObj.setX(-100);
GameUtils.removeList.add(doubleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=3;
}
}
//敌方2号boss子弹和我方三级子弹碰撞之后,我方子弹消失,当1号boss血量为0的时候,1号boss也会消失,否则不会消失
for(TripleShellObj tripleshellObj: GameUtils.tripleShellObjList){
if(this.getRec().intersects(tripleshellObj.getRec())&&health>0){
tripleshellObj.setX(-100);
tripleshellObj.setX(-100);
GameUtils.removeList.add(tripleshellObj);
health-=5;
} else if (this.getRec().intersects(tripleshellObj.getRec())&&health<=0) {
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
tripleshellObj.setX(-100);
tripleshellObj.setX(-100);
GameUtils.removeList.add(tripleshellObj);
this.x=-200;
this.y=-200;
GameUtils.removeList.add(this);
GameWin.score+=3;
}
}
//越界判断
if(this.y>800){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十七,我方飞机类
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PlaneObj extends GameObj{
//记录我方飞机碰撞补给的次数
public static int times=0;
LittleBoss1 littleBoss1=new LittleBoss1();
LittleBoss2 littleBoss2=new LittleBoss2();
public PlaneObj() {
super();
}
public PlaneObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
//添加鼠标的移动事件
this.frame.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
PlaneObj.super.x=e.getX()-19;
PlaneObj.super.y=e.getY()-20;
}
});
}
public PlaneObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//进行碰撞检测
//我方飞机和敌方小飞机碰撞之后,敌方小飞机消失,我方飞机也消失
for (Enemy1Obj enemy1Obj: GameUtils.enemy1ObjList) {
if(this.getRec().intersects(enemy1Obj.getRec())){
//碰撞之后出现爆炸动画
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
//爆炸播放完后需要消失
GameUtils.removeList.add(explodeObj);
//并不是删除元素,而是通过移动坐标让它不在窗口中出现
//移动敌方小飞机
enemy1Obj.setX(-100);
enemy1Obj.setY(-100);
//移动我方小飞机
// this.x=-200;
// this.y=-200;
//将这些移除窗口的汇总成removeList
GameUtils.removeList.add(enemy1Obj);
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
//我方飞机和敌方大飞机碰撞之后,两者都消失
for (Enemy2Obj enemy2Obj: GameUtils.enemy2ObjList) {
if(this.getRec().intersects(enemy2Obj.getRec())){
//碰撞之后出现爆炸动画
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
//爆炸播放完后需要消失
GameUtils.removeList.add(explodeObj);
enemy2Obj.setX(-100);
enemy2Obj.setY(-100);
// this.x=-200;
// this.y=-200;
GameUtils.removeList.add(enemy2Obj);
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
//我方飞机和敌方大飞机子弹碰撞之后,两者都消失
for (Enemy2BulletObj enemy2BulletObj: GameUtils.enemy2BulletObjList) {
if(this.getRec().intersects(enemy2BulletObj.getRec())){
//碰撞之后出现爆炸动画
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
//爆炸播放完后需要消失
GameUtils.removeList.add(explodeObj);
enemy2BulletObj.setX(-100);
enemy2BulletObj.setY(-100);
// this.x=-200;
// this.y=-200;
GameUtils.removeList.add(enemy2BulletObj);
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
//当我方飞机和敌方boss1碰撞的时候,我方飞机消失,敌方飞机不消失
if(this.getRec().intersects(littleBoss1.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
// this.x=-200;
// this.y=-200;
// GameUtils.removeList.add(this);
GameWin.state=3;
}
//当我方飞机和敌方boss2碰撞的时候,我方飞机消失,敌方飞机不消失
if(this.getRec().intersects(littleBoss2.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
// this.x=-200;
// this.y=-200;
// GameUtils.removeList.add(this);
GameWin.state=3;
}
//当我方飞机和敌方1号boss子弹碰撞之后,两者都消失
for (LittleBoss1Bullet littleBoss1Bullet:GameUtils.littleBoss1BulletList) {
if(this.getRec().intersects(littleBoss1Bullet.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
littleBoss1Bullet.setX(-100);
littleBoss1Bullet.setY(-100);
GameUtils.removeList.add(littleBoss1Bullet);
// this.x=-200;
// this.y=-200;
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
//当我方飞机和敌方2号boss子弹碰撞之后,两者都消失
for (LittleBoss2Bullet littleBoss2Bullet:GameUtils.littleBoss2BulletList) {
if(this.getRec().intersects(littleBoss2Bullet.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
littleBoss2Bullet.setX(-100);
littleBoss2Bullet.setY(-100);
GameUtils.removeList.add(littleBoss2Bullet);
// this.x=-200;
// this.y=-200;
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
//我方飞机和补给碰撞后,我方飞机不消失,补给消失
for (GiftObj giftObj: GameUtils.giftObjList) {
if(this.getRec().intersects(giftObj.getRec())){
giftObj.setX(-100);
giftObj.setY(-100);
GameUtils.removeList.add(giftObj);
times++;
}
}
//我方飞机碰撞boss子弹后,两者都消失
for(BossBullet bossBullet:GameUtils.bossBulletList){
if(this.getRec().intersects(bossBullet.getRec())){
//绘制爆炸
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeList.add(explodeObj);
bossBullet.setX(-100);
bossBullet.setY(-100);
// this.x=-200;
// this.y=-200;
GameUtils.removeList.add(bossBullet);
// GameUtils.removeList.add(this);
GameWin.state=3;
}
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十八,我方飞机子弹类
import java.awt.*;
public class ShellObj extends GameObj{
public ShellObj() {
super();
}
public ShellObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public ShellObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
//实现子弹的移动,改变子弹的纵坐标
y -= speed;
//越界处理
if(y<0){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
十九,我方飞机三级子弹类
import java.awt.*;
public class TripleShellObj extends GameObj{
public TripleShellObj() {
super();
}
public TripleShellObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public TripleShellObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public TripleShellObj(int x, int y) {
super(x, y);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
y-=speed;
//越界处理
if(y<0){
GameUtils.removeList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
二十, BOSS警示类
import java.awt.*;
public class WarningObj extends GameObj{
public WarningObj() {
super();
}
public WarningObj(Image img, int width, int height, int x, int y, double speed, GameWin frame) {
super(img, width, height, x, y, speed, frame);
}
public WarningObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public WarningObj(int x, int y) {
super(x, y);
}
@Override
public void paintSelf(Graphics g) {
super.paintSelf(g);
}
}
游戏效果显示
关于游戏里用到的所有的图片资源我都放在下面了,我做飞机大战时的把部分数据改了一点,但是大部分还是和尚学堂飞机大战3.0一样。
关于背景音乐大家选择下载wav格式,运行代码通常不会报错