【问题标题】:How is this code capturing user input without pressing enter? [closed]此代码如何在不按 Enter 的情况下捕获用户输入? [关闭]
【发布时间】:2014-10-28 23:59:50
【问题描述】:

在 Snake 游戏中,用户在其中键入选项,游戏将带您进入该选项,而无需按 Enter。你是怎么做到的?

但请记住,我是初学者,所以请向我解释任何您认为困难的事情。游戏可以在这里找到http://www.dostips.com/forum/viewtopic.php?f=3&t=4741

【问题讨论】:

    标签: file batch-file choice


    【解决方案1】:

    dbenham 制作的蛇代码非常复杂,但是由于您的问题仅限于输入机制,所以这不是问题。

    这个贪吃蛇游戏使用choice 命令,它只需要按下一个指定的键,不需要再按下一个键。根据用户按下的键,变量errorlevel 将更改为该字符在选项列表中的数字位置。

    为帮助屏幕键入 choice /?。摘录如下:

    CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
    
    Description:
        This tool allows users to select one item from a list
        of choices and returns the index of the selected choice.
    
    Parameter List:
       /C    choices       Specifies the list of choices to be created.
                           Default list is "YN".
    
       /N                  Hides the list of choices in the prompt.
                           The message before the prompt is displayed
                           and the choices are still enabled.
    
       /CS                 Enables case-sensitive choices to be selected.
                           By default, the utility is case-insensitive.
    
       /T    timeout       The number of seconds to pause before a default
                           choice is made. Acceptable values are from 0 to
                           9999. If 0 is specified, there will be no pause
                           and the default choice is selected.
    
       /D    choice        Specifies the default choice after nnnn seconds.
                           Character must be in the set of choices specified
                           by /C option and must also specify nnnn with /T.
    
       /M    text          Specifies the message to be displayed before
                           the prompt. If not specified, the utility
                           displays only a prompt.
    
       /?                  Displays this help message.
    

    这是一个例子:

    C:\> set errorlevel=-1
    C:\> choice /c "YN" /m "Yes or No"
    Yes or No: [Y,N]? Y
    
    C:\> Echo %errorlevel%
    1
    
    C:\>
    

    【讨论】:

    • Snake.bat 使用 xcopy /w 作为 2.0 版的输入
    • 是的,但由于 OP 明确表示他是初学者,我想我会给他choice 命令,因为它更简单。无论如何,谢谢你把它捡起来。
    • 感谢您记住我有点菜鸟,但您能解释一下错误级别是什么吗?我尝试查找它,但显然它有点复杂,请告诉我 xcopy 是什么,谢谢!
    • Errorlevel 是一个变量,一些批量命令通常会更改它以传达它们刚刚返回的错误类型。 Choice 使用它来传达用户所做的选择。 Xcopy 是一个批处理命令,用于复制文件时的附加功能,但是由于它的许多参数,它通常对其他东西有用。
    • 感谢您的解释,但这是我的另一个问题,errorlevel 用于传达什么样的错误,但它如何影响输入机制?再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多