QuickHit游戏

一 项目需求

根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见,规定用户只要错误输出一次,则游戏结束).玩家最高级别为6级,初始级别一律为一级.

项目所覆盖的知识点:

①面向对象设计的思想.

②使用类图理解类的关系

③类的封装

④构造方法的使用

⑤this和static关键字的使用

运行效果图:

玩家输入正确的界面

玩家输入错误的界面

玩家输入超时的界面

二 问题分析

1.需要使用到的类

游戏类(Game),玩家类(Player),和级别类(Level)

2.发现类的属性:

玩家(Player)类:玩家当前级别(levelNo),玩家当前级别积分(currScore),当前级别开始时间(StartTime),当前级别已用时间(elapsedTime)

级别(Level)类:个级别编号(levelNo),个级别一次输出字符串的长度(strLength),个级别输出字符串的次数(strTime),个级别闯关的时间限制(timeLimit)和各级别正确输入一次的得分(perScore).

Game(游戏)类:在游戏类中要添加一个玩家的属性player

3.发现类的方法:

玩家类(Player)的方法:play();

游戏类(Game)的方法:

String printStr(),输出字符串,返回字符串用于和玩家的输入进行比较。

void printResult(String out,String in)比较输出out和玩家输入in

三 项目实现功能

Player类

package cn.quickhit;

import java.util.Scanner;
/*
 * 玩家类
 */
public class Player {
    public int levelNo;// 级别号
    public int currScore;// 当前积分
    public long startTime;// 各级别开始时间
    public 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;
    }
      //玩家玩游戏的方法
    public void play() {
    // 调用游戏类的带参构造传入玩家对象
         Game game=new Game(this);
         Scanner input=new Scanner(System.in);
         //外层循环,循环一次级别晋级一次
         for (int i = 0; i < Levelparam.level.length; i++) {
             //晋级
            this.levelNo+=1;
            //晋级后计时清零,积分清零
            this.startTime=System.currentTimeMillis();
            this.currScore=0;
            if(this.levelNo==6)
            {
                System.out.println("恭喜通关");
                break;
            }
            //内层循环,循环一次完成一次字符串的输出,输入,比较
            for (int j = 0; j < Levelparam.level[levelNo-1].getStrTimes(); j++) {
                //游戏输出字符串
                String outstr=game.printstr();
                //接受用户输入
                String instr=input.next();
                //游戏判断玩家输入是否正确,并输出相应结果信息
                game.printResult(outstr, instr);
            }
        }
    }
}

Game类

package cn.quickhit;

import java.util.Random;
/*
 * 游戏类
 */
public class Game {
    public Player player;//代表玩家

    public Game(Player player) {
        this.player = player;
    }
    /**
     * 生成字符串
     */
    public String printstr() {
        //获取级别对应的要输出字符串的长度
        int strLength = Levelparam.level[player.getLevelNo() - 1].getStrLength();
        StringBuffer buffer=new StringBuffer();
        //实例化生成随机数的对象
        Random random=new Random();
        //通过循环生成要输出的字符串
        for (int i = 0; i < strLength; i++) {
            //产生随机数
            int rand=random.nextInt(strLength);
            //根据随机数拼接字符数
            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();
    }

    //系统给的字符串和用户输入的字符串对比
    //out  系统输出的字符串
    //in 用户输入的字符串
    public void printResult(String out,String in){
        boolean flag=false;  //定义标记默认不同
        if(out.equals(in)){
            //证明两个字符串相同
            flag=true;  //改变标记
        }else{
            System.out.println("输出错误,退出");
            System.exit(0);
        }

        /**
         * 如果输入正确则会出现两种情况 01.如果超时,则直接输出错误信息并退出程序 02.如果没有超时: 计算玩家当前积分 计算玩家已用时间
         * 输出玩家当前级别,当前积分和已用时间 判断用户是否已经闯过最后一关
         * */
        if(flag){
        long currentTime=System.currentTimeMillis();
        //如果超时
        if((currentTime-player.getStartTime())/1000>Levelparam.level[player.getLevelNo()-1].getTimeLimit())
        {
            System.out.println("您输入太慢了,已经超时,退出");
            System.exit(1);
        }
        //如果没有超时
        else{
            //计算玩家当前积分
            player.setCurrScore(player.getCurrScore()+Levelparam.level[player.getLevelNo()-1].getPerScore());
            //计算玩家已用时间
            player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
            //输出玩家当前级别,当前积分和已用时间
            System.out.println("输入正确,您的级别:"+player.levelNo+"您的积分:"+player.currScore+"已用时间:"+player.elapsedTime+"秒");
        }

        }
    }
}

Level类

package cn.quickhit;
/*
 * 级别类
 */
public class Level {
    public int levelNo;  // 级别号
    public int strLength;// 各级别一次输出字符串的长度
    public int strTimes;// 各级别输出字符串的次数
    public int timeLimit;// 各级别闯关的时间限制
    public int 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 getStrTimes() {
        return strTimes;
    }

    public void setStrTimes(int strTimes) {
        this.strTimes = strTimes;
    }

    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 Level(int levelNo, int strLength, int strTimes, int timeLimit,
            int perScore) {
        this.levelNo = levelNo;
        this.strLength = strLength;
        this.strTimes = strTimes;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }

}

LevelParam类

package cn.quickhit;
/*
 * 级别类
 */
public class Level {
    public int levelNo;  // 级别号
    public int strLength;// 各级别一次输出字符串的长度
    public int strTimes;// 各级别输出字符串的次数
    public int timeLimit;// 各级别闯关的时间限制
    public int 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 getStrTimes() {
        return strTimes;
    }

