通过游戏初步了解面向对象

下面是在刚接触面向对象时候写的,一个打字的小游戏

//玩家类
public class Player {
Scanner input = new Scanner(System.in);
	
private int levelNo;//玩家当前级别号
private int currScore;//当前级别的分数
private long startTime;//当前级别开始时间
private int elapsedTime;//当前级别结束时间
	
public int getLevelNo() {
	return levelNo;
}

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

public int getCurrScore() {
	return currScore;
}

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

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;
}
		
/**
 * 构造方法
 * @param levelNo 当前玩家级别
 * @param currScore 完假分数
 * @param startTime 开始时间
 * @param elapsedTime 当前级别结束时间
 */
public Player(int levelNo, int currScore, long startTime, int elapsedTime) {
this.levelNo = levelNo;
this.currScore = currScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
	
public Player() {}
/**
* 玩游戏
*/
public void play() {
		Game game = new Game(this);
		
	for(int i = 0;i < LevelParam.levels.length ; i ++) {
		//玩家晋级
		this.levelNo += 1;
		//游戏分数清零
		this.currScore = 0;
		//时间从头开始计算
		this.startTime = System.currentTimeMillis();
	//内层循环代表每个玩家需要晋级 所玩的次数
	for(int j = 0;j < LevelParam.levels[levelNo - 1].getstrLength() ; j++) {
		  String out = game.printstre();//系统生成的字符串
	  System.out.println(out);
  	  System.out.println("输入你要输入的字符串");
	  
      String in = input.next();//玩家输入的字符串
      game.printResult(out,in);//比较产生结果
		}
		
	}
	
}	
}
//每一级对应的状态类
public class LevelParam {
	
public final static Level [] levels = new Level[6];

//静态区域块,初始化数据
static {
	levels[0] = new Level(1, 9, 10, 30, 1);
	levels[1] = new Level(2, 8, 9, 26, 2);
	levels[2] = new Level(3, 7, 8, 35, 5);
	levels[3] = new Level(4, 6, 7, 25, 8);
	levels[4] = new Level(5, 5, 6, 28, 10);
	levels[5] = new Level(6, 4, 5, 18, 15);
	}
}

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) {
	this.levelno = levelno;
	this.strLength = strLength;
	this.strTime = strTime;
	this.timeLimit = timeLimit;
	this.perScore = perScore;
}
	
public Level() {}
	
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 Game {
//将玩家类作为属性
private Player player;

public Game(Player player) {

	this.player = player;
}

public Game() {}

/**
 * 生成玩家需要输入的字符串的方法
 * @return 返回生成的字符串
 */
public String printstre() {
	//获取玩家当前等级
	int n = player.getLevelNo();
	String str1 = "";
	String str;

for (int i = 0; i < n; i++) {
	str = "";
	// 0-5的随机数
int r = (int) (Math.random() * 6);

	switch (r) {
	case 0:
		str = "&";
		break;
	case 1:
		str = "*";
		break;
	case 2:
		str = "^";
		break;
	case 3:
		str = "/";
		break;
	case 4:
		str = "<";
		break;
	case 5:
		str = ">";
		break;

	}
	str1 += str;
}
return str1;
}

	/**
	 * 判断玩家输入的和电脑生成的是否相等
	 */
	public void printResult(String out, String in) {

if (out.equals(in)) {	
	long currentTime = System.currentTimeMillis();
	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);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值