FlyFar

Tetris In Pure Batch (Vista and Up)

Oct 21st, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.48 KB | None | 0 0
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3.  
  4. if "%~1" neq "" goto %1
  5.  
  6. title Tetris
  7.  
  8.  
  9. rem Delete the next line when Bitmap font 16x8 is used
  10. set "F16x8=REM"
  11.  
  12. cls
  13. echo/
  14. echo ===  Pure .BATch-file Tetris game by Aacini  ===
  15. echo/
  16. echo/
  17. echo Tetris pieces are controlled with these keys:
  18. echo/
  19. echo                                 rot.right
  20. echo rot.             rot.     move      I      move
  21. echo left ^<- A S D -^> right    left ^<- J K L -^> right
  22. echo           ^|                         ^|
  23. echo           v                         v
  24. echo       soft drop                 hard drop
  25. echo/
  26. echo/
  27. echo Press P to pause the game; press N to end game
  28. echo/
  29. echo/
  30. pause
  31. cls
  32.  
  33. rem Field dimensions
  34. set /A cols=10, lines=20
  35.  
  36. set /A col=cols+6, lin=lines+8
  37. %F16x8% set /A lin+=lines+2
  38. mode CON: cols=%col% lines=%lin%
  39. if %errorlevel% neq 0 (
  40.    echo Configuration error^^^!
  41.    echo You must select a font size that allows to set
  42.    echo a text window of %col% columns X %lin% lines
  43.    pause
  44.    goto :EOF
  45. )
  46.  
  47. chcp 850 > NUL
  48. cd . > pipeFile.txt
  49. "%~F0" Input >> pipeFile.txt  |  "%~F0" Main < pipeFile.txt
  50. ping localhost -n 2 > NUL
  51. del pipeFile.txt
  52. goto :EOF
  53.  
  54.  
  55.  
  56. :Input
  57. set "com[J]=Dx=-1"
  58. set "com[L]=Dx=1"
  59. set "com[K]=del=3"
  60. set "com[I]=R=-1"
  61. set "com[A]=R=1"
  62. set "com[D]=R=-1"
  63. set "com[S]=Dy=-1"
  64. set "com[Y]=Y"
  65. set "com[N]=N=1"
  66. set "com[P]=pause=1"
  67.  
  68. for /L %%# in () do (
  69.    set "key="
  70.    for /F "delims=" %%k in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%k"
  71.    for /F %%k in ("!key:~-1!") do (
  72.       echo(!com[%%k]!
  73.       if /I "%%k" equ "N" exit
  74.    )
  75. )
  76. rem exit
  77.  
  78.  
  79.  
  80. :Main
  81.  
  82. (
  83.    for /F "delims==" %%v in ('set') do set "%%v="
  84.    set /A cols=%cols%, lines=%lines%
  85.    set "F16x8=%F16x8%"
  86. )
  87.  
  88. rem Initialize the Field
  89. for /L %%i in (1,1,%cols%) do set "spc=!spc! "
  90. for /L %%i in (1,1,%lines%) do set "F%%i=  ³%spc%³"
  91. set /A top=lines+1
  92. set "F%top%=  Ú" & set "F0=  À"
  93. for /L %%i in (1,1,%cols%) do set "F%top%=!F%top%!Ä" & set "F0=!F0!Ä"
  94. set "F%top%=!F%top%!¿" & set "F0=%F0%Ù"
  95. set "F-1=  Level: 1" & set "Level=1"
  96. set "F-2=   Rows: 0" & set "Rows=0"
  97. set "F-3=  Score: 0" & set "Score=0"
  98. for /L %%i in (1,1,%cols%) do set "blk=!blk!Û"
  99. set /A top=lines+3, delay=50
  100. %F16x8% set /A linesP2=lines+2
  101.  
  102. rem Define all ":orientations:" of the O I S Z L J T pieces via "triplets":
  103. rem (offset Y . offset X . length X); one "triplet" for each horizontal line
  104. for %%t in ( "O:0.-1.2 -1.-1.2"
  105.              "I:0.-2.4:1.0.1 0.0.1 -1.0.1 -2.0.1"
  106.              "S:0.0.2 -1.-1.2:1.0.1 0.0.2 -1.1.1"
  107.              "Z:0.-1.2 -1.0.2:1.1.1 0.0.2 -1.0.1"
  108.              "L:0.-1.3 -1.-1.1:1.0.1 0.0.1 -1.0.2:1.1.1 0.-1.3:1.-1.2 0.0.1 -1.0.1"
  109.              "J:0.-1.3 -1.1.1:1.0.2 0.0.1 -1.0.1:1.-1.1 0.-1.3:1.0.1 0.0.1 -1.-1.2"
  110.              "T:0.-1.3 -1.0.1:1.0.1 0.0.2 -1.0.1:1.0.1 0.-1.3:1.0.1 0.-1.2 -1.0.1" ) do (
  111.    set "pc=%%~t"
  112.    set "i=-1"
  113.    for /F "delims=" %%p in (^"!pc::^=^
  114. % New line %
  115. !^") do (
  116.       if !i! lss 0 (set "pc=%%p") else set "!pc!!i!=%%p"
  117.       set /A i+=1
  118.    )
  119.    set "!pc!N=!i!"
  120. )
  121. set "pcs=OISZLJT"
  122.  
  123. set "init=1"
  124. for /L %%# in () do (
  125.  
  126.    if defined init (
  127.       setlocal EnableDelayedExpansion
  128.       set "init="
  129.  
  130.       rem Create the first "previous" piece
  131.       for /L %%i in (0,1,!time:~-1!) do set /A p=!random!%%7
  132.       for %%p in (!p!) do set "p2=!pcs:~%%p,1!"
  133.       for %%p in (!p2!) do set "p3=!%%p0!" & set "p4=!%%pN!"
  134.  
  135.       set "new=1"
  136.    )
  137.  
  138.    if defined new (
  139.       set "new="
  140.  
  141.       rem Take the "previous" piece as current one
  142.       set "pc=!p2!" & set "p0=!p3!" & set "pN=!p4!"
  143.  
  144.       rem Create a new "previous" piece
  145.       for /L %%i in (1,1,2) do (
  146.          set /A p=!random!*7/32768
  147.          for %%p in (!p!) do (
  148.             set "p=!pcs:~%%p,1!"
  149.             if !p! neq !pc! set "p2=!p!"
  150.          )
  151.       )
  152.       for %%p in (!p2!) do set "p3=!%%p0!" & set "p4=!%%pN!"
  153.  
  154.       rem Insert the new "previous" piece in its place, above Field
  155.       set /A x=3+cols/2, y=top, yp=top-1
  156.       set "F!yp!=   %spc%"
  157.       for %%p in (!p3!) do (
  158.          for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  159.             set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  160.             for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  161.                set "F%%a=!spc:~0,%%b!!blk:~0,%%k!!spc:~%%c!"
  162.             )
  163.          )
  164.       )
  165.  
  166.       rem Try to insert the new current piece in the Field...
  167.       set /A x=3+cols/2, y=lines,   b=1
  168.       for %%p in (!p0!) do (
  169.          for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  170.             set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  171.             for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  172.                if "!F%%a:~%%b,%%k!" neq "!spc:~0,%%k!" set     "b="
  173.                set "F%%a=!F%%a:~0,%%b!!blk:~0,%%k!!F%%a:~%%c!"
  174.             )
  175.          )
  176.       )
  177.       cls
  178.       for /L %%i in (%top%,-1,-3) do (
  179.          echo(!F%%i!
  180. %F16x8%  if %%i geq %linesP2% echo(!F%%i!
  181. %F16x8%  if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  182.       )
  183.  
  184.       rem ... if that was not possible:
  185.       if not defined b call :endGame & endlocal
  186.  
  187.       set "p1=!p0!"
  188.       set /A "pI=0, del=delay, b=1!time:~-2!"
  189.  
  190.    )
  191.  
  192.    rem Control module: move the piece as requested via a key, or down one row each %del% centiseconds
  193.    set "move="
  194.    set /A "Dy=Dx=0"
  195.    set /P "com="
  196.    if defined com (
  197.       set /A "!com!, move=1"
  198.       set "com="
  199.       if defined N exit
  200.       if defined pause call :Pause & set "move="
  201.       set "b=1!time:~-2!"
  202.    ) else (
  203.       set /A "e=1!time:~-2!, elap=e-b, elap-=(elap>>31)*100"
  204.       if !elap! geq !del! set /A b=e, Dy=move=-1
  205.    )
  206.  
  207.    if defined move (
  208.  
  209.       rem Delete the piece from its current position, and store current coordinates
  210.       set i=0
  211.       for %%p in (!p0!) do for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  212.          set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  213.          for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  214.             set "F%%a=!F%%a:~0,%%b!!spc:~0,%%k!!F%%a:~%%c!"
  215.             set /A i+=1
  216.             set "c!i!=%%a %%b %%c %%k"
  217.          )
  218.       )
  219.  
  220.       rem If move is Rotate: get rotated piece
  221.       if defined R (
  222.          set /A "p=(pI+R+pN)%%pN"
  223.          for /F "tokens=1,2" %%i in ("!pc! !p!") do set "p1=!%%i%%j!"
  224.       )
  225.  
  226.       rem Test if the piece can be placed at the new position, and store new coordinates
  227.       set j=0
  228.       for %%p in (!p1!) do if defined move (
  229.          for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  230.             set /A yp=y+%%i+Dy, xp=x+%%j+Dx, xL=xp+%%k
  231.             for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  232.                if "!F%%a:~%%b,%%k!" equ "!spc:~0,%%k!" (
  233.                   set /A j+=1
  234.                   set "n!j!=%%a %%b %%c %%k"
  235.                ) else (
  236.                   set "move="
  237.                )
  238.             )
  239.          )
  240.       )
  241.  
  242.       if defined move (
  243.  
  244.          rem Place the piece at the new position
  245.          for /L %%j in (1,1,!j!) do (
  246.             for /F "tokens=1-4" %%a in ("!n%%j!") do (
  247.                set "F%%a=!F%%a:~0,%%b!!blk:~0,%%d!!F%%a:~%%c!"
  248.             )
  249.          )
  250.  
  251.          rem Update the Field in screen
  252.          cls
  253.          for /L %%i in (%top%,-1,-3) do (
  254.             echo(!F%%i!
  255. %F16x8%     if %%i geq %linesP2% echo(!F%%i!
  256. %F16x8%     if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  257.          )
  258.  
  259.          rem Update any changes in the piece
  260.          set /A y+=Dy, x+=Dx
  261.          if defined R set "p0=!p1!" & set "pI=!p!" & set "R="
  262.  
  263.       ) else (   rem The piece can not be moved
  264.  
  265.          rem Recover the piece at its current position
  266.          for /L %%i in (1,1,!i!) do (
  267.             for /F "tokens=1-4" %%a in ("!c%%i!") do (
  268.                set "F%%a=!F%%a:~0,%%b!!blk:~0,%%d!!F%%a:~%%c!"
  269.             )
  270.          )
  271.          if defined R set "p1=!p0!" & set "R="
  272.  
  273.          if !Dy! neq 0 (   rem The piece "lands"
  274.  
  275.             rem Count completed lines
  276.             set "j=0"
  277.             for /L %%i in (1,1,!i!) do for /F %%a in ("!c%%i!") do (
  278.                if "!F%%a:~3,%cols%!" equ "%blk%" (
  279.                   set "F%%a=  ³%spc: ==%³"
  280.                   set /A j+=1
  281.                )
  282.             )
  283.  
  284.             if !j! neq 0 (
  285.               rem Update scores (See N-Blox at http://www.tetrisfriends.com/help/tips_appendix.php#rankingsystem)
  286.                set /A "xp=Level*(40+((j-2>>31)+1)*60+((j-3>>31)+1)*200+((j-4>>31)+1)*900), Score+=xp, Rows+=j, xL=Level, Level=(Rows-1)/10+1"
  287.                set "F-2=!F-2:~0,8!+!j!     "
  288.                set "xp=!xp!     "
  289.                set "F-3=!F-3:~0,8!+!xp:~0,6!"
  290.                echo  BEL Ctrl-G Ascii-7
  291.                cls
  292.                for /L %%i in (%top%,-1,-3) do (
  293.                   echo(!F%%i!
  294. %F16x8%           if %%i geq %linesP2% echo(!F%%i!
  295. %F16x8%           if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  296.                )
  297.                set "F-1=!F-1:~0,8! !Level!"
  298.                set "F-2=!F-2:~0,8! !Rows!"
  299.                set "F-3=!F-3:~0,8! !Score!"
  300.                if !Level! neq !xL! if !delay! gtr 5 set /A delay-=5
  301.  
  302.                rem Remove completed lines
  303.                set "i=1"
  304.                for /L %%i in (1,1,%lines%) do (
  305.                   set "F!i!=!F%%i!"
  306.                   if "!F%%i:~3,1!" neq "=" set /A i+=1
  307.                )
  308.                for /L %%i in (!i!,1,%lines%) do set "F%%i=  ³%spc%³"
  309.                call :Delay 95
  310.                cls
  311.                for /L %%i in (%top%,-1,-3) do (
  312.                   echo(!F%%i!
  313. %F16x8%           if %%i geq %linesP2% echo(!F%%i!
  314. %F16x8%           if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  315.                )
  316.             )
  317.  
  318.             rem Request to show a new piece
  319.             set "new=1"
  320.  
  321.          )
  322.  
  323.       )
  324.  
  325.    )
  326.  
  327. )
  328.  
  329. :endGame
  330. set /P "=Play again? " < NUL
  331. :choice
  332.    set /P "com="
  333. if not defined com goto choice
  334. if /I "%com%" equ "Y" exit /B
  335. if /I "%com:~0,1%" neq "N" set "com=" & goto choice
  336. echo N
  337. exit
  338.  
  339.  
  340. :Pause
  341. set "pause=!F%lines%!"
  342. set "F%lines%=  ³%spc:          =  PAUSED  %³"
  343. cls & for /L %%i in (%top%,-1,-3) do (
  344.          echo(!F%%i!
  345. %F16x8%  if %%i geq %linesP2% echo(!F%%i!
  346. %F16x8%  if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  347.       )
  348. :wait
  349.    set /P "com="
  350. if not defined com goto wait
  351. set "com="
  352. set "F%lines%=%pause%"
  353. cls & for /L %%i in (%top%,-1,-3) do (
  354.          echo(!F%%i!
  355. %F16x8%  if %%i geq %linesP2% echo(!F%%i!
  356. %F16x8%  if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  357.       )
  358. set "pause="
  359. exit /B
  360.  
  361.  
  362. :Delay centisecs
  363. set "b=1%time:~-2%"
  364. :wait2
  365.    set /A "e=1%time:~-2%, elap=e-b, elap-=(elap>>31)*100"
  366. if %elap% lss %1 goto wait2
  367. set "b=1%time:~-2%"
  368. exit /B
Add Comment
Please, Sign In to add comment