您的位置:首页 > 产品设计 > UI/UE

Day16:QuickHit项目

2020-07-28 09:10 169 查看

QuickHit项目

项目需求

QuickHit游戏考验学员键盘输入内容的速度和准确性。一次显示的字符数越多,级别越高,根据输入速率和正确率将玩家分为不同级别,在规定时间内完成规定次数的输入,正确率高的玩家正确输入一次的得分也越高。如果玩家在规定时只要输入错误次,则游戏结束。达到规定要求,则玩家升级(为了简单起见,规定用户只玩家最高级别为6级,初始级别一律为 一级)。

案例覆盖的技能点

➢面向对象程序设计的思想。
➢使用类图理解类的关系。
➢类的封装。
➢构造方法的使用。
➢this 和static关键字的使用。

项目实现思路

QuickHit项目的执行步骤描述如下。
(1)游戏根据玩家的级别在控制台输出指定数量的字符。
(2)玩家根据控制台的输出来输入相同字符,按Enter键确认。
(3)游戏确认玩家输入是否正确。
(4)如果输入错误,则输出玩家输入错误提示信息,游戏非正常结束。
(5)如果输入正确但超时,则输出玩家速度太慢提示信息,游戏非正常结束。
(6)如果输入正确且没有超时,则输出玩家的积分、级别和用时信息。然后重复以上步骤,继续输出、输入和确认。
(7)玩家在规定时间内连续正确输入规定次数后,将显示玩家升级提示,游戏将重新计时计分,将一次输出更多字符。6级玩家闯关成功,输出恭喜信息,游戏正常结束。

public class Game {
private Player player;

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
}

public Game(Player player){
super();
this.player=player;
}
/*
输出字符串
*/
public String printStr(){
StringBuffer buffer=new StringBuffer();//在循环中执行字符串拼接操作
Random random=new Random();
for(int i=0;i<LevelParam.levels[player.getLevelNo()-1].getStrLength();i++){
int rand=random.nextInt(6);//产生0-5之间的随机数
switch (rand){
case 0: {
buffer.append("1");
break;
}
case 1: {
buffer.append("2");
break;
}
case 2:{
buffer.append("3");
break;
}
case 3:{
buffer.append("4");
break;
}
case 4:{
buffer.append("5");
break;
}
case 5:{
buffer.append("6");
break;
}
}
}
System.out.println(buffer.toString());
return buffer.toString();
}
/*
确认玩家输入是否正确和输出相应的结果信息
*/
public void printResult(String out,String in){
//输入和输出相同时:判断是否超时;否则闯关失败
if(out.equals(in)){
long currentTime=System.currentTimeMillis();//获得自1970-1-1到现在的时间距离,单位是毫秒
if((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());
System.out.println("输入正确,您的积分是"+player.getCurrScore()+"分," +
"您的级别是"+player.getLevelNo()+"级," +
"您所用时间"+(currentTime-player.getStartTime())/1000+"秒");
if(player.getLevelNo()==6){
int score=LevelParam.levels[player.getLevelNo()-1].getPerScore()*LevelParam.levels[player.getLevelNo()-1].getStrTime();
if(player.getCurrScore()==score){
System.out.println("闯关成功!!!");
System.exit(0);
}
}
}
}else {
System.out.println("输入错误,闯关失败!!!");
System.exit(1);
}
}
}
public class Level {
private int levelNo;//玩家当前级别号
private int strLength;//各级别一次输出字符串的长度
private int strTime;//各级别输出字符串的次数
private int timeLimit;//各级别闯关的时间限制
private int perScore;//各级别正确输入一次的得分

/*
构造方法
*/
public Level(int levelNo,int strLength,int strTime,int timeLimit,int perScore){
super();
this.levelNo=levelNo;
this.strLength=strLength;
this.strTime=strTime;
this.timeLimit=timeLimit;
this.perScore=perScore;
}
public Level(){
super();
}
/*
设置setter、getter()方法
*/
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 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);
}
}
public class Player {
private int levelNo;//当前级别号
private long startTime;
private int currScore;//当前级别积分
private long elapseTime;//每级别游戏持续时间

public int getLevelNo() {
return levelNo;
}

public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}

public long getStartTime() {
return startTime;
}

public void setStartTime(long startTime) {
this.startTime = startTime;
}

public int getCurrScore() {
return currScore;
}

public void setCurrScore(int currScore) {
this.currScore = currScore;
}

public long getElapseTime() {
return elapseTime;
}

public void setElapseTime(long elapseTime) {
this.elapseTime = elapseTime;
}

/*
构造方法
*/
public Player(int levelNo,int currScore,long startTime,int elapseTime){
super();
this.levelNo=levelNo;
this.currScore=currScore;
this.startTime=startTime;
this.elapseTime=elapseTime;
}
public Player(){
super();
}
/*
方法:玩家玩游戏
*/
public void play(){
Game game=new Game(this);
Scanner scanner=new Scanner(System.in);
//外层循环代表等级
for(int i=0;i<LevelParam.levels.length;i++){
levelNo +=1;//玩家晋级
startTime=System.currentTimeMillis();//获得新的游戏开始时间
currScore=0;//每次晋级玩家积分清零
//内层循环代表每个级别所需要玩的次数
for(int j=0;j<LevelParam.levels[i].getStrTime();j++){
String out=game.printStr();//系统生成的字符串
String in=scanner.next();//玩家输入的字符串
game.printResult(out,in);//比较产生结果
}
}
}
}
public class Test {
public static void main(String[] args) {
Player player=new Player();
player.play();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: