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;
            }
        }

    }
}

运行截图
这里写图片描述

转载于:https://www.cnblogs.com/wangqilong/p/8279806.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值