    public void setStrTimes(int strTimes) {
        this.strTimes = strTimes;
    }

    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 Level(int levelNo, int strLength, int strTimes, int timeLimit,
            int perScore) {
        this.levelNo = levelNo;
        this.strLength = strLength;
        this.strTimes = strTimes;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }

}

Test类

package cn.quickhit;

public class Test {

    /**
     * @param args
     * 测试类
     */
    public static void main(String[] args) {
        //实例化玩家对象
        Player player=new Player();
        //调用玩家玩游戏的方法
        player.play();
    }

}
时间: 2024-08-13 13:59:23

QuickHit游戏的相关文章

跟王老师学Java三大特性(二):案例 QuickHit:游戏输出字符串

案例 QuickHit:游戏输出字符串 主讲教师:王少华   QQ群号:483773664 学习目标 完成游戏输出字符串 一.需求说明 在控制台输出随机字符串 二.思路分析 生成字符串 输出字符串 返回字符串 三.难点提示 Game类中的player属性,代表玩家,查询player的级别号,根据级别号到LevelParam类中获取该级别的字符串长度 字符串长度固定可以通过for循环来实现,而随机内容可以通过获取随机数,而不同随机数对应不同字符来实现 四.参考代码 1 2 3 4 5 6 7 8

跟王老师学Java三大特性(四):案例 QuickHit:玩家玩游戏

案例 QuickHit:玩家玩游戏 主讲教师:王少华   QQ群号:483773664 学习目标 完成Player类中的play方法 一.需求说明 玩家玩游戏 二.思路分析 创建Game对象并传入player属性: 外层循环(循环次数是6,每循环一次玩家级别升一级) 晋级: 积分清零.计时清零: 内层循环(循环次数是该级别的strTime,每循环一次完成一次人机交互) 游戏输出字符串: 玩家输入字符串: 游戏判断玩家输入并输出相应结果. 三.参考代码 1 2 3 4 5 6 7 8 9 10 1

QuickHit项目(输出字符串游戏)

public class leve { private int leveNo; private int strLength; private int strTimes; private int timeLimit; private int perScore; public leve(int leveNo, int strLength, int strTimes, int timeLimit, int perScore) { super(); this.leveNo = leveNo; this.

跟王老师学Java三大特性(一):案例 QuickHit:需求分析

项目案例:QuickHit:需求分析 主讲教师:王少华   QQ群号:483773664 学习目标 学会用面向对象思想来进行需求分析 一.需求 根据输入速率和正确率将玩家分为不同级别 级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高 规定时间内完成规定次数的输入,正确率达到规定要求,则升级 玩家最高级别为6级.初始级别一律为1级 用户错误输入一次,游戏结束 二.面向对象分析 (一) 发现类 玩家(Player)类 游戏(Game)类 级别(Level)类 (二)发现类的属性 1.玩家

第五章项目:QuickHit

需求概述: 根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见,规定用户只要错误一次,则游戏结束).最高为6级,刚开始一律1级. 案例覆盖的技能点: 面向对象设计的思想 使用类图理解类的关系 类的封装 构造方法的使用 this和static关键字的使用 需要用到的类: 玩家(Player)类:当前级别号(levelNo),当前级别积分(currScore),当前

跟王老师学Java三大特性(三):案例 QuickHit:确认输入并输出结果

案例 QuickHit:确认输入并输出结果 主讲教师:王少华   QQ群号:483773664 学习目标 完成Game类中的printResult方法的编写 一.需求说明 确认用户输入并输出结果 二.思路分析 确认玩家输入是否正确 如果输入不正确,则直接输出错误信息并退出程序 如果输入正确 如果超时,则直接输出错误信息并退出程序 如果不超时 计算玩家当前积分 计算 玩家已用的时间 输出当前玩家的级别.当前积分.已用时间: 判断用户是已经闯过最后一关并处理 三.参考代码 1 2 3 4 5 6 7

20170913自制猜数字游戏

/* 猜数字:系统随机生成一个四位数,请根据下列判断猜出来 A:数值正确,位置正确 B:数值正确,位置不正确 C:数值不正确 */ #include<stdio.h> #include<time.h> #include<stdlib.h> #pragma warning (disable:4996) #define pUCharHead unsigned char * //以数组形式返回n个无重复的随机数,范围可指定[min,max] pUCharHead GenNoR

洛谷P1199 三国游戏

题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之间有一个"默契值",表示若此两位武将作为一对组合作战时,该组合的威力有多大.游戏开始前,所有武将都是自由的(称为自由武将,一旦某个自由武将被选中作为某方军队的一员,那么他就不再是自由武将了),换句话说,所谓的自由武将不属于任何一方. 游戏开始,小涵和计算机要从自由武将中挑选武将组成自己的军

游戏开发 系统app开发游戏定制开发找谁

梦幻珍珠港理财拆分游戏系统软件开发怎么做?找[江生:185-2911-8412 微电]. 梦幻珍珠港拆分游戏平台开发.梦幻珍珠港理财全网模式开发.梦幻珍珠港收益模式介绍开发. 梦幻珍珠港拆分游戏系统源码搭建平台!!我们是软件开发,玩家勿扰!非平台客服,玩家勿扰!] 游戏规则: 一.开发新会员要从您的库房扣除3 03颗珍珠,体系扣除5颗珍珠,新会员有298颗珍珠. 二.推荐老友,推荐人能够得到第一代会员的2%,第二代会员的1%,第三代会员的0.5%,一代的收益就是5.96颗珍珠奖赏(可转换为等量可