蝙蝠游戏。使用 set /p 添加计时器

发布于 2024-12-09 06:24:07 字数 216 浏览 1 评论 0原文

我正在制作一个 .bat 游戏,目前在输入命令时,代码是

set /p command=

我想知道的是是否可以以某种方式限制输入命令的时间。例如,如果你与守卫战斗,并且你在 5 秒内没有发出命令,守卫就会攻击。

这不是迫切需要的东西,我只是想更多地了解我所受到的限制,而且我想我无论如何都知道答案(答案是你不能)

谢谢

Im making a .bat game, and currently when putting in a command the code is

set /p command=

What i want to know is if you can somehow have a time limit for inputting the commands. For example if your fighting a guard, and you haven't put a command in for say 5 seconds, the guard attacks.

This is not something of dire need, I am just wondering more about the limitations im bound to, and i think i know the anawer anyway (the answer being that you cant)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离去的眼神 2024-12-16 06:24:07

也可以仅通过批处理来完成。

您可以使用 start /b 创建第二个线程(在同一窗口中)。
如果此线程使用 set /p 等待用户输入,则主线程不受影响。
此示例将等待 5 秒用户输入,如果用户输入文本,则会将其移动到文件中,以便第一个线程可以访问它。

@echo off
setlocal EnableDelayedExpansion
if "%1" NEQ "" goto %1

del enter.tmp 2>nul >nul
start /b cmd /c %0 :secondThread

:FirstThread
set n=0
echo Enter Text (5 seconds timeout):

:loop
set /a n+=1
ping -n 2 localhost > nul
if !n! LSS 5 (
    if not exist entER.tmp goto :loop
    < Enter.tmp (
        set /p input=
    )
    echo !input!
) ELSE (
    echo Timeout for input
)

exit /b

:secondThread
set /p var=
> enter.tmp echo !var!
exit /b

It can also be done with batch only.

You can create a second thread (in the same window) with start /b.
If this thread wait with set /p for user input, the main thread is not affected.
This sample will wait for 5 seconds for userinput, if the user inputs text it is moved into a file, so the first thread can access it.

@echo off
setlocal EnableDelayedExpansion
if "%1" NEQ "" goto %1

del enter.tmp 2>nul >nul
start /b cmd /c %0 :secondThread

:FirstThread
set n=0
echo Enter Text (5 seconds timeout):

:loop
set /a n+=1
ping -n 2 localhost > nul
if !n! LSS 5 (
    if not exist entER.tmp goto :loop
    < Enter.tmp (
        set /p input=
    )
    echo !input!
) ELSE (
    echo Timeout for input
)

exit /b

:secondThread
set /p var=
> enter.tmp echo !var!
exit /b
酷遇一生 2024-12-16 06:24:07

可以将批处理文件与其他文件混合使用,例如 c#。由于现在几乎所有 Windows 电脑上都安装了 .net,所以这应该不是什么大问题。

在下面的示例中,有 3 秒的延迟,用户可以在其中输入一些内容。如果未输入任何内容,程序将继续,但 %result% 将为空。

/* 2>NUL
@echo off 
REM cls
set WinDirNet=%WinDir%\Microsoft.NET\Framework
IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
%csc% /nologo /out:"%~0.exe" %0



echo enter some text:

set result=
for /F "tokens=*" %%a in ('"%~0.exe"') do set result=%%a

echo you have entered:%result%




del "%~0.exe"
goto :eof
*/
using System;
using System.Text;
using System.IO;
using System.Threading;

class Program
{
    static void Main (string[] args)
    {
        byte[] buffer=new byte[80];

        using (Stream s = Console.OpenStandardInput ()) {
            ManualResetEvent e=new ManualResetEvent(false);
            s.BeginRead (buffer, 0, buffer.Length, x => e.Set(), null);

            e.WaitOne (3000);
        }

        Console.WriteLine (Encoding.UTF8.GetString (buffer));
    }
}

通过这种方式,您可以对批处理文件进行编程,并使用 C# 来完成批处理文件中不可能完成的所有操作。请注意,此代码可以进行多项改进。

请参阅如何向 Console.ReadLine() 添加超时? 用于改进 C# 代码。

