简单打字游戏

在这里插package quickHit;


public class Game {
	  private Player player;

	public Game() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Game(Player player) {
		super();
		this.player = player;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}
	//根据当前级别获取字符串的方法
	  public String Printstr() {
		  int strlength = LevelStandard.arr[this.player.getLevel()-1].getStrLength();
			String[] strs= {"1","2","3"};
			String str="";
			for (int i = 0; i < strlength; i++) {
				//产生一个随机数
				int index=(int)(Math.random()*3);
				str+=strs[index];
			}
			return str;
		  
	  }
	  //根据产生的字符串和用户输入的字符串比较是否超时
	  public boolean Judge(String str,String str1) {
		  
	  
		  return str.equals(str1);	  
	  }
}
入代码片
在package quickHit;

public class Level {
	private int levelNum;
	private int strLength;
	private int strCount;
	private int time;
	private int score;
	public Level() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Level(int levelNum, int strLength, int strCount, int time, int score) {
		super();
		this.levelNum = levelNum;
		this.strLength = strLength;
		this.strCount = strCount;
		this.time = time;
		this.score = score;
	}
	public int getLevelNum() {
		return levelNum;
	}
	public void setLevelNum(int levelNum) {
		this.levelNum = levelNum;
	}
	public int getStrLength() {
		return strLength;
	}
	public void setStrLength(int strLength) {
		this.strLength = strLength;
	}
	public int getStrCount() {
		return strCount;
	}
	public void setStrCount(int strCount) {
		this.strCount = strCount;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	
}
这里插入代码片
在这package quickHit;

public class LevelStandard {
	static Level arr[] = new Level[6];
	static {
		//等级 字符长度 字符次数 时间 得分
		arr[0]=new Level(1,1,6,30,1);
		arr[1]=new Level(2,2,5,30,2);
		arr[2]=new Level(3,3,4,30,3);
		arr[3]=new Level(4,4,3,30,4);
		arr[4]=new Level(5,5,2,30,5);
		arr[5]=new Level(6,6,2,30,6);
		
	}
}
里插入代码片
在这package quickHit;

import java.util.Scanner;



public class Player {
	private int time;
	private int score;
	private int level=1;
	public Player() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Player(int time, int score, int level) {
		super();
		this.time = time;
		this.score = score;
		this.level = level;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public void play() {
		Scanner input=new Scanner(System.in);
		Game game = new Game(this);
		Level level = LevelStandard.arr[this.level - 1];
		System.out.println("开始游戏");
		for (int i = this.level - 1; i < LevelStandard.arr.length; i++) {
			//获取当前系统时间
			long startcurrentTimeMillis = System.currentTimeMillis();
			this.time=0;
			this.score=0;
			// 根据当前级别决定产生多少次字符串
			for (int j = 0; j < LevelStandard.arr[this.level - 1].getStrCount(); j++) {
				// 游戏生成字符串
				String str = game.Printstr();
				System.out.println(str);
				//输入字符串
				String inputStr=input.next();
				//判断你输入的字符串和产生的字符串是否一致
				boolean flag=game.Judge(str,inputStr);
				if(flag) {
					//输入一致 在判断时间
					//获取当前系统时间
					long endcurrentTimeMillis = System.currentTimeMillis();
					int usetime=(int)((endcurrentTimeMillis-startcurrentTimeMillis)/1000);
					this.time+=usetime;
					if(this.time<LevelStandard.arr[this.level - 1].getTime()) {
						//在规定时间正确输入了字符
						//积分累积
						this.score+=LevelStandard.arr[this.level - 1].getScore();
						System.out.println("当前级别是"+this.level+"积分是"+this.score+"用时是"+this.time);
					}else {
						//超时了
						System.out.println("超时退出");
						return;
					}
				}else {
					//输入不一致
					System.out.println("游戏结束 输入错误");
					return;
				}
			}
			//升级
			this.level+=1;
			System.out.println("升级了");
		}
		System.out.println("恭喜你闯关成功");
	}
}
里插入代码片
package quickHit;

public class Test {

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

}

在这开始游戏
2
2
当前级别是1积分是1用时是1
2
2
当前级别是1积分是2用时是3
1
1
当前级别是1积分是3用时是6
2
2
当前级别是1积分是4用时是10
3
3
当前级别是1积分是5用时是16
2
2
当前级别是1积分是6用时是22
升级了
12
12
当前级别是2积分是2用时是1
22
22
当前级别是2积分是4用时是3
33
33
当前级别是2积分是6用时是6
31
31
当前级别是2积分是8用时是11
21
21
当前级别是2积分是10用时是17
升级了
232
232
当前级别是3积分是3用时是1
332
332
当前级别是3积分是6用时是4
233
233
当前级别是3积分是9用时是8
322
322
当前级别是3积分是12用时是16
升级了
3121
3121
当前级别是4积分是4用时是2
2312
2312
当前级别是4积分是8用时是6
2222
2222
当前级别是4积分是12用时是12
升级了
11111
11111
当前级别是5积分是5用时是2
12132
12132
当前级别是5积分是10用时是6
升级了
133313
133313
当前级别是6积分是6用时是4
121212
121212
当前级别是6积分是12用时是10
升级了
恭喜你闯关成功
里插入代码片
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值