T3RRYT3RR0R

Batch Tetris

Oct 31st, 2020 (edited)
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.65 KB | None | 0 0
  1. ::: NEW VERSION including getkeys.exe for enhanced controls and gameplay
  2. ::: available for download from:
  3. ::: https://drive.google.com/file/d/1iLm9RsbWIfIBDEU854IQbvg_mjA3RnvC/view?usp=sharing
  4.  
  5.  
  6. @Echo off
  7.  Setlocal EnableExtensions DISABLEdelayedexpansion
  8.  wmic OS get OSArchitecture,caption | FIND "10" >nul || (ECHO/Windows 10 required for ascii escape codes & Endlocal & Exit /B)
  9. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  10.  Title Tetris by T3RRY & rem commenced 30/10/2020
  11. rem ::: 30/10/2020 Piece creation, basic collision detection, piece movement implemented.
  12. rem ::: 31/10/2020 New piece and Game over trigger tests added.
  13. rem ::: 31/10/2020 Color, Piece rotation added
  14. rem ::: 01/11/2020 Function to test for a completed line added.
  15. rem ::: 01/11/2020  Scoring system added
  16. :::::::: [ TO BE ACTIONED ] :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  17. rem ::: Getkeys controller version with variable delay for speed increase based on score
  18. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  19. rem ---------------------------------------------------- /* ASCII escape code definition */
  20.  for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "\E=%%a"
  21. rem ------------------------------------------------------------------ /* define macro's */
  22.  Set "nPiece[1]=Set /A "x1=6,y1=1,x2=6,y2=2,x3=6,y3=3,x4=6,y4=4""
  23.  Set "nPiece[2]=Set /A "x1=6,y1=1,x2=7,y2=1,x3=6,y3=2,x4=7,y4=2""
  24.  Set "nPiece[3]=Set /A "x1=6,y1=1,x2=7,y2=1,x3=7,y3=2,x4=7,y4=3""
  25.  Set "nPiece[4]=Set /A "x1=6,y1=1,x2=6,y2=2,x3=6,y3=3,x4=7,y4=1""
  26.  Set "nPiece[5]=Set /A "x1=6,y1=1,x2=6,y2=2,x3=7,y3=2,x4=7,y4=3""
  27.  Set "nPiece[6]=Set /A "x1=7,y1=1,x2=7,y2=2,x3=6,y3=2,x4=6,y4=3""
  28.  Set "nPiece[7]=Set /A "x1=6,y1=1,x2=6,y2=2,x3=7,y3=2,x4=6,y4=3""
  29.  Set "MoveLeft=Set "valid=0"&Set /A "_x1=!x1!-1,_x2=!x2!-1,_x3=!x3!-1,_x4=!x4!-1" & (For %%v in ("!y1!;!_x1!" "!y2!;!_x2!" "!y3!;!_x3!" "!y4!;!_x4!")Do (IF "!}%%~v!" == "" (Set /A "Valid=!Valid! + 1"))) & If "!Valid!" == "4" (Set /A "x1-=1,x2-=1,x3-=1,x4-=1")"
  30.  Set "MoveRight=Set "valid=0"&Set /A "_x1=!x1!+1,_x2=!x2!+1,_x3=!x3!+1,_x4=!x4!+1" & (For %%v in ("!y1!;!_x1!" "!y2!;!_x2!" "!y3!;!_x3!" "!y4!;!_x4!")Do (IF "!}%%~v!" == "" (Set /A "Valid=!Valid! + 1"))) & If "!Valid!" == "4" (Set /A "x1+=1,x2+=1,x3+=1,x4+=1")"
  31.  Set "MoveDown=Set "valid=0"&Set /A "_y1=!y1!+1,_y2=!y2!+1,_y3=!y3!+1,_y4=!y4!+1" & (For %%v in ("!_y1!;!x1!" "!_y2!;!x2!" "!_y3!;!x3!" "!_y4!;!x4!")Do (IF "!}%%~v!" == "" (Set /A "Valid=!Valid! + 1"))) & (If "!Valid!" == "4" (Set /A "y1+=1,y2+=1,y3+=1,y4+=1")Else (Set /A "Score+=10"&Set "}!y1!;!x1!=%\E%[!y1!;!x1!H%\E%[3!pce!mX%\E%[0m"& Set "}!y2!;!x2!=%\E%[!y2!;!x2!H%\E%[3!pce!mX%\E%[0m"& Set "}!y3!;!x3!=%\E%[!y3!;!x3!H%\E%[3!pce!mX%\E%[0m"& Set "}!y4!;!x4!=%\E%[!y4!;!x4!H%\E%[3!pce!mX%\E%[0m"& >"%TEMP%\tetris.~tmp" (For /F "Tokens=2* Delims==" %%G in ('Set }')Do Echo/%%G) & If "!y1!" == "1" (Goto :Gameover)Else ( Call :isMatch & Goto :newpiece )))"
  32.  Set "ShowPiece=<Nul Set /P "=%\E%[3!pce!m%\E%[!y1!;!x1!HX%\E%[!y2!;!x2!HX%\E%[!y3!;!x3!HX%\E%[!y4!;!x4!HX%\E%[0m""
  33.  Set "HideLast=<Nul Set /P "=%\E%[!ly1!;!lx1!H %\E%[!ly2!;!lx2!H %\E%[!ly3!;!lx3!H %\E%[!ly4!;!lx4!H %\E%[0m""
  34. rem ---------------------------------------------------------- /* Define Control Display */
  35.  Set "}1;20=%\E%[1;20H%\E%[90mScore: !Score!"
  36.  Set "}3;20=%\E%[3;20H%\E%[33mControls:"
  37.  Set "}4;20=%\E%[4;20HA - move Left"
  38.  Set "}5;20=%\E%[5;20HS - move Down"
  39.  Set "}6;20=%\E%[6;20HD - move Right"
  40.  Set "}7;20=%\E%[7;20HR - Rotate"
  41.  Set "}8;20=%\E%[8;20HV - Drop Piece"
  42.  Set "}9;20=%\E%[9;20H%\E%[32mE - Exit%\E%[0m"
  43. rem -------------------- /* Establish environment for macro usage and code block actions */
  44.  Setlocal EnableDelayedExpansion
  45.  mode 60,42
  46. rem ----------------------------------------------------------------- /* hide the cursor */
  47.  <nul Set /P "=%\E%[?25l"
  48. rem --------------------------------------------------------------- /* define the border */
  49.  For %%s in (1 12)Do For /L %%n in (1 1 41) Do (
  50.   Set "}%%n;%%s=%\E%[%%n;%%sH%\E%[90m.%\E%[0m"
  51.   If %%n==41 For /L %%b in (2 1 11)Do (
  52.    Set "}%%n;%%b=%\E%[%%n;%%bH%\E%[90m.%\E%[0m"
  53.   )
  54.  )
  55.  >"%TEMP%\tetris.~tmp" (For /F "Tokens=2* Delims==" %%G in ('Set "}"')Do Echo/%%G)
  56. :newpiece
  57.  CLS
  58.  Type "%TEMP%\tetris.~tmp"
  59.  Set /A pce=%random% %%7 + 1
  60.  !nPiece[%pce%]!
  61.  %ShowPiece%
  62. :loop
  63. rem ----------------------------- /* get pieces last y;x vals to blank out with hidelast */
  64.  For %%c in (y x)Do For %%i in (1 2 3 4)Do Set "l%%c%%i=!%%c%%i!"
  65. rem ----------------------------------------------------------------------- /* game pace */
  66.  Set /A tick+=1
  67.  If "!tick!" == "4" (
  68.   Set "tick=0"
  69.   %movedown%
  70.  )
  71. rem ----------------------------------------------------------------- /* game controller */
  72.  For /F "delims=" %%G in ('choice /N /T 1 /C:prevasd /D S') Do (
  73.   If "%%G" == "E" (TITLE Quit Y\N?&For /F "Delims=" %%C in ('Choice /N /C:YN')Do if %%C==Y (Title & Goto :quit)Else (Title Tetris by T3RRY))
  74.   If "%%G" == "A" %Moveleft%
  75.   If "%%G" == "S" %Movedown%
  76.   If "%%G" == "D" %Moveright%
  77.   If "%%G" == "P" ( Title Paused & Pause > nul & Title Tetris by T3RRY )
  78.   If "%%G" == "V" (For /L %%s in (1 1 39)Do %MoveDown%)
  79. rem ------------------------------------------------------------ /* start rotation logic */
  80. rem --- [ Horizontal flip followed by mirror to simulate 90 degree rotation. ]
  81.   If "%%G" == "R" (
  82. rem ----------------------------------------------------------------- /* flip horizontal */
  83.    Set /A midX=500,midY=500
  84.    For %%A in ("!y1!;!x1!" "!y2!;!x2!" "!y3!;!x3!" "!y4!;!x4!") Do (
  85.     For /F "Tokens=1,2 Delims=;" %%X in ("%%~A") Do (
  86.      Set /A "oY=%%~X"
  87.      Set /A "oX=%%~Y"
  88.     )
  89.     If !oY! LSS !midY! Set /A "midY=!oY!"
  90.     If !oX! LSS !midX! Set /A "midX=!oX!"
  91.    )
  92.    Set "pixel=0"
  93.    For %%B in ("!y1!;!x1!" "!y2!;!x2!" "!y3!;!x3!" "!y4!;!x4!") Do (
  94.     Set /A pixel+=1
  95.     For /F "Tokens=1,2 Delims=;" %%r in ("%%~B") Do (
  96.      Set /A "ny!pixel!=(%%~s-midX)+!midY!,nx!pixel!=(%%~r-!midY!)+!midX!"
  97.    ))
  98. rem -------------------------------------------------------------------------- /* mirror */
  99.   Set "pixel=0"
  100.   Set /A lmidX=500,umidX=0
  101.   For %%C in (!nx1! !nx2! !nx3! !nx4!) Do (
  102.    If %%C LSS !lmidX! (Set /A "lmidX=%%C")
  103.    If %%C GTR !umidX! (Set /A "umidX=%%C")
  104.   )
  105.   Set /A "Mid.X=( !lmidX! + !umidX! ) / 2"
  106.   For %%X in (!nx1! !nx2! !nx3! !nx4!) Do (
  107.    Set /A "pixel=!pixel! + 1"
  108.    Set /A "nx!pixel!=%%X + 1"
  109.    IF %%X LSS !Mid.X! (Set /A "nx!pixel!= %%X + ((!Mid.X!-%%X) * 2) + 1")
  110.    IF %%X GTR !Mid.X! (Set /A "nx!pixel!= %%X - ((%%X-!Mid.X!) * 2) + 1")
  111.   )
  112. rem ------------------------------------------- /* collision detection for rotated coords */
  113.    Set "valid=0"
  114.    For /L %%i in (1 1 4)Do (
  115.     for %%c in ("!ny%%i!;!nx%%i!")Do If "!}%%~c!" == "" (
  116.      Set /A "valid=!valid!+1"
  117.   ))
  118.    If "!valid!" == "4" (
  119.     For %%v in (x y)Do For /L %%i in (1 1 4)Do (
  120.       Set "%%v%%i=!n%%v%%i!"
  121.  )))
  122. rem --------------------------------------------------------------- /* end rotation logic */
  123. rem ------------------------------------------------------------ /* display current state */
  124.   TYPE "%TEMP%\tetris.~tmp"
  125.   %HideLast%
  126.   %ShowPiece%
  127. )
  128. Goto :loop
  129. rem ----------------------------- /* test for completed line when a piece is set in place */
  130. :isMatch
  131.  Set "match=0"
  132.  Set "ymin=42"
  133.  Set "ymax=0"
  134.  For %%n in (!y1! !y2! !y3! !y4!)Do (
  135.   if %%n gtr !ymax! Set "ymax=%%n"
  136.   if %%n lss !ymin! Set "ymin=%%n"
  137.  )
  138. rem ------------- /* test completion of line for each y coord for last piece set in place */
  139.  For /L %%i in (!ymin! 1 !ymax!)Do (
  140.   Set "#of10=0"
  141.   For /L %%c in (2 1 11)Do If Not "!}%%i;%%c!" == "" Set /A "#of10=!#of10!+1"
  142.   If "!#of10!" == "10" (
  143.    Set /A "match=!match!+1"
  144.    Set /A "Score+=(150 * !match!)"
  145.    Set /A "lineabove=%%i-1"
  146.    For /L %%n in (0 1 !lineabove!) Do (
  147.     Set /A "offset=%%n+1"
  148. rem -------------------------- /* transfer updated coord values after iteration complete */
  149.     For %%o in (!offset!)Do For /L %%x in (2 1 11) Do If not "!}%%n;%%x!" == "" (
  150.      Set "n}%%o;%%x=!}%%n;%%x:[%%n;=[%%o;!"
  151.     ) Else (
  152.      Set "n}%%o;%%x="
  153.    ))
  154.    For /L %%Y in (1 1 %%i) Do For /L %%X in (2 1 11) Do Set "}%%Y;%%X=!n}%%Y;%%X!"
  155.    CLS
  156.  ))
  157. rem ----------------------------- /* Update display values for each cell to display file */
  158.  >"%TEMP%\tetris.~tmp" (For /F "Tokens=2* Delims==" %%G in ('Set "}"')Do Echo/%%G)
  159. Exit /B 0
  160. :gameover
  161.  Echo/| CHOICE /N 2> nul & rem BEL
  162.  Echo/%\E%[2;20H%\E%[31mGame Over.%\E%[31m
  163.  Pause > Nul
  164. :quit
  165. rem -------------------------------------- /* restore the cursor ; End of script cleanup */
  166.  <nul Set /P "=%\E%[?25h"
  167.  Del /Q "%TEMP%\tetris.~tmp" 2> nul
  168.  Endlocal
  169.  Endlocal
  170.  cls
  171.  Title
  172. Goto :Eof
Add Comment
Please, Sign In to add comment