第四次项目《字符打怪兽》

2024-02-26 10:38
文章标签 项目 字符 第四次 怪兽

本文主要是介绍第四次项目《字符打怪兽》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*** * 游戏* @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;}}
}

这篇关于第四次项目《字符打怪兽》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/748581

相关文章

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内

配置springboot项目动静分离打包分离lib方式

《配置springboot项目动静分离打包分离lib方式》本文介绍了如何将SpringBoot工程中的静态资源和配置文件分离出来,以减少jar包大小,方便修改配置文件,通过在jar包同级目录创建co... 目录前言1、分离配置文件原理2、pom文件配置3、使用package命令打包4、总结前言默认情况下,

python实现简易SSL的项目实践

《python实现简易SSL的项目实践》本文主要介绍了python实现简易SSL的项目实践,包括CA.py、server.py和client.py三个模块,文中通过示例代码介绍的非常详细,对大家的学习... 目录运行环境运行前准备程序实现与流程说明运行截图代码CA.pyclient.pyserver.py参