Advertisement
Guest User

Athena Robotics Tetris.bat File

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