JAVA打字小游戏,面向对象完成

JAVA打字小游戏,面向对象完成,第1张

测试类,测试类主要是用于引用,将我们进行引入

package com.ytzl.Project.work;

public class Test {
    public static void main(String[] args) {
        Player player = new Player();
        player.play();
    }
}

 实体类1,该类主要用于输出系统随机字符,接收玩家打印字符,并传入game类进行判断

package com.ytzl.Project.work;

import java.util.Scanner;

public class Player {
    private int levelNo;//当前级别号
    private int currScore;//当前级别积分
    private long startTime;//开始时间
    private long elapsedTime;//已用时间

    public Player() {
    }

    public Player(int levelNo, int currScore, long startTime, long elapsedTime) {
        this.levelNo = levelNo;
        this.currScore = currScore;
        this.startTime = startTime;
        this.elapsedTime = 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 long getElapsedTime() {
        return elapsedTime;
    }

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

    //玩游戏
    public void play(){
        Game game = new Game(this);
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < LevelParam.levels.length; i++) {
            //晋级
            this.levelNo+=1;
            //2.时间开始计时
            this.startTime = System.currentTimeMillis();
            //3.积分清零
            this.currScore = 0;
            //4.内层循环 循环的是电脑输出,玩家输入,比较
            for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {
                //4.1 电脑输出字符串
                String out = game.printStr();
                //4.2 接受用户输入
                String in = scanner.next();
                //4.3 比较,并判断结果
                game.printResult(out,in);
            }
        }
    }
}

 实体类2,该类主要用于创造随机字符,根据玩家打印字符进行判断。

package com.ytzl.Project.work;

import java.util.Random;

public class Game {
    /*
    * 构造方法传入玩家信息
    * */
    private Player player;

    public Game(Player player) {
        this.player=player;
    }

    String printStr(){
        //从创建好的player对象中获取级别号
        //因为我们需要将Game中和player中的
        //两个方法对应的级别统一
        int strLength = LevelParam.
                levels[player.
                getLevelNo() -1].getStrLength();

        //随机数
        Random random = new Random();
        // *** 作字符串
        StringBuffer buffer = new StringBuffer();
        //buffer.append() 字符串的连接
        for (int i = 0; i < LevelParam.levels[player.getLevelNo()-1].getStrLength(); i++) {
            //生成随机数
            int rand=random.nextInt(4);
            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;
                default:
                    break;
            }
        }
        System.out.println(buffer.toString());
        return buffer.toString();
    }

    public void printResult(String out,String in){
        boolean flag;
        if (out.equals(in)){
            flag=true;
        }else {
            flag=false;
        }
        //第一层:是否输入正确
        if (flag){
            //第二层:是否超时
            //currentTime :输入正确之后拿到的时间
            long currentTime=System.currentTimeMillis();
            if (
                    //currentTime : 160345678325
                    //getStartTime : 160345678330
                    (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());
                //当前所用时间,用每一次输入正确之后的
                //时间减去初始进入游戏时间
                player.setElapsedTime((currentTime-player.getStartTime())/1000);
                //输出语句
                System.out.println("输入正确,您的级别为:"+player.getLevelNo()+
                        ",您的积分为:"+player.getCurrScore()+
                        ",已用时间为:"+player.getElapsedTime()+
                        "秒");
                //判断是否闯关成功
                if (player.getLevelNo() == 6){
                   int scroe= LevelParam.levels[player.getLevelNo()-1].getStrTimes()
                            *LevelParam.levels[player.getLevelNo()-1].getPerScore();
                    System.out.println("闯关成功!");
                    System.out.println("总积分为:"+scroe);
                    System.exit(1);
                }
            }
        }else {
            System.out.println("输入错误!退出");
            System.exit(1);
        }
    }
}

实体类3,该类用于存储游戏等级,字符长度,字符次数,时间限制,正确得分

package com.ytzl.Project.work;

public class Level {
    private int levelNo;//各级别号
    private int strLength;//各级输入字符长度
    private int strTimes;//各级数输入字符次数
    private int timeLimit;//各级闯关时间限制
    private int perScore;//各级输入正确得分

    public Level() {
    }

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

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

实体类4,该类为数组类,用来设置level中的数据

package com.ytzl.Project.work;

public class LevelParam {
    public final static com.ytzl.Project.work.Level levels[]=new com.ytzl.Project.work.Level[6];//对应六个级别
    static {
        levels[0] = new Level(1,2,10,30,1);
        levels[1] = new Level(2,3,26,26,2);
        levels[2] = new Level(3,4,33,33,4);
        levels[3] = new Level(4,5,18,18,6);
        levels[4] = new Level(5,6,14,14,8);
        levels[5] = new Level(6,7,13,12,10);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存