1.设置背景音乐
package util;
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.io.IOException;
public class MusicUtil {
private static AudioClip start;
private static AudioClip bomb;
//装载音乐资源
static {
try {
//引号里面的是音乐文件所在的绝对路径
start = Applet.newAudioClip(new File("music/start.wav").toURL());//加载音频
//bomb = Applet.newAudioClip(new File("music/bomb.wav").toURL());//加载音频
} catch (IOException e) {
e.printStackTrace();
}
}
public static void playStart() {
start.play(); //播放音频
}
public static void playBomb() {
bomb.play(); //播放音频
}
}
2.游戏配置
在map包创建GameInfo:
package map;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 游戏相关的信息的类
*/
public class GameInfo {
//从文件中读取
//关卡数量
private static int levelCount;
static {
Properties prop =new Properties();
try {
prop.load(new FileInputStream("level/gameinfo"));
levelCount = Integer.parseInt(prop.getProperty("levelCount"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getLevelCount() {
return levelCount;
}
}
3.通关判断
/**
* 是否是最后一关
* @return
*/
public static boolean isLastLevel(){
//当前关卡和总关卡一致
int currLevel = LevelInof.getInstance().getLevel();
int levelCount = GameInfo.getLevelCount();
return currLevel==levelCount;
}
/**
* 判断是否过关了
* @return
*/
public static boolean isCrossLevel(){
//消灭敌人的数量 和关卡的敌人的数量一致
return killEnemyCount == LevelInof.getInstance().getEnemyCount();
}
判断
//坦克死亡后处理的内容
private void die(){
if(isEnemy){
GameFrame.killEnemyCount ++;
//敌人坦克被消灭了,归还对象池
TankPool.theReturn((EnemyTank) this);
//本关是否结束
if (GameFrame.isCrossLevel()){
//判断游戏是否通关?
if(GameFrame.isLastLevel()){
//是
GameFrame.setGameState(Constant.STATE_WIN);
}else {
//进入下一关卡
GameFrame.startCrossLevel();
}
}
}else {
// 切换游戏状态
delaySecondsToOver(1000);
}
}