那些年我们用Java写过的小游戏 --- 快速击键系统

时间:2022-05-04
本文章向大家介绍那些年我们用Java写过的小游戏 --- 快速击键系统,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

训练技能点

面向对象设计的思想 使用类图理解类的关系 类的封装 构造方法的使用 this、static关键字的使用

需求概述 根据输入速率和正确率将玩家分为不同级别 级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高 规定时间内完成规定次数的输入,正确率达到规定要求,则升级 玩家最高级别为6级、初始级别一律为1级 用户错误输入一次,游戏结束

类图:

 详细代码:

 1.玩家类:

package cn.system;

import java.util.Scanner;

/**
 * 
 * @author: 房上的猫
 * 
 * @time: 下午3:19:54
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        玩家类
 *
 */

public class Player {
    // 玩家当前级别号
    private int leveNo;
    // 玩家当前级别积分
    private int curScore;
    // 当期级别开始时间
    private long startTime;
    // 当前级别已用时间
    private int clapsedTime;

    public Player() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Player(int leveNo, int curScore, long startTime, int clapsedTime) {
        super();
        this.leveNo = leveNo;
        this.curScore = curScore;
        this.startTime = startTime;
        this.clapsedTime = clapsedTime;
    }

    // public Level level;

    // 玩游戏
    public void play() {
        Scanner input = new Scanner(System.in);
        Level level = new Level();
        // TODO Auto-generated method stub

        // 外层循环;控制等级循环次数
        for (int i = 0; i < LevelParam.levels.length; i++) {
            level.setLevelNo(level.getLevelNo() + 1);
            // System.out.println(level.getLevelNo());
            Level count = LevelParam.levels[level.getLevelNo() - 1];
            // 内存循环;控制每等级的次数
            for (int j = 0; j < count.getStrTimes(); j++) {
                Game game = new Game();
                // 将本等级需要的字符数传过去。并接收返回值
                String str = game.printStr(count.getStrLength());
                // 获取当前时间并存入
                setStartTime(System.currentTimeMillis());
                // System.out.println(getStartTime() / 1000);
                System.out.println(str);
                // System.out.println("请输入:");
                game.printResult(str, input.nextLine(), count);
                // 存储已用时间
                this.setClapsedTime((int) ((System.currentTimeMillis() - getStartTime()) / 1000));
                // System.out.println(this.getClapsedTime());
                // System.out.println(this);

                // 比较时间是否超时
                if (this.getClapsedTime() > count.getTimeLimit()) {
                    System.out.println("您输入的太慢了,已超时,退出!");
                    System.exit(1);
                }

                setCurScore(getCurScore() + count.getPerScore());
                // 如果输入正确,输出当前状态
                System.out.println(
                        "输入正确,您的积分为" + getCurScore() + ",您的级别" + count.getLevelNo() + ",已用时" + getClapsedTime());
            }
            // 完成一个等级;系统提示
            System.out.println("晋级!目前等级为:" + count.getLevelNo() + 1);
        }
    }

    public int getLeveNo() {
        return leveNo;
    }

    public void setLeveNo(int leveNo) {
        this.leveNo = leveNo;
    }

    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 getClapsedTime() {
        return clapsedTime;
    }

    public void setClapsedTime(int clapsedTime) {
        this.clapsedTime = clapsedTime;
    }

}

2.级别类:

package cn.system;

/**
 * 
 * @author: 房上的猫
 * 
 * @time: 下午3:27:58
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        级别类
 *
 */

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

    public Level() {
        super();
        // TODO Auto-generated constructor stub
    }

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

}

3.玩家类:

package cn.system;

import java.util.Scanner;

/**
 * 
 * @author: 房上的猫
 * 
 * @time: 下午3:19:54
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        玩家类
 *
 */

public class Player {
    // 玩家当前级别号
    private int leveNo;
    // 玩家当前级别积分
    private int curScore;
    // 当期级别开始时间
    private long startTime;
    // 当前级别已用时间
    private int clapsedTime;

    public Player() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Player(int leveNo, int curScore, long startTime, int clapsedTime) {
        super();
        this.leveNo = leveNo;
        this.curScore = curScore;
        this.startTime = startTime;
        this.clapsedTime = clapsedTime;
    }

    // public Level level;

