Javaoop 快速击键系统

package 项目;

import java.util.Random;

public class Game {
	private Player player;	
	/**
	 * 构造函数,传入玩家信息
	 */

	public Game(Player player){
		this.player=player;
	}
	/**
	 * 输出指定级别规定长度的字符串
	 * @return 输出的字符串,用于和用户输入比较   
	 */
	public String printStr(){
		StringBuffer buffer=new StringBuffer();
		Random random=new Random();
		int strLength=LevelParam.levels[player.getLevelNo()-1].getStrLength();
		//通过循环生成要输出的字符串
		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;
			}
		}
		return buffer.toString();
	}
	
	public void printResult(String out,String in){
		if(out.equals(in)){
			long time=System.currentTimeMillis();
			if((time-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) (time - player.getStartTime()) / 1000);
				int levelNo=player.getLevelNo();
				int score=player.getCurScore();
				int elapsedTime=player.getElapsedTime();
				System.out.println("输入正确,您的级别"+levelNo+"您的积分"+score+"已用时间"+elapsedTime+"秒。");
				if(levelNo==6){
					System.out.println("游戏结束");
					System.exit(1);
				}
			}
			
		}else{
			System.out.println("输入错误,退出");
			System.exit(1);
		}
		
	}

}
package 项目;

public class LevelParam extends Level {
	public LevelParam(int levelNo, int strLength, int strTimes, int timeLimit,int perScore) {			
		super(levelNo, strLength, strTimes, timeLimit, perScore);		
	}
	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,34,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);
	}

}

 

 

 

package 项目;

import java.util.Scanner;

public class Player {

	private int levelNo;
	private int curScore;
	private long startTime;
	private int elapsedTime;
	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(){
			Game game=new Game(this);
			Scanner input=new Scanner(System.in);
			//外层循环,循环一次级别晋一级
			for (int i = 0; i < LevelParam.levels.length; i++) {
				//1.晋级
				this.levelNo+=1;
				//2.晋级后计时清零,积分清零
				this.startTime=System.currentTimeMillis();
				this.curScore=0;
				//3.内层循环,循环一次完成一次字符串的输出、输入比较
				for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {
					//游戏输出字符串
					String outStr=game.printStr();
					System.out.println(outStr);
					//接受用户输入
					String inStr=input.next();
					//游戏判断玩家输入是否正确,并输入相应结果信息
					game.printResult(outStr, inStr);
					
				}
			}
		}
	
	
}
package 项目;

public class Level {
	private int levelNo; //级别号
	private int strLength;//各级别一次输出字符串的长度
	private int strTimes;  //各级别输出字符串的次数
	private int timeLimit;  //各级别闯关的时间限制
	private int 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;
	}
	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();
		// TODO Auto-generated constructor stub
	}
	
}
package 项目;

public class myMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Player player=new Player();
		player.play();
		
	}

}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值