(嵌入式c#的来源代码)

It is possible to mix a batch file with something else, for example c#. As .net is installed on almost all windows pc nowadays, that should not be a big problem.

In the example below there is a 3 second delay where the user can enter some input. If nothing is entered, the program continues, but %result% will be empty.

/* 2>NUL
@echo off 
REM cls
set WinDirNet=%WinDir%\Microsoft.NET\Framework
IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
%csc% /nologo /out:"%~0.exe" %0



echo enter some text:

set result=
for /F "tokens=*" %%a in ('"%~0.exe"') do set result=%%a

echo you have entered:%result%




del "%~0.exe"
goto :eof
*/
using System;
using System.Text;
using System.IO;
using System.Threading;

class Program
{
    static void Main (string[] args)
    {
        byte[] buffer=new byte[80];

        using (Stream s = Console.OpenStandardInput ()) {
            ManualResetEvent e=new ManualResetEvent(false);
            s.BeginRead (buffer, 0, buffer.Length, x => e.Set(), null);

            e.WaitOne (3000);
        }

        Console.WriteLine (Encoding.UTF8.GetString (buffer));
    }
}

This way you can program your batch file, and use c# for everything what is not possible in batch files. Note there are several improvements possible to this code.

See How to add a Timeout to Console.ReadLine()? for improvements of the c# code.

(Source of embedded c# code)

多彩岁月 2024-12-16 06:24:07

如果命令有唯一的起始字母,也许您可​​以考虑使用 CHOICE 命令? (在 Windows 7 中再次可用)

If commands have a unique starting letter, maybe you can consider using the CHOICE command? (Available again in Windows 7)

完美的未来在梦里 2024-12-16 06:24:07

批处理文件的功能可以借助辅助程序来增强,如果用汇编语言编写,其中一些可能会非常简单:

@ECHO OFF
(
ECHO A100
ECHO MOV AH,B
ECHO INT 21
ECHO MOV AH,4C
ECHO INT 21
ECHO/
ECHO RCX
ECHO 8
ECHO W
ECHO Q
) | DEBUG CHKKEY.COM

上一个批处理文件创建 8 字节长的 CHKKEY.COM 辅助程序,用于检查是否按下了某个键并如果是,则返回 ERRORLEVEL 255;如果不是,则返回 0。例如:

:waitforkey
echo Waiting for a key to be pressed...
chkkey
if not errorlevel 1 goto waitforkey
echo A key was pressed!

如果您没有 DEBUG.COM 程序,您可以从网络上获取它。这样,等待一个按键5秒:

for /F "tokens=3 delims=:." %%a in ("%time%") do set /A second=%%c+5
if %second% geq 60 set /A second-=60
:waitforkey
for /F "tokens=3 delims=:." %%a in ("%time%") do if %%c == %second% goto timeexceeded
chkkey
if not errorlevel 1 goto waitforkey
set /P command=

如果在MOV AH,B指令中将B值更改1,则读取一个按键,并在ERRORLEVEL中返回其ASCII码;此功能允许读取单个击键并立即处理它。如果值为8,则读取到的按键不显示在屏幕上;这允许处理键盘的任何键,甚至是返回两个值的功能和特殊键:第一个值为零(标识特殊键),第二个值标识按下的键。例如,F1 键返回 0 和 59。

Batch file capabilities may be increased with the aid of auxiliary programs, some of wich may be very simple if they are written in assembly language:

@ECHO OFF
(
ECHO A100
ECHO MOV AH,B
ECHO INT 21
ECHO MOV AH,4C
ECHO INT 21
ECHO/
ECHO RCX
ECHO 8
ECHO W
ECHO Q
) | DEBUG CHKKEY.COM

Previous Batch file creates the 8-bytes long CHKKEY.COM auxiliary program that check if a key was pressed and return an ERRORLEVEL of 255 if so, or zero if not. For example:

:waitforkey
echo Waiting for a key to be pressed...
chkkey
if not errorlevel 1 goto waitforkey
echo A key was pressed!

If you have not the DEBUG.COM program, you may get it in the web. This way, to wait for a key for 5 seconds:

for /F "tokens=3 delims=:." %%a in ("%time%") do set /A second=%%c+5
if %second% geq 60 set /A second-=60
:waitforkey
for /F "tokens=3 delims=:." %%a in ("%time%") do if %%c == %second% goto timeexceeded
chkkey
if not errorlevel 1 goto waitforkey
set /P command=

If you change the B value by 1 in MOV AH,B instruction, a key is read and its ASCII code is returned in ERRORLEVEL; this feature allows to read a single keystroke and process it immediately. If the value is 8, the key read is not displayed in the screen; this allows to process any key of the keyboard even function and special keys that return two values: the first one is zero (that identify a special key) and the second one identify the key pressed. For example, F1 key returns 0 and 59.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文