    // 玩游戏
    public void play() {
        Scanner input = new Scanner(System.in);
        Level level = new Level();
        // TODO Auto-generated method stub

        // 外层循环;控制等级循环次数
        for (int i = 0; i < LevelParam.levels.length; i++) {
            level.setLevelNo(level.getLevelNo() + 1);
            // System.out.println(level.getLevelNo());
            Level count = LevelParam.levels[level.getLevelNo() - 1];
            // 内存循环;控制每等级的次数
            for (int j = 0; j < count.getStrTimes(); j++) {
                Game game = new Game();
                // 将本等级需要的字符数传过去。并接收返回值
                String str = game.printStr(count.getStrLength());
                // 获取当前时间并存入
                setStartTime(System.currentTimeMillis());
                // System.out.println(getStartTime() / 1000);
                System.out.println(str);
                // System.out.println("请输入:");
                game.printResult(str, input.nextLine(), count);
                // 存储已用时间
                this.setClapsedTime((int) ((System.currentTimeMillis() - getStartTime()) / 1000));
                // System.out.println(this.getClapsedTime());
                // System.out.println(this);

                // 比较时间是否超时
                if (this.getClapsedTime() > count.getTimeLimit()) {
                    System.out.println("您输入的太慢了,已超时,退出!");
                    System.exit(1);
                }

                setCurScore(getCurScore() + count.getPerScore());
                // 如果输入正确,输出当前状态
                System.out.println(
                        "输入正确,您的积分为" + getCurScore() + ",您的级别" + count.getLevelNo() + ",已用时" + getClapsedTime());
            }
            // 完成一个等级;系统提示
            System.out.println("晋级!目前等级为:" + count.getLevelNo() + 1);
        }
    }

    public int getLeveNo() {
        return leveNo;
    }

    public void setLeveNo(int leveNo) {
        this.leveNo = leveNo;
    }

    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 getClapsedTime() {
        return clapsedTime;
    }

    public void setClapsedTime(int clapsedTime) {
        this.clapsedTime = clapsedTime;
    }

}

4.工具类:

package cn.system;

/**
 * 
 * @author: 房上的猫
 * 
 * @time: 下午3:31:48
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        存储各级别参数
 *
 */

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

5.测试类:

package cn.system;

import java.util.Scanner;

/**
 * 
 * @author: 房上的猫
 * 
 * @time: 下午3:19:00
 * 
 * @博客地址: https://www.cnblogs.com/lsy131479/
 *
 *        测试类
 *
 */

public class Text {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("是否开始游戏(Y/N)?");
        // 判断用户输入,如果输入非y,退出程序
        // equalsIgnoreCase :忽略大小写进行比较
        if (!input.next().equalsIgnoreCase("Y")) {
            System.out.println("程序退出!");
            System.exit(1);
        }
        System.out.println("游戏开始!");
        // TODO Auto-generated method stub
        Player play = new Player();
        play.play();
    }

}

最后:总结一下下!

一.思路分析

1.确认玩家输入是否正确 如果输入不正确,则直接输出错误信息并退出程序 如果输入正确 如果超时,则直接输出错误信息并退出程序; 如果没有超时 计算玩家当前积分; 计算玩家已用时间; 输出玩家当前级别、当前积分、已用时间; 判断用户是否已经闯过最后一关并处理

2.创建Game对象并传入player属性; 外层循环(循环次数是6,每循环一次玩家级别升一级) 晋级; 积分清零、计时清零; 内层循环(循环次数是该级别的strTime,每循环一次完成一次人机交互) 游戏输出字符串; 玩家输入字符串; 游戏判断玩家输入并输出相应结果。

3.游戏开始之前初始化各个级别的具体参数信息 如各级别号 各级别一次输出字符串的长度 各级别输出字符串的次数 各级别闯关的时间限制 各级别正确输入一次的得分

二.难点提示: (工具类)

使用static修饰属性和代码块 static属性 属于这个类所有 通过“对象名.static属性名” 、“类名.static属性名” 访问 static方法 通过“类名.static方法名”和“对象名.static方法名” 访问 static代码块 主要作用是实现static属性的初始化 当类被载入的时候执行它,且只被执行一次

项目完成!!!可以去得瑟,得瑟了~~~~~~~~~~~~~~~~~~~~~~~

(C) 房上的猫 。 保留所有权利。  https://www.cnblogs.com/lsy131479/

如需转载,请注明出处!!!