2015-10-16 78 views
0

所以我想創建一個基於回合的RPG(批量)的打字文本,你有一個人控制幾乎所有與玩家交互的東西。 我想要這樣,當每個人輪到時,它會顯示每個人都做了什麼,但以一種戲劇性的方式。 現在有些人都知道鍵入文字尚未(但將)批量定義變量

for %%i in (h e l l o) do (set /p a=%%i<nul & ping 0.0.0.0 -n 2.5>nul 2>&1)

,但我想有一個變量做到這一點使用 set /p Write=<Write.txt 這樣我就可以有這樣的事for %%i in (%write%) do (set /p a=%%i<nul & ping 0.0.0.0 -n 2.5>nul 2>&1) 使它寫在一個時間一個字母。

謝謝!

+2

在完全不相關的說明中,您只能使用整數作爲ping的-n選項,因爲它是運行ping的次數,而不是暫停多少秒。 – SomethingDark

+0

@SomethingDark是的,我想我已經知道了,但它只是從另一篇文章中,似乎延遲了,所以我認爲它是合法的。 – face5054

回答

1

下面的批處理文件中使用了一個有趣的特技我從this post借用該轉換ASCII(1字節)字符轉換成Unicode 2個字節的字符通過的cmd.exe/U開關(插入之間的零字節

@echo off 
setlocal 

echo Hello, World!> Write.txt 

for /F %%a in ('echo prompt $H^| cmd') do set "BS=%%a" 

for /F "delims=" %%i in ('cmd /D /U /C type Write.txt ^| find /V ""') do (
    set /P "=X%BS%%%i" < NUL 
    ping localhost -n 2 > NUL 
) 

EDIT新版本添加

我修改字符),然後通過查找命令分割到每個線路的零字節以前的代碼以正確的方式顯示幾行代碼;它也使用JScript的睡眠方法,以便在每個字符之間使用可變延遲間隔,這一點導致輸出以更戲劇性的方式出現。

@if (@CodeSection == @Batch) @then 


@echo off 

(
echo Hello, World! 
echo The World is ending! 
) > Write.txt 

for /F %%a in ('echo prompt $H^| cmd') do set "BS=%%a" 

for /F "delims=" %%a in (Write.txt) do (
    for /F "delims=" %%i in ('cmd /D /U /C set /P "=%%a"^<NUL ^| find /V ""') do (
     set /P "=X%BS%%%i" < NUL 
     REM ping localhost -n 3 > NUL 
     cscript //nologo //E:JScript "%~F0" 
    ) 
    echo/ 
) 
goto :EOF 


@end 


// JScript section: wait a random number of milliseconds between 0 and 300 
WScript.Sleep(300*Math.random()); 
+0

您的代碼是否可以更改,以便如果文本文件有多行,它將輸出到屏幕上的多行。當我把兩行輸入到一個文件時,它輸出爲一個文件。你好,世界!世界正在結束! – Squashman

+0

@Squashman:完成! **'' – Aacini

+0

我是一個新手,在編碼的這個角落,但是爲什麼,如果你把代碼放在@if(@CodeSection == @Batch)@之上,那麼'是'(1,6 )Microsoft JScript編譯錯誤:預期';''? – face5054

0

要通過一個任意字符串的一個打印字符中的一個,你可以使用以下命令:

@echo off 

rem define string to print: 
set "WRITE=Hello world!" 

setlocal EnableDelayedExpansion 

for /L %%I in (0,1,8191) do (
    set "CHAR=!WRITE:~%%I,1!" 
    rem if "!WRITE:~%%I,2!"=="!CHAR! " set "CHAR=!CHAR! " 
    if "!CHAR!"=="" goto :SKIP 
    < nul set /P "DUMMY=!CHAR!" 
    > nul 2>&1 ping 127.0.0.1 -n 2 
) 
:SKIP 

endlocal 

正如你會發現,在SPACEWRITE字符串內不打印,因爲set /P做不要在提示字符串中打印任何(前導)空格。要解決此問題,請在for循環內刪除if前面的rem。實際上,這會預覽下一個字符,並將其附加到當前字符以防空格,因爲set /P會打印尾部空格。

0
@echo off 
setlocal enabledelayedexpansion 

for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A 

set /P string=<write.txt 
call :StrLen "%string%" _len1 

for /L %%A in (0,1,%_len1%) do (
    set "substring=!string:~%%A,1!" 
    set /p "=.%BS%!substring!" <nul 
    > nul 2>&1 ping 127.0.0.1 -n 1 
) 
echo. 
pause 
GOTO :EOF 

REM Subroutine to return the length of a string variable 
:StrLen [string] [len-var] 
    set "_str=A%~1" & set "_len=0" 
    for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A") 
    set /a %~2=%_len% 
    exit /b 
0

這是一個Ghost typer的版本,它使用變量來顯示屏幕上的文本,以便用戶輸入文本。

@echo off 
:: Ghost typer - variable version 

:: shows text in a variable on the screen in a way that a person might type it. 
:: Do not use " in the text to be printed, replace them with ' 

:: Adjust speed variable - higher numbers increase typing speed - default=3 
set speed=6 

:: backspace variable is used in the printing routine 
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a" 

set "line=Your dog was eaten by a red winged dragon!" 
call :begin 
set "line=Two sloths ate all your chocolate!" 
call :begin 
set "line=A genie appears, and grants you 4 wishes...." 
call :begin 
set "line=How many woodchucks does it take?" 
call :begin 


pause>nul 
goto :EOF 

:begin 
set num=0 
:type 
call set "letter=%%line:~%num%,1%%" 
set "delay=%random%%random%%random%%random%%random%%random%%random%" 
set "delay=%delay:~-6%" 
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul 

for /L %%b in (1,%speed%,%delay%) do rem 
if "%letter%"=="" echo.&goto :EOF 
set /a num+=1 
goto :type