2

Is there any way you can delay the choice command (in batch) in under a second?

for example if i want to make a game where you can move around, and you have some enemies you have to fight, the enemies will not move that fast (they'll just move each second!).

here's what i have tried:

choice /c 123 /n /t 0.2 /d c

as you can see i want to delay it for about 0.2 seconds.

i have also tried to use 0, but when i do that the 1, 2 and 3 button don't work...

tell me if i need to be more specific!

thanks for all help :D

dan1st
  • 12,568
  • 8
  • 34
  • 67

1 Answers1

1

No, the native CHOICE command does not support a timeout value less than 1 second. Even if it did, I don't think it would help you.

Based on the information in your question, it sounds like you are attempting to develop a graphical game that works in real time (not turn based). I'm assuming you want the game to continuously update the screen with moving objects, and respond in some fashion when the user presses certain keys.

The CHOICE command will only recognize key presses that occur while it is waiting - In other words, the keypresses are not buffered. Presumbably your batch script is in a loop that gets user input, does a series of computations, and then refreshes the screen. It takes time to do the computations and refresh the screen. Some keypresses will work because they happen to occur while CHOICE is excecuting, and some will not. This will likely be very frustrating for the user.

(Note that input from a pipe or redirected input is buffered, but not direct key presses)

I believe you want all of the following to develop a graphical game: You want some mechanism to buffer key presses, and within a batch loop, test and take action if a key press is detected. You want the test for a key press to be "instantaneous" (non-blocking). And you also want a mechanism to control the speed of the game by introducing a sub-second sleep. There are many other issues to solve. All of this is possible using batch, but it is extremely difficult, requiring lots of undocumented knowledge about how batch scripts work, and lots of arcane syntax and hacks.

There are third party tools that can make the job easier, but what is the point?

Note - The following is my opinion, and StackOverflow frowns upon opinion based answers. But, here goes...

If you simply want to develop a game, and/or teach yourself some programming skills, then batch is a terrible place to start. You would be much better off using just about any other language you can find. Use anything other than batch, and you will end up with a far superiour product with much less effort, and learn more useful skills while you're at it.

But if you want to develop a game using batch purely for the challenge and fun of it, then I like your way of thinking, and more power to you :-) However, if this is your goal, then I recommend not using any third party tools, otherwise it is cheating in my book.

End of opinion

If you are sure that you really want to develop a game using batch, then I highly recommend you study my SNAKE.BAT game. Read the entire thread, and study the code. But I warn you that there are lots of advanced techniques and hacks that will be difficult to understand. It will likely take multiple readings and significant time to understand everything.

One of the things I solved was how to detect key presses in an "instantaneous", non-blocking way. I described the technique at http://www.dostips.com/forum/viewtopic.php?p=31035#p31035.

dbenham
  • 127,446
  • 28
  • 251
  • 390