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

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

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

在.NET项目中嵌入Python代码的实践指南

《在.NET项目中嵌入Python代码的实践指南》在现代开发中,.NET与Python的协作需求日益增长,从机器学习模型集成到科学计算,从脚本自动化到数据分析,然而,传统的解决方案(如HTTPAPI或... 目录一、CSnakes vs python.NET:为何选择 CSnakes?二、环境准备:从 Py

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima