使用JAVA基础知识做的一个增强版飞机大战,仙侠主题,增设关卡BOSS等
第一次在CSDN写博客 不足之处请大家多多指教
先放一下效果图
以下是部分代码介绍
首先定义播放音乐和图片的工具类,都是使用静态代码块来加载资源
package Game;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/** 处理图片的工具类*/
public class Images {
public static BufferedImage[] robot0;//支援机器人出现前
public static BufferedImage[] robot;//支援机器人
public static BufferedImage[] superaward;//超级奖励
public static BufferedImage[] superaward2;
public static BufferedImage paihang;//排行榜
public static BufferedImage tongguan;//通关
public static BufferedImage[] award;
public static BufferedImage[] awardtype;
public static BufferedImage[] protect;
public static BufferedImage[] bskill;//BOSS技能图
public static BufferedImage[] bskill2;//BOSS技能图
public static BufferedImage[] bskill3;//BOSS技能图
public static BufferedImage skill;//技能图
public static BufferedImage[] bosstest5;
public static BufferedImage[] bosstest4;
public static BufferedImage[] bosstest3;
public static BufferedImage[] bosstest2;
public static BufferedImage[] bosstest2f;
public static BufferedImage[] bairplanetest5;//大敌机五号
public static BufferedImage[] bairplanetest4;//大敌机四号
public static BufferedImage[] bairplanetest3;//大敌机三号
public static BufferedImage[] bairplanetest2;//大敌机二号
public static BufferedImage[] bairplanetest;//大敌机一号
public static BufferedImage[] airplanetest;//小敌机一号
public static BufferedImage[] airplanetest2;//小敌机二号
public static BufferedImage[] airplanetest3;//小敌机三号
public static BufferedImage[] airplanetest4;//小敌机4号
public static BufferedImage[] airplanetest5;//小敌机5号
public static BufferedImage[] beetest;//蜜蜂一号
public static BufferedImage[] beetest2;//蜜蜂二号
public static BufferedImage[] beetest3;//蜜蜂三号
public static BufferedImage[] beetest4;//蜜蜂三号
public static BufferedImage[] test;//BOSS机一号
public static BufferedImage[] test2;
public static BufferedImage[] ebullet;//敌方子弹图片
public static BufferedImage helphero; //僚机图片
public static BufferedImage sky[]; //天空图片数组
public static BufferedImage bullet[]; //子弹图片
public static BufferedImage[] heros;
public static BufferedImage airs[]; //敌机图片
public static BufferedImage[] boom;//爆炸数组
public static BufferedImage bairs[];
public static BufferedImage bees[];
//public static BufferedImage[] boss;//boss机数组
public static BufferedImage start;//开始状态的图片
//public static BufferedImage pause;//开始状态的图片
public static BufferedImage gameover;//开始状态的图片
/**静态代码块中进行初始赋值*/
static{
bosstest5 = new BufferedImage[41];
bosstest4 = new BufferedImage[33];
bosstest3 = new BufferedImage[16];
bosstest2 = new BufferedImage[9];
bosstest2f = new BufferedImage[9];
bairplanetest = new BufferedImage[4];
bairplanetest2 = new BufferedImage[6];
bairplanetest3 = new BufferedImage[8];
bairplanetest4 = new BufferedImage[6];
bairplanetest5 = new BufferedImage[14];
airplanetest = new BufferedImage[6];
airplanetest3 = new BufferedImage[6];
airplanetest4 = new BufferedImage[7];
airplanetest5 = new BufferedImage[8];
bullet = new BufferedImage[2];
robot = new BufferedImage[4];
robot0 = new BufferedImage[7];
bskill = new BufferedImage[5];
bskill2 = new BufferedImage[8];
bskill3 = new BufferedImage[8];
protect = new BufferedImage[11];
airplanetest2 = new BufferedImage[13];
beetest = new BufferedImage[6];
beetest2 = new BufferedImage[6];
beetest3 = new BufferedImage[6];
beetest4 = new BufferedImage[6];
test = new BufferedImage[6];
test2 = new BufferedImage[6];
bairs = new BufferedImage[5];
bees = new BufferedImage[5];
airs = new BufferedImage[5];
ebullet = new BufferedImage[2];
skill = readImage("fire.png");
paihang = readImage("排行榜.jpg");
tongguan = readImage("通关.png");
ebullet[0] = readImage("月光.png");
ebullet[1] = readImage("600.png");
superaward = new BufferedImage[10];
superaward2 = new BufferedImage[10];
award = new BufferedImage[17];
awardtype = new BufferedImage[2];
awardtype[0] = readImage("生命.png");
awardtype[1] = readImage("子弹奖励.jpg");
// boss = new BufferedImage[5];
bullet[0] = readImage("bullet.png");
bullet[1] = readImage("bullet2.png");
helphero = readImage("hero.png");
boom = new BufferedImage[6];
heros = new BufferedImage[12];
sky = new BufferedImage[5];
// boss[0] = readImage("602.png");
// boss[1] = readImage("ep13.png");
// boss[2] = readImage("ep15.png");
// boss[3] = readImage("601.png");
// boss[4] = readImage("ep14.png");
// airs[0] = readImage("airplane.png");
// airs[1] = readImage("ep01.png");
// airs[2] = readImage("ep02.png");
// airs[3] = readImage("ep03.png");
// airs[4] = readImage("ep05.png");
// bairs[0] = readImage("bigairplane.png");
// bairs[1] = readImage("ep10.png");
// bairs[2] = readImage("ep11.png");
// bairs[3] = readImage("ep12.png");
// bairs[4] = readImage("ep12.png");
// bees[0] = readImage("bee.png");
// bees[1] = readImage("ep06.png");
// bees[2] = readImage("ep07.png");
// bees[3] = readImage("ep08.png");
// bees[4] = readImage("ep09.png");
start = readImage("start.jpg");
// pause = readImage("pause.png");
gameover = readImage("失败.png");
/** 爆炸效果图片数组 */
for(int i=0;i<boom.length;i++){
boom[i] = readImage("boom ("+(i+1)+").png");
}
for (int i = 1; i < sky.length+1; i++) {
sky[i-1] = readImage("bg"+i+".jpg");
}
/**
* 加载英雄机图片
*/
for (int i = 0; i < heros.length; i++) {
heros[i] = readImage("hero ("+(i+1)+").png");
}
for (int i = 0; i < protect.length; i++) {
protect[i] = readImage("护盾 ("+(i+1)+").png");
}
/**
* 加载正向超级奖励
*/
for (int i = 0; i < superaward.length; i++) {
superaward[i] = readImage("super ("+(i+1)+").png");
superaward2[i] = readImage("super2 ("+(i+1)+").png");
}
for (int i = 0; i < robot.length; i++) {
robot[i] = readImage("机器人 ("+(i+1)+").png");
}
/**
* 加载超级奖励2号
*/
for (int i = 0; i < award.length; i++) {
award[i] = readImage("bee2 ("+(i+1)+").png");
}
/**
* 加载大小一号敌机
*/
for (int i = 0; i < beetest.length; i++) {
beetest[i] = readImage("bee ("+(i+1)+").png");
beetest2[i] = readImage("bee3 ("+(i+1)+").png");
beetest3[i] = readImage("bee4 ("+(i+1)+").png");
beetest4[i] = readImage("bee5 ("+(i+1)+").png");
airplanetest[i] = readImage("e1 ("+(i+1)+").png");
airplanetest3[i] = readImage("e3 ("+(i+1)+").png");
bairplanetest2[i] = readImage("En2 ("+(i+1)+").png");
bairplanetest4[i] = readImage("En4 ("+(i+1)+").png");
}
/**
* 加载小敌机4号
*/
for (int i = 0; i < airplanetest4.length; i++) {
airplanetest4[i] = readImage("e4 ("+(i+1)+").png");
}
for (int i = 0; i < bskill.length; i++) {
bskill[i] = readImage("sk"+i+".png");
}
for (int i = 0; i < bairplanetest.length; i++) {
bairplanetest[i] = readImage("En1 ("+(i+1)+").png");
}
/**
* 加载大敌机五号
*/
for (int i = 0; i < bairplanetest5.length; i++) {
bairplanetest5[i] = readImage("En5 ("+(i+1)+").png");
}
/**
* 加载小敌机二号
*/
for (int i = 0; i < airplanetest2.length; i++) {
airplanetest2[i] = readImage("e2 ("+(i+1)+").png");
}
/**
* 加载大敌机三号
*/
for (int i = 0; i < bairplanetest3.length; i++) {
bairplanetest3[i] = readImage("En3 ("+(i+1)+").png");
airplanetest5[i] = readImage("e5 ("+(i+1)+").png");
}
/**
* 加载BOSS1
*/
for (int i = 0; i < test.length; i++) {
test[i] = readImage("test正"+i+".png");
test2[i] = readImage("test反"+i+".png");
}
/**
* 加载BOSS2
*/
for (int i = 0; i < bosstest2.length; i++) {
bosstest2[i] = readImage("boss2 ("+(i+1)+").png");
bosstest2f[i] = readImage("boss2反 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest3.length; i++) {
bosstest3[i] = readImage("boss3 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest5.length; i++) {
bosstest5[i] = readImage("boss5 ("+(i+1)+").png");
}
for (int i = 0; i < bosstest4.length; i++) {
bosstest4[i] = readImage("boss4 ("+(i+1)+").png");
}
/**
* 加载机器人
*/
for (int i = 0; i < robot0.length; i++) {
robot0[i] = readImage("机器出现 ("+(i+1)+").png");
}
/**
* 加载BOSS精灵王技能
*/
for (int i = 0; i < bskill2.length; i++) {
bskill2[i] = readImage("闪电 ("+(i+1)+").png");
bskill3[i] = readImage("飓风 ("+(i+1)+").png");
}
}
/**读取图片的方法*/
public static BufferedImage readImage(String fileName){
try{
BufferedImage img = ImageIO.read(Flys.class.getResource