下面是在刚接触面向对象时候写的,一个打字的小游戏
//玩家类
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);
}
}
}