JAVA打字小游戏,面向对象完成
测试类,测试类主要是用于引用,将我们进行引入
package com.ytzl.Project.work;public class Test {public static void main(String[] args) {Player player = new Player();player.play();}
}
实体类1,该类主要用于输出系统随机字符,接收玩家打印字符,并传入game类进行判断
package com.ytzl.Project.work;import java.util.Scanner;public class Player {private int levelNo;//当前级别号private int currScore;//当前级别积分private long startTime;//开始时间private long elapsedTime;//已用时间public Player() {}public Player(int levelNo, int currScore, long startTime, long elapsedTime) {this.levelNo = levelNo;this.currScore = currScore;this.startTime = startTime;this.elapsedTime = elapsedTime;}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 long getElapsedTime() {return elapsedTime;}public void setElapsedTime(long elapsedTime) {this.elapsedTime = elapsedTime;}//玩游戏public void play(){Game game = new Game(this);Scanner scanner = new Scanner(System.in);for (int i = 0; i < LevelParam.levels.length; i++) {//晋级this.levelNo+=1;//2.时间开始计时this.startTime = System.currentTimeMillis();//3.积分清零this.currScore = 0;//4.内层循环 循环的是电脑输出,玩家输入,比较for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {//4.1 电脑输出字符串String out = game.printStr();//4.2 接受用户输入String in = scanner.next();//4.3 比较,并判断结果game.printResult(out,in);}}}
}
实体类2,该类主要用于创造随机字符,根据玩家打印字符进行判断。
package com.ytzl.Project.work;import java.util.Random;public class Game {/** 构造方法传入玩家信息* */private Player player;public Game(Player player) {this.player=player;}String printStr(){//从创建好的player对象中获取级别号//因为我们需要将Game中和player中的//两个方法对应的级别统一int strLength = LevelParam.levels[player.getLevelNo() -1].getStrLength();//随机数Random random = new Random();//操作字符串StringBuffer buffer = new StringBuffer();//buffer.append() 字符串的连接for (int i = 0; i < LevelParam.levels[player.getLevelNo()-1].getStrLength(); i++) {//生成随机数int rand=random.nextInt(4);switch (rand){case 0:buffer.append(">");break;case 1:buffer.append("<");break;case 2:buffer.append("/");break;case 3:buffer.append("%");break;case 4:buffer.append("&");break;default:break;}}System.out.println(buffer.toString());return buffer.toString();}public void printResult(String out,String in){boolean flag;if (out.equals(in)){flag=true;}else {flag=false;}//第一层:是否输入正确if (flag){//第二层:是否超时//currentTime :输入正确之后拿到的时间long currentTime=System.currentTimeMillis();if (//currentTime : 160345678325//getStartTime : 160345678330(currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){System.out.println("你已超时,游戏结束!");System.exit(1);}else {//没有超时//计算当前积分//当前积分 + 各级别给的积分player.setCurrScore(player.getCurrScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());//当前所用时间,用每一次输入正确之后的//时间减去初始进入游戏时间player.setElapsedTime((currentTime-player.getStartTime())/1000);//输出语句System.out.println("输入正确,您的级别为:"+player.getLevelNo()+",您的积分为:"+player.getCurrScore()+",已用时间为:"+player.getElapsedTime()+"秒");//判断是否闯关成功if (player.getLevelNo() == 6){int scroe= LevelParam.levels[player.getLevelNo()-1].getStrTimes()*LevelParam.levels[player.getLevelNo()-1].getPerScore();System.out.println("闯关成功!");System.out.println("总积分为:"+scroe);System.exit(1);}}}else {System.out.println("输入错误!退出");System.exit(1);}}
}
实体类3,该类用于存储游戏等级,字符长度,字符次数,时间限制,正确得分
package com.ytzl.Project.work;public class Level {private int levelNo;//各级别号private int strLength;//各级输入字符长度private int strTimes;//各级数输入字符次数private int timeLimit;//各级闯关时间限制private int perScore;//各级输入正确得分public Level() {}public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {this.levelNo = levelNo;this.strLength = strLength;this.strTimes = strTimes;this.timeLimit = timeLimit;this.perScore = perScore;}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 getStrTimes() {return strTimes;}public void setStrTimes(int strTimes) {this.strTimes = strTimes;}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;}
}
实体类4,该类为数组类,用来设置level中的数据
package com.ytzl.Project.work;public class LevelParam {public final static com.ytzl.Project.work.Level levels[]=new com.ytzl.Project.work.Level[6];//对应六个级别static {levels[0] = new Level(1,2,10,30,1);levels[1] = new Level(2,3,26,26,2);levels[2] = new Level(3,4,33,33,4);levels[3] = new Level(4,5,18,18,6);levels[4] = new Level(5,6,14,14,8);levels[5] = new Level(6,7,13,12,10);}
}
JAVA打字小游戏,面向对象完成相关推荐
- java打字小游戏_java实现打字游戏小程序
本文实例为大家分享了java实现打字游戏小程序的具体代码,供大家参考,具体内容如下 一.设计思路 1.创建一个窗体 2.在窗体上放置一个面板,用paint方法画出英文字母,随机放置字母位置,并随时间自 ...
- Java猜拳小游戏(面向对象版)
主要功能: 1)选取对战角色 2)猜拳 3)记录分数 4)显示比分以及结果 完整代码: import java.util.Scanner;/** @author: wyh* @classname: G ...
- java打字小游戏源码_求JAVA编写打字游戏源代码!
展开全部 package chen; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; ...
- JAVA实现打字小游戏
目录 一.效果 二.教程 三.代码 一.效果 首先我们先看效果,左上角的分数是用来记录我们打对了多少字母.字母是从上面开始往下落.每打对一个字母,分数增加,增加到一定分数后,字母下落的速度也会增加.( ...
- java实验2总结心得,打字小游戏JAVA实验总结及心得体会
篇一:扫雷游戏实验报告 课程设计 班 级: 姓 名: 学 号: 指导教师: 成 绩: 电子与信息工程学院信息与通信工程系 目录 1.任务概述------------------------------ ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) 代码实现 窗口类 小车类 玩家类 电脑类 赛道类 小树类 打字类 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇) 资源下载 完整代码 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) Python 打字小游戏开发,来体验不一样的 ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) 前言 游戏素材准备 游戏项目结构 项目里面的类说明 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) Python ...
- 原生js实现的金山打字小游戏
首先先来看一下效果图 如果感兴趣的就来看一下Js源码吧 //计分板 var board = {dom: document.getElementById("score"),maxLo ...
最新文章
- 深度解析MegEngine亚线性显存优化技术
- 虚拟机网络设置方法——转载
- 京东 | AI人才联合培养计划
- python去除数组缺失值_动态数组的应用,VBA中如何利用动态数组去除重复值的第二讲...
- Python导入CSV短代码(pandas?),以';'分隔 和','全部
- linux 访问windows共享
- 【Python】Turtle绘制科赫雪花
- 如何在Windows上的Git Bash中退出'git diff'的结果? [重复]
- 网盘助手插件chrome插件
- 电动汽车蒙特卡洛分析matlab 通过matlab程序编写电动汽车蒙特卡洛模型
- 论文-公式对齐和右侧自动编号
- 微信淘宝客机器人分享
- 齐供应TAPPI四碘化5,10,15,20-四(对-N,N,N三甲基苯胺基)卟啉敏化的钛酸盐纳米管(TAPPI-TNTs)高效的可见光催化剂岳
- php redis 存入数组,php redis 多维数组操作
- 24.多线程(等待唤醒机制,volatile,CAS 算法,线程池,定时器,设计模式)
- java随堂练习09-面对对象,创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
- 上海喔趣科技有限公司面经 Java实习生(记录第一次面试)已oc
- OpenGL 摄像机
- 屏蔽Mac版迅雷更新提醒
- 生产制造|销售管理实时在线,业务效率直线提升