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

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

相关文章

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地