Search code examples
batch-filetimersystempoints

Typing challenge in 5 seconds in game


I am doing an RPG game in batch file and I want to make a challenge for user to type word like ‚butter’ in 5 seconds. If he’s not going to do this he will get nothing and go back. But if he is going to type correctly he will gain money. And I want it to show timer. Can you guys help? Could it be done with the PING command?


Solution

  • Counting your effort, you don't deserve it.
    Just to give you a starting point,
    batch isn't good in measuring/handling/calculating time.

    :: Q:\Test\2018\05\31\typingtest.cmd
    @Echo off&SetLocal EnableExtensions EnableDelayedExpansion
    :loop
    cls
    Echo enter butter
    choice /n /C b >NUL
    set start=%time%
    choice /n /C u >NUL
    choice /n /C t >NUL
    choice /n /C t >NUL
    choice /n /C e >NUL
    choice /n /C r >NUL
    set ready=%time%
    Echo start:%start%
    Echo ready:%ready%
    

    Sample output:

    enter butter
    start:21:00:50,50
    ready:21:00:51,56
    

    Despite my initial comment, the following version is more universal and calculates seconds correctly provided your %time% retuns 24hr format and hundreds seconds.

    :: Q:\Test\2018\05\31\typingtest.cmd
    @Echo off&SetLocal EnableExtensions EnableDelayedExpansion
    
    cls
    Echo enter butter
    Set "Start="
    For %%A in (b u t t e r) do (
        choice /n /C %%A >NUL
        if not defined Start Set "Start=%time%"
    )
    Set "Ready=%time%"
    Call :CalcTime Start
    Call :CalcTime Ready
    Set /A "Lapse=Ready-Start"
    Echo Secs :        %Lapse:~0,-2%,%Lapse:~-2%
    Pause
    Goto :Eof
    
    :CalcTime
    Echo=%1: !%1!
    For /f "tokens=1-4delims=:.," %%H in ("!%1!"
      ) Do Set /A "%1=(1%%H-100)*360000+(1%%I-100)*6000+(1%%J-100)*100+%%K"
    

    Sample output:

    enter butter
    Start: 16:25:43,42
    Ready: 16:25:46,94
    Secs :        3,52