Advertisement
Mysoft

Rally Game In Batch

Aug 6th, 2020 (edited)
3,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.01 KB | None | 0 0
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set Debug=rem
  4.  
  5. ::===========================================================================================================
  6. ::===================================== Rally Batch Game v0.2.1 By Mysoft ===================================
  7. ::===========================================================================================================
  8.  
  9. ::se inicializado no programa separado vai para a função de teclado
  10. cd /d "%~dp0"
  11. if "%1"=="InitKeyboard" ( goto :InitKeyboard )
  12. if "%1"=="InitEngine" ( goto :InitGame )
  13. call :Inicializar %~nx0
  14. goto :eof
  15.  
  16. :InitGame
  17. echo Rally :D
  18. echo Mantenha se na pista...
  19. echo Use as teclas A,D ou 4,6 para movimentar o carro
  20. echo Pressione R para (Re)iniciar o jogo
  21. echo Pressione Q para sair do jogo.
  22.  
  23. :Mainloop
  24.   call :CheckKeys
  25.   call :Sync10
  26.   call :ProcessFrame
  27. if "%DoQuit%" == "" goto :Mainloop
  28. color
  29. goto :QuitGame
  30.  
  31. ::===================================================================================
  32. ::===================================== Funções =====================================
  33. ::===================================================================================
  34.  
  35. ::---------------------
  36. ::Inicialização do jogo
  37. ::---------------------
  38. :Inicializar
  39.  
  40.   ::tela
  41.   chcp 1252
  42.   mode 80,25
  43.   cls
  44.  
  45.   ::teclado
  46.   reg add "HKCU\Volatile Environment" /ve /t REG_SZ /f /d "" >nul
  47.   start /high /b %* InitKeyboard
  48.  
  49.   ::tempo
  50.   set OldSync=0
  51.  
  52.   ::jogo
  53.   set DoQuit=
  54.   set Space=                                                                     .
  55.   set KeyN=0
  56.   set DoOver=1
  57.  
  58.   echo . | %* InitEngine
  59.  
  60. goto :eof
  61.  
  62. ::-------------------
  63. ::Variaveis de começo de jogo
  64. ::-------------------
  65. :GameStart
  66.   cls
  67.   color 60
  68.   set fDistance=0
  69.   set iTrackPos=25
  70.   set iTrackMov=0
  71.   set DoOver=0
  72.   set /a iCarPos=%iTrackPos%+10
  73.   set sL=#
  74.   set sR=#
  75.   set sCar=V
  76. goto :eof
  77.  
  78. ::-------------------
  79. ::-------------------
  80. :ProcessFrame
  81.   if %DoOver%==1 goto :eof
  82.  
  83.   ::variaveis de posicao e comprimento da pista
  84.   set /a iDistance=%iDistance%+1
  85.   set /a iL=%iCarPos%-%iTrackPos%
  86.   set /a iR=20-%iL%
  87.  ::em caso de toque o jogo acaba
  88.   if %iL% LEQ 0 (set DoOver=1&&Set iL=0&&Set sL=)
  89.   if %iR% LEQ 0 (set DoOver=1&&Set iR=0&&Set sR=)
  90.   if %DoOver%==1 (set sCar=*)
  91.  ::strings que desenham a pista
  92.   set sPre=!!Space:~0,%iTrackPos%!!
  93.   set sTrk=%sL%!Space:~0,%iL%!%sCar%!Space:~0,%iR%!%sR%
  94.   echo %sPre%%sTrk%
  95.  ::movimento da pista
  96.   if %random% GTR 16384 goto :skip
  97.     set /a iTrackMov=(%random%%%3)-1
  98.   :skip
  99.   set /a iTrackPos=%iTrackPos%+%iTrackMov%
  100.   if %iTrackPos% LSS 0 (set iTrackPos=0)
  101.   if %iTrackPos% GTR 56 (set iTrackPos=56)
  102.  
  103.   ::fim de jogo.
  104.   if %DoOver%==1 (
  105.     set /a iDistA=!iDistance!/100
  106.     set /a iDistB=!iDistance!%%100
  107.     set /a iDistB=iDistB/10
  108.     color 40
  109.     call :Sync10
  110.     color C0
  111.     call :Sync10
  112.     color 40
  113.     call :Sync10
  114.     color 60
  115.     echo FIM DE JOGO.
  116.     echo Você percorreu !iDistA!.!iDistB!km
  117.     echo Pressione R para reiniciar ou Q para sair
  118.   )
  119. goto :eof
  120.  
  121. ::---------------------
  122. ::Eventos do teclado
  123. ::---------------------
  124. :CheckKeys
  125.   for /F "tokens=2 delims=^|" %%g in ('reg query "HKCU\Volatile Environment" /ve ^| find "REG_SZ"') do set "K=%%g"
  126.   if "%K%" EQU "" ( goto :eof )  
  127.   set KeyPos=-2
  128.   set ValidOpts=A4D6QR
  129.   :ckNextKey    
  130.     set /a KeyPos=(%KeyPos%+2)
  131.     set sKey=!!K:~%KeyPos%,2!!
  132.     if "%sKey%" EQU "" ( goto :eof )
  133.     if "%sKey:~0,1%" EQU " " ( goto :eof )
  134.     if "%sKey:~0,1%" NEQ "%KeyN%" ( goto :ckNextKey )
  135.     set /a KeyN=(%KeyN%+1)%%10
  136.     set sKey=%sKey:~1,1%
  137.     set sTemp=!ValidOpts:%sKey%=!
  138.     if %sTemp%==%ValidOpts% goto :ckAfterKey
  139.     call :ckKey%sKey%
  140.   :ckAfterKey
  141.   if %KeyPos% NEQ 16 ( goto :ckNextKey )
  142. goto :eof
  143.  
  144.   ::left
  145.   :ckKeyA
  146.   :ckKey4
  147.     set /a iCarPos=%iCarPos%-1
  148.     goto :eof
  149.  ::right
  150.   :ckKeyD
  151.   :ckKey6
  152.     set /a iCarPos=%iCarPos%+1
  153.     goto :eof
  154.  ::quit
  155.   :ckKeyQ
  156.     set DoQuit=1
  157.     goto :eof
  158.  ::start
  159.   :ckKeyR
  160.     call :GameStart
  161.     goto :eof
  162.  
  163. ::----------------------
  164. ::Aguarda próxima leva de 100ms (sincroniza para 10fps)
  165. ::----------------------
  166. :Sync10
  167.   set Sync=%time:~-2,1%
  168.   if %Sync%==%OldSync% goto :Sync10
  169.   set OldSync=%Sync%
  170. goto :eof
  171.  
  172. ::---------------------
  173. ::Esta função aguarda um caractere e o coloca no buffer e ela roda em um processo separado
  174. ::---------------------
  175. :InitKeyboard
  176.   %Debug% Inicializando o teclado.
  177.   set Num=0
  178.   set Out=
  179.   :ikNextKey
  180.     for /F "eol=0 delims=" %%i in ('xcopy /w "%~f0." ?') do set "K=^"%%i"
  181.     set Key=%K:~-1,1%
  182.     if "%Key%" EQU "" goto :ikNextKey
  183.     if "%Out%" NEQ "" (set Out=%Out:~-16,16%)
  184.     set Out=%Out%%Num%%Key%
  185.     set /a Num=((%Num%+1)%%10)
  186.     reg add "HKCU\Volatile Environment" /ve /t REG_SZ /f /d "^|%Out%" >nul
  187.     if "%Key%" EQU "Q" (goto :ikEndKey)
  188.     if "%Key%" EQU "q" (goto :ikEndKey)
  189.   goto :ikNextKey
  190.   :ikEndKey
  191.   %debug% Teclado finalizado.
  192. exit
  193.  
  194. ::Sai do programa
  195. :QuitGame
  196. color 07&color
  197.  
  198. :quit
  199.   reg delete "HKCU\Volatile Environment" /ve /f 2>&1 >nul
  200.   endlocal
  201.   echo Done...
  202. exit /b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement