20200728-Java实战小项目-(小游戏:键盘输入随机生成的字符,晋级通关)
项目要求:
1、根据输入速率和正确率将玩家分为不同级别
2、级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
3、规定时间内完成规定次数的输入,正确率达到规定要求,则升级
4、用户输入错误一次,游戏结束
代码:
类–>游戏级别设置
package cn.kgc.kb09.QickHit;
public class Level {
public int levelNo; //级别编号
public int strLength; //各级别一次输入字符串长度
public int strTimes; //各级别输出字符串的次数
public int timeLimit; //各级别闯关时间限制
public int perScore; //各级别正确输入一次的得分
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;
}
}
类—>玩家属性
package cn.kgc.kb09.QickHit;
public class Player {
public int levelNo; //当前级别编号
public int surScore; //当前级别积分
public long startimes; //当前级别开始时间
public int elapsedTime;//当前级别以用时间
public Player() {
}
public Player(int levelNo, int surScore, long startimes, int elapsedTime) {
this.levelNo = levelNo;
this.surScore = surScore;
this.startimes = startimes;
this.elapsedTime = elapsedTime;
}
public void show(){
System.out.println("当前级别编号:"+levelNo+", 当前积分:"+surScore+", 已使用时间:"+elapsedTime);
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getSurScore() {
return surScore;
}
public void setSurScore(int surScore) {
this.surScore = surScore;
}
public long getStartimes() {
return startimes;
}
public void setStartimes(long startimes) {
this.startimes = startimes;
}
public int getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
}
public void play() {
long currentTime = System.currentTimeMillis();
elapsedTime = (int) (currentTime - getStartimes())/1000;
if (elapsedTime > LevelParam.levels[getLevelNo() - 1].getTimeLimit()) {
System.out.println("你输入的太慢了,已经超时,退出游戏!");
System.exit(1);
}
}
}
类—>级别参数
package cn.kgc.kb09.QickHit;
/**
/
public class LevelParam {
public final static Level levels[] = new Level[6];
static {
levels[0] = new Level(1,3,3,30,2);
levels[1] = new Level(1,5,3,35,3);
levels[2] = new Level(1,7,3,40,4);
levels[3] = new Level(1,9,3,45,5);
// levels[4] = new Level(1,11,3,50,6);
// levels[5] = new Level(1,13,3,55,7);
}
}
类—>开始游戏
package cn.kgc.kb09.QickHit;
import java.util.Random;
import java.util.Scanner;
public class Game {
Player p = new Player();
Level[] levels = new Level[6];
public void run() {
System.out.println("欢迎来到打字游戏:");
}
public void printStr() {
System.out.println("随机生成一串字符:");
StringBuffer buffer = new StringBuffer();
Random random = new Random();
p.setSurScore(0);
a: for (int j = 0; j < 4; j++) {
//加了个标号,直接在内层循环跳出外层循环
p.setStartimes(System.currentTimeMillis());
p.setLevelNo(j + 1);
for (int k = 0; k < LevelParam.levels[j].strTimes; k++) {
for (int i = 0; i < LevelParam.levels[j].strLength; i++) {
int rand = random.nextInt(LevelParam.levels[j].strLength);
switch (rand) {
case 0:
buffer.append("a");
break;
case 1:
buffer.append("b");
break;
case 2:
buffer.append("g");
break;
case 3:
buffer.append("f");
break;
case 4:
buffer.append("s");
break;
case 5:
buffer.append("y");
break;
}
}
System.out.println(buffer.toString());
System.out.println("请输入字符串:");
Scanner input = new Scanner(System.in);
String str = input.next();
p.play();
if (str.equals(buffer.toString())) {
System.out.println("输入正确,进入下一关!");
p.setSurScore(p.getSurScore()+LevelParam.levels[p.getLevelNo()-1].getPerScore());
p.show();
buffer.delete(0,LevelParam.levels[j].strLength);
} else {
System.out.println("输入有误");
break a;
}
}
p.setElapsedTime(0);
}
}
}
测试类:
public class TestGame {
public static void main(String[] args) {
Game game = new Game();
game.run();
game.printStr();
}
}