首先先明确下这个小游戏的需求
1.根据输入速率和正确率将玩家分为不同级别
2.级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
3.规定时间内完成规定次数的输入,正确率达到规定要求,则升级 (玩家最高级别为6级、初始级别一律为1级 )
5.用户错误输入一次,游戏结束
看到这些需求很多刚接触的小伙伴一定懵了吧,不知道该如何下手了。没事跟着我,慢慢来。
首先我们先创造几个类:玩家(Player)类
游戏(Game)类
级别(Level)类
级别类(Level)里的属性 :
各级别号(levelNo)
各级别一次输出字符串的长度(strLength)
各级别输出字符串的次数(strTimes)
各级别闯关的时间限制(timeLimit)
各级别正确输入一次的得分(perScore)
public class Level {
private int levelNo; // 级别号
private int strLength; // 各级别一次输出字符串的长度
private int strTimes; // 各级别输出字符串的次数
private int timeLimit; // 各级别闯关的时间限制
private int perScore; // 各级别成功输入一次字符串后增加的分值
public Level(int levelNo, int strLength, int strTimes, int timeLimit,
int perScore) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
}
public Level() {
super();
}
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;
}
}
再创个新类(LevelParam),初始化一下信息
public class LevelParam {
public static final Level[] levels = new Level[6];
static{
levels[0]= new Level(1, 2, 10, 30, 1);
levels[1]= new Level(2, 3, 9, 26, 2);
levels[2]= new Level(3, 4, 8, 22, 5);
levels[3]= new Level(4, 5, 7, 18, 8);
levels[4]= new Level(5, 6, 6, 15, 10);
levels[5]= new Level(6, 7, 5, 12, 15);
}
}
游戏类(Game)的方法
String printStr() 输出字符串,返回字符串用于和玩家输入比较
void printResult(String out,String in) 比较游戏输出out和玩家输入in,根据比较结果输出相应信息
import java.util.Random;
public class Game {
private Player player;
public Game(Player player) {
super();
this.player = player;
}
public Game() {
super();
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public String printStr() {
int strLength = LevelParam.levels[player.getLevelNo() - 1]
.getStrLength();
StringBuffer buffer = new StringBuffer();
Random random = new Random();
// 通过循环生成要输出的字符串
for (int i = 0; i < strLength; i++) {
int rand = random.nextInt(strLength); // 产生随机数
// 根据随机数拼接字符串
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;
case 5:
buffer.append("$");
break;
}
}
System.out.println(buffer);
return buffer.toString();
}
public void printResult(String out, String in) {
boolean flag;
if(out.equals(in)){
flag=true;
}else{
flag=false;
}
if(flag){
long currentTime = System.currentTimeMillis();
//超时
if((currentTime- player.getStartTime())/1000>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
System.out.println("你输入的太慢了,已经超时了,退出!");
System.exit(1);
}else{
//没有超时
//计算积分
player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
//计算已用时
player.setElapsedTime((int)((currentTime- player.getStartTime())/1000));
//输出
System.out.println("输入正确,您的级别:"+player.getLevelNo()+",您的积分:"+player.getCurScore()+",您所用时:"+player.getElapsedTime()+"秒");
//是否通过最后一关
if(player.getLevelNo()==6){
//计算他的分数
int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()*LevelParam.levels[player.getLevelNo()-1].getStrTimes();
if (score==player.getCurScore()){
System.out.println("恭喜你闯关成功,你已经成为绝世高手了");
System.exit(0);
}
}
}
}else{
System.out.println("输入错误,退出!");
System.exit(1);
}
}
}
玩家类(Player)的方法
玩游戏play()
import java.util.Scanner;
public class Player {
private int levelNo; // 级别号
private int curScore; // 当前积分
private long startTime = 0; // 各级别开始时间
private int elapsedTime; // 各级别已用时间
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
super();
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
public Player() {
super();
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getCurScore() {
return curScore;
}
public void setCurScore(int curScore) {
this.curScore = curScore;
}
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(){
Scanner sc = new Scanner(System.in);
Game game = new Game(this);
for(int i =0;i<LevelParam.levels.length;i++){
this.levelNo+=1;
this.startTime=System.currentTimeMillis();
this.curScore= 0;
for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {
// 游戏字符串
String outStr = game.printStr();
// 用户输入
String inStr = sc.next();
// 游戏判断玩家输入是否正确,并输出相应结果信息
game.printResult(outStr, inStr);
}
}
}
}
最后建一个Test类进行游戏
public class Test {
public static void main(String[] args) {
System.out.println("开始游戏");
Player player = new Player();
player.play();
}
}
这样就可以开始玩这个小游戏了