run in this way,   no why,   only for you heart
CSDN博客(点击进入) CSDN
51CTO(点击进入) 51CTO

QuickHit游戏

需求
这里写图片描述

游戏功能
这里写图片描述

游戏玩家类

/**
 * 玩家类
 * 
 * @author hp
 *
 */
public class Player {
    private int levelNo;// 玩家当前级别
    private int currrScore;// 玩家积分
    private long startTime;// 开始时间
    private long elapsedTime;// 结束时间

    public Player() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Player(int levelNo, int currrScore) {
        super();
        this.levelNo = levelNo;
        this.currrScore = currrScore;
    }

    public int getLevelNo() {
        return levelNo;
    }

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

    public int getCurrrScore() {
        return currrScore;
    }

    public void setCurrrScore(int currrScore) {
        this.currrScore = currrScore;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getElapsedTime() {
        return elapsedTime;
    }

    public void setElapsedTime(long elapsedTime) {
        this.elapsedTime = elapsedTime;
    }

    /**
     * 玩游戏
     */
    public void play() {

    }
}

游戏级别类

/**
 * 级别类
 * 
 * @author hp
 *
 */
public class Level {
    private int levelNo;// 各级别号
    private int strLength;// 各级别一次输出字符串的长度
    private int strTime;// 各级别输出字符串的次数
    private int timeLinmit;// 各级别闯关的时间限制
    private int perScore;// 各级别正确输入一次得分

    public Level() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Level(int levelNo, int strLength, int strTime, int timeLinmit,
            int perScore) {
        super();
        this.levelNo = levelNo;
        this.strLength = strLength;
        this.strTime = strTime;
        this.timeLinmit = timeLinmit;
        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 getTimeLinmit() {
        return timeLinmit;
    }

    public void setTimeLinmit(int timeLinmit) {
        this.timeLinmit = timeLinmit;
    }

    public int getPerScore() {
        return perScore;
    }

    public void setPerScore(int perScore) {
        this.perScore = perScore;
    }

}

游戏级别初始化类

/**
 * 各级别具体参数信息
 * 
 * @author hp
 *
 */
public class LevelParam {
    public final static Level[] level = new Level[6];
    static {
        level[0] = new Level(1, 2, 10, 30, 1);
        level[1] = new Level(2, 3, 9, 26, 2);
        level[2] = new Level(3, 4, 8, 22, 5);
        level[3] = new Level(4, 5, 7, 18, 8);
        level[4] = new Level(5, 6, 6, 15, 10);
        level[5] = new Level(6, 7, 5, 12, 15);
    }
}

游戏类

import java.util.Random;

/**
 * 游戏类
 * 
 * @author hp
 *
 */
public class Game {
    Player p = new Player();// 得到玩家当前属性

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

    /**
     * 输出字符串
     * 
     * @return
     */
    public String printStr() {
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();
        LevelParam lp = new LevelParam();
        int num = lp.level[p.getLevelNo() - 1].getStrLength(); // 获得账号的级别输入一次字符串的长度
        for (int i = 0; i < num; i++) {
            int rand = random.nextInt(num);
            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, long currentTime1,
            long currentTime) {
        if (out.equalsIgnoreCase(in)) {
            boolean flag = false;
            p.setStartTime(currentTime1);
            p.setElapsedTime(currentTime);
            if ((currentTime1 - currentTime) / 1000 > LevelParam.level[p
                    .getLevelNo() - 1].getTimeLinmit()) {
                System.out.println("你输入太慢了,已经超时,退出!");
                System.exit(1);
            } else {
                p.setCurrrScore(p.getCurrrScore()
                        + LevelParam.level[p.getLevelNo() - 1].getPerScore());
            }
        } else {
            System.out.println("输入错误,退出!");
            System.exit(1);
        }
    }

}

游戏测试、主函数类

/**
 * 游戏开始类
 * 
 * @author hp
 *
 */
public class Strat {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Player p = new Player(1, 2); // 得到玩家当前属性
        Game game = new Game(p);
        long currentTime1 = System.currentTimeMillis();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 3; j++) {
                String out = game.printStr();
                System.out.println(out);
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入:");
                String in = sc.next();
                long currentTime = System.currentTimeMillis();
                game.printResult(out, in, currentTime1, currentTime);
                System.out.println("输入正确,您的积分" + p.getCurrrScore() + ",您的级别"
                        + p.getLevelNo() + ",已用时间"
                        + (p.getElapsedTime() - p.getStartTime()) / 1000);
            }
            p.setLevelNo(p.getLevelNo() + 1);// 晋级
            System.out.println("恭喜晋级为" + p.getLevelNo() + "级!");
            p.setCurrrScore(0);
            if (p.getLevelNo() > 6) {
                System.out.println("您已满级通关!");
                break;
            }
        }

    }
}

运行截图
这里写图片描述

posted @ 2017-08-05 21:44  _小龙人  阅读(143)  评论(0编辑  收藏  举报