/**
*
* 游戏
* @author 周太阳
* @version 1.0
*/
public class Game {
// 玩家
private static Player player;
// 初始化玩家变量
public Game(Player player) {
super();
// 将Player的属性传给Game,可以使Game操作同一个对象
this.player = player;
}
public Game() {
super();
// TODO Auto-generated constructor stub
}
/**输出字符串*/
public String printStr() {
// 获取怪物信息
monsterPrint();
StringBuilder sb = new StringBuilder();
// 数字强转成字符
for (int i = 0; i < Test.level[player.getLevelNo()-1].getStrLength(); i++) {
char word = (char) (Math.random() * (127 - 33) + 33);
sb.append(word);
}
// StringBuilder转为String类型
return sb.toString();
}
/**比较游戏输出和玩家输入*/
public void printResult(String out, String in) {
if (in.equals(out)) {
// 获取当前时间毫秒数
long currentTime = System.currentTimeMillis();
// 计算时间
int time = (int) ((currentTime - player.getStartTime())/1000);
// 判断是否超时
if (time < Test.level[player.getLevelNo()-1].getTimeLimit()) {
// 获取怪物生命值
mosterHp();
// 计算当前耗时
player.setElapsedTime(time);
// 计算当前分数
score();
// 输出积分、时间信息
userPrints();
if (player.getLevelNo() == Test.level.length) {
System.out.println("恭喜您通关了!");
System.out.println("您的总分为:"+player.getCurrScore());
System.exit(0);
}
}else {
System.err.println("太迟了,怪物已经攻过来了,"+player.getName()+"被怪物蹂躏致死!\n『胜败乃兵家常事,少侠请重新来过~』");
System.exit(1);
}
}else {
System.err.println("少侠手滑了一下!"+player.getName()+"被怪物蹂躏致死!\n『胜败乃兵家常事,少侠请重新来过~』");
System.exit(1);
}
}
/**输出积分、时间信息*/
static void userPrints() {
System.out.println(player.getName()+"当前积分为:"+player.getCurrScore());
System.out.println(player.getName()+"用时:"+player.getElapsedTime()+"秒");
}
/**计算分数*/
private static void score() {
int s = Test.level[player.getLevelNo()-1].getPerScore()
* (Test.level[player.getLevelNo()-1].getTimeLimit()
-player.getElapsedTime());
player.setCurrScore(player.getCurrScore()+s);
}
/**输出怪物信息*/
static void monsterPrint() {
System.out.println("⊙怪物:"+ Test.level[player.getLevelNo() - 1].getMonsterName());
System.out.println("⊙怪物生命值:"+ Test.level[player.getLevelNo() - 1].getHealthPoints());
}
/**输出怪物生命值*/
static void mosterHp() {
// 获取当前怪物生命值
int hp = Test.level[player.getLevelNo() - 1].getHealthPoints();
// 获取攻击后怪物生命值
int currentHp = hp - player.getAttack();
// 赋值给怪物
Test.level[player.getLevelNo() - 1].setHealthPoints(currentHp);
System.out.println("※干得漂亮!命中怪物!当前怪物生命值为:"+ (hp-player.getAttack()));
}
}
/**
* 级别类
* @author 周太阳
* @version 1.0
*/
public class Level{
/**各级别编号*/
private int levelNo;
/**各级别一次输出字符串长度*/
private int strLength;
/**各级别输出字符串的次数*/
private int strTime;
/**各级别闯关的时间限制*/
private int timeLimit;
/**各级别输入正确得分*/
private int perScore;
/**怪物血量*/
private int healthPoints;
/**怪物姓名*/
private String monsterName;
public Level(int levelNo, int strLength, int strTime, int timeLimit,
int perScore, int healthPoints, String monsterName) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTime = strTime;
this.timeLimit = timeLimit;
this.perScore = perScore;
this.healthPoints = healthPoints;
this.monsterName = monsterName;
}
public int getHealthPoints() {
return healthPoints;
}
public void setHealthPoints(int healthPoints) {
this.healthPoints = healthPoints;
}
public String getMonsterName() {
return monsterName;
}
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
public Level() {
super();
// TODO Auto-generated constructor stub
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getStrLength() {
return strLength;
}
public void setStrLength(int strLength) {
this.strLength = strLength;
}
public int getStrTime() {
return strTime;
}
public void setStrTime(int strTime) {
this.strTime = strTime;
}
public int getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
}
/**各级别的参数信息*/
public class LevelParam {
/**普通难度*/
public final static Level[] normal = new Level[6];
/**困难难度*/
public final static Level[] hard = new Level[6];
static {
normal[0] = new Level(1, 1, 2, 10, 1, 100, "『大海龟』");
normal[1] = new Level(2, 1, 2, 10, 3, 500, "『骷髅怪』");
normal[2] = new Level(3, 2, 2, 8, 5, 1000, "『黑熊精』");
normal[3] = new Level(4, 2, 2, 8, 7, 3000, "『雷鸟人』");
normal[4] = new Level(5, 3, 2, 7, 9, 5000, "『天将』");
normal[5] = new Level(6, 4, 2, 6, 11, 10000, "『地狱战神』");
hard[0] = new Level(1, 2, 2, 10, 3, 100, "『大海龟』");
hard[1] = new Level(2, 4, 2, 9, 5, 500, "『骷髅怪』");
hard[2] = new Level(3, 4, 2, 8, 7, 1000, "『黑熊精』");
hard[3] = new Level(4, 6, 2, 7, 9, 3000, "『雷鸟人』");
hard[4] = new Level(5, 6, 2, 6, 11, 5000, "『天将』");
hard[5] = new Level(6, 8, 2, 6, 13, 10000, "『地狱战神』");
}
}
/**
* 玩家类
* @author 周太阳
* @version 1.0
* @date 2019年4月12日 下午9:29:40
*/
public class Player {
/**当前级别号*/
private int levelNo;
/**当前级别得分*/
private int currScore;
/**开始时间*/
private long startTime;
/**当前级别已用时间*/
private int elapsedTime;
/**玩家攻击力*/
private int attack;
/**玩家姓名*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getCurrScore() {
return currScore;
}
public void setCurrScore(int currScore) {
this.currScore = currScore;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
}
/**玩游戏*/
public void play() {
Game game = new Game(this);
Level lv = new Level();
System.out.println("游戏开始!");
// 循环输入次数
for (int i = 0; i < Test.level.length; i++) {
// 升级
levelNo++;
// 计时清零
startTime = 0;
// 玩家攻击力
attack = Test.level[i].getHealthPoints()
-Test.level[i].getHealthPoints()/Test.level[levelNo - 1].getStrTime();
System.out.println("★"+name+"当前级别为:"+levelNo+"级");
System.out.println("★"+name+"当前攻击力为:"+attack);
System.out.println("〉〉〉怪物来啦!!!(⊙_⊙)");
for (int j = 0; j < Test.level[i].getStrTime(); j++) {
// 随机输出字符
String out = game.printStr();
System.out.println("¤"+name+"抓紧时间!请快输入:【"+out+"】怪物将在"+Test.level[levelNo-1].getTimeLimit()+"秒后开始攻击!!!");
// 开始时间
startTime = System.currentTimeMillis();
Scanner input = new Scanner(System.in);
String in = input.next();
// 输出结果
game.printResult(out, in);
}
System.out.println("↑↑↑↑↑少侠升级啦!!!↑↑↑↑↑");
}
}
}
/**
* 玩家玩游戏
*/
public class Test {
/**选择难度数组*/
static Level[] level = new Level[6];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Player player = new Player();
System.out.println("————————————————————————————");
System.out.println("|\t\t☆欢迎来到字符打怪兽☆ \t\t|");
System.out.println("————————————————————————————");
System.out.print("请输入您的姓名:");
String name = input.next();
// 玩家姓名
player.setName(name);
do {
System.out.print("请选择难度(1、【正常】 2、【困难】):");
level = input.nextInt() == 1 ? LevelParam.normal : LevelParam.hard;
// 开始玩游戏
player.play();
// 是否继续
}while(isContinue());
System.out.println("===游戏结束。欢迎下次再来!===");
}
/**是否继续游戏*/
public static boolean isContinue() {
Scanner input = new Scanner(System.in);
System.out.println("是否继续(y/n)?");
String select = input.next();
if(select.equals("y")) {
return true;
}else {
return false;
}
}
}
第四次项目《字符打怪兽》
猜你喜欢
转载自blog.csdn.net/Super_Robot/article/details/89471560
今日推荐
周排行