快速射击游戏
随机输入字符串,然后和系统随机生成的相匹配
实现升级
实现积分累计
实现时间控制
在这里插入代码片
package com.uplooking;
import com.sun.org.apache.xpath.internal.functions.FuncStringLength;
import java.util.Random;
public class Game {
private Player palyer;
public Game(Player player) {
this.palyer = player;
}
public String printStr() {
int stringLength = Levelparam.levels[palyer.getLevelNo() - 1].getStrLength();
StringBuffer buffer=new StringBuffer();
Random random = new Random();//生成随机数
for(int i=0;i< stringLength;i++) {
//根据随机数拼接字符串
int rand = random.nextInt(stringLength);
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;
}
}
//输出字符串
System.out.println(buffer);
return buffer.toString();//调用toString函数
}
//比较字符串
public void printResult(String out, String in){boolean flag;
//先判断输入和输出是否一致
if (out.equals(in)) {
flag=true;
} else {
flag=false;
}
if(flag){
//输入和输出是一致,判断是否超时
long currentTime=System.currentTimeMillis();
if ((currentTime-palyer.getStartTime())/1000>Levelparam.levels[palyer.getLevelNo()-1].getTimeLimit())
{
System.out.println("你输入太慢了,已经超时,退出");
System.exit(1);
}else{
//计算积分
palyer.setCurScore(palyer.getCurScore()+Levelparam.levels[palyer.getLevelNo()-1].getPerScore());
//计算已用时间
palyer.setElapsedTime((int) ((currentTime-palyer.getStartTime())/1000));
//输出当前已用时间,当前积分,当前级别
System.out.println("您当前的级别为:"+palyer.getLevelNo()+"您当前的积分为:"+palyer.getCurScore()+"您当前的已用时间为"+palyer.getElapsedTime());
//判断是否闯过最后一关,如果闯过结束程序
if(palyer.getLevelNo()==6){
//计算他的分数
int score=Levelparam.levels[palyer.getLevelNo()-1].getPerScore()*Levelparam.levels[palyer.getLevelNo()-1].getStrTime();
if (score==palyer.getCurScore()){
System.out.println("恭喜你闯关成功,你已经成为绝世高手了");
System.exit(0);
}
}
}
}else{
System.out.println("输入错误 ,游戏结束");
System.exit(1);
}
}
}
package com.uplooking;
public class Level {
private int levelNo; // 级别号
private int strLength; // 各级别一次输出字符串的长度
private int strTime; // 各级别输出字符串的次数
private int timeLimit; // 各级别闯关的时间限制
private int perScore; // 各级别成功输入一次字符串后增加的分值
public Level() {
}
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 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;
}
}
package com.uplooking;
import cn.kgc.quickhit.Level;
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);
}
}
package com.uplooking;
import java.util.Scanner;
public class Player {
private int levelNo; // 级别号
private int curScore; // 当前积分
private long startTime = 0; // 各级别开始时间
private int elapsedTime; // 各级别已用时间
public Player() {
}
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = 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++){
this.levelNo=+1;
//升级后积分清零,时间清零
this.curScore=0;
this.startTime=System.currentTimeMillis();
for (int j = 0; j < Levelparam.levels[levelNo-1].getStrTime(); j++) {
// 3.1、游戏输出字符串
String outStr = game.printStr();
// 3.2、接收用户输入
String inStr = input.next();
// 3.3、游戏判断玩家输入是否正确,并输出相应结果信息
game.printResult(outStr, inStr);
}
}
}
}
package com.uplooking;
public class Test {
public static void main(String[] args) {
System.out.println("***********欢迎来到快速射击**********");
System.out.println("***********游戏即将开始,请做好准备*****");
Player player = new Player();
player.play();
}
}
总结
本程序使用java基础,
1.对于对象数组的使用
2.生成随机字符串函数的使用
3.对于java中时间函数的使用,获取时间戳