蝙蝠游戏。使用 set /p 添加计时器
我正在制作一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也可以仅通过批处理来完成。
您可以使用
start /b
创建第二个线程(在同一窗口中)。如果此线程使用
set /p
等待用户输入,则主线程不受影响。此示例将等待 5 秒用户输入,如果用户输入文本,则会将其移动到文件中,以便第一个线程可以访问它。
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.
可以将批处理文件与其他文件混合使用,例如 c#。由于现在几乎所有 Windows 电脑上都安装了 .net,所以这应该不是什么大问题。
在下面的示例中,有 3 秒的延迟,用户可以在其中输入一些内容。如果未输入任何内容,程序将继续,但
%result%
将为空。通过这种方式,您可以对批处理文件进行编程,并使用 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.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)
如果命令有唯一的起始字母,也许您可以考虑使用 CHOICE 命令? (在 Windows 7 中再次可用)
If commands have a unique starting letter, maybe you can consider using the CHOICE command? (Available again in Windows 7)
批处理文件的功能可以借助辅助程序来增强,如果用汇编语言编写,其中一些可能会非常简单:
上一个批处理文件创建 8 字节长的 CHKKEY.COM 辅助程序,用于检查是否按下了某个键并如果是,则返回 ERRORLEVEL 255;如果不是,则返回 0。例如:
如果您没有 DEBUG.COM 程序,您可以从网络上获取它。这样,等待一个按键5秒:
如果在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:
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:
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:
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.