Advertisement
T3RRYT3RR0R

Snake v1.0

Dec 9th, 2020
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1. @Echo off
  2. ::: Snake by T3RRY Version 1.0 06/12/2020
  3. ::: To play, Download from https://drive.google.com/file/d/1LAHcmkiKL5XQt1FRdPS5DAKEDSbaKBLL/view?usp=sharing
  4. ::: Download contains:
  5. ::: Getkey.Exe by Antonio {https://stackoverflow.com/users/778560/aacini} for Extended key controls.
  6. ::: Snake.bat v1.2 by T3RRY {https://stackoverflow.com/users/12343998/t3rr0r}
  7. ::: ** Newest Version 1.3 : https://pastebin.com/wQU5cXgQ
  8. rem /* Enable Codepage for unicode characters */
  9. CHCP 65001 > nul
  10. (Set \n=^^^
  11.  
  12. %= Macro Newline Do not Modify this code block =%)
  13. (Set COMMENT=rem {i} ^^^
  14.  
  15. %= Macro Comment line. Do not modify this code block =%)
  16. rem /* use wmic output with find to test if system is windows 10. Exit if not */
  17. wmic OS get OSArchitecture,caption | FIND "10" > nul || (ECHO/Windows 10 required for ascii escape codes & Exit /B)
  18. rem /* Define Escape character */
  19. for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "\E=%%a"
  20. rem ------/* Getkeys controller. allows use of extended keys */
  21. For /F "Delims=" %%C in ('dir "%~dp0*GetKey.exe" /B /S') Do If not defined GetKey Set "GetKey="%%C" /n"
  22. rem /* Ensure array used to display screen elements is undefined. */
  23. (For /F "Tokens=1,2 Delims==" %%G in ('Set "}" 2^> Nul ')Do Set "%%~G=") 2> nul
  24. rem /* game background data file */
  25. Set Background="%TEMP%\%~n0_background.~tmp"
  26. Set Foreground="%TEMP%\%~n0_foreground.~tmp"
  27. Set Score.Save="%TEMP%\%~n0_highscore.~sav"
  28. rem /* Console dimensions */
  29. Set /A ConWidth=80,ConHieght=26
  30. mode %ConWidth%,%ConHieght%
  31. =============================================================================================
  32. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Macros to define Playfield Border and display
  33. rem /* output defined }!y!;!x! coordinates to file to be typed for display; Hide cmd Cursor */
  34. Set "UPDATE.Background=Set /A ".End=!.Ymax! + 1" & ((For /F "Tokens=2* Delims==" %%G in ('Set "}"')Do (< nul Set /P "=%%~G") & < nul Set /P "=%\E%[!.END!;1H%\E%[0m%\E%[?25l")) >%Background% & Type %Background%"
  35. ::: Border cells are defined to }Ypos;Xpos for use in collision detection.
  36. rem /* Usage: %Background.Border%{VTcolor}{.Xmin}{.Xmax}{.Ymin}{.Ymax}{.Char} */
  37. Set Background.Border=Set "Border=" ^& For %%n in (1 2)Do If %%n==2 (%\n%
  38. %COMMENT:{i}= reset arg Border and capture new value \n%
  39. For /F "Tokens=1,2,3,4,5,6 Delims={}" %%G in ("!Border!")Do If not "%%L" == "" (%\n%
  40. %COMMENT:{i}= if 6 args present store in return vars \n%
  41. Set ".Xmin=%%H"^&Set ".Xmax=%%I"^&Set ".Ymin=%%J"^&Set ".Ymax=%%K"^&Set ".Char=%%L"%\n%
  42. %COMMENT:{i}= Sides - for .Xmin .Xmax do for each in .Ymin to .Ymax Define border cell \n%
  43. For %%w in (%%H %%I)Do for /L %%h in (%%J,1,%%K)Do (%\n%
  44. Set "}%%h;%%w=%\E%[%%h;%%wH%\E%[%%~Gm%%~L"%\n%
  45. %COMMENT:{i}= Top + Base - for each in .Xmin to .Xmax do IF .Ymin or .Ymax Define border cell \n%
  46. For %%i in (%%J %%K)Do If %%h Equ %%i (%\n%
  47. For /L %%W in (%%H 1 %%I)Do (%\n%
  48. Set "}%%h;%%W=%\E%[%%h;%%WH%\E%[%%~Gm%%~L"%\n%
  49. )%\n%
  50. )%\n%
  51. )%\n%
  52. )Else (Echo/Missing Args for %%Background.Border%%^& Pause ^> nul)%\n%
  53. )Else Set Border=
  54. ::: Stores return values in Vars: .Xmin .Xmax .Ymin .Ymax .Char
  55. =============================================================================================
  56. :::::: SNAKE display macro. Show / Hide
  57. Set show.snake=For %%n in (1 2)Do if %%n==2 ( %\n%
  58. %COMMENT:{i}= Capture Args for {snake.head} and {snake.tail} \n%
  59. (For /F "Tokens=1,2 Delims={}" %%A in ("!Chars!")Do ( %\n%
  60. %COMMENT:{i}= Substitute hash value in head variable for {snake.head} and output \n%
  61. For %%h in ("!head:#=%%~A!")Do ( %\n%
  62. ^<nul Set /P "=%%~h%\E%[!.End!H" %\n%
  63. ) %\n%
  64. For %%t in (!tail!)Do ( %\n%
  65. %COMMENT:{i}= for each coordinate in tail list output {snake.tail} \n%
  66. ^<nul Set /P "=%\E%[%%~tH%\E%[!Tail.Color!m%%B%\E%[!.End!H%\E%[0m" %\n%
  67. ) %\n%
  68. %COMMENT:{i}= redirect all output to file and type once complete \n%
  69. ))^>%Foreground% ^& Type %foreground% %\n%
  70. )Else Set Chars=
  71. =============================================================================================
  72. ::: Getkey key errorlevel returns:
  73. :::::: Movement Macros with Collision detection.
  74. Set "Left={x}{-}"
  75. Set "Right={x}{+}"
  76. Set "Up={y}{-}"
  77. Set "Down={y}{+}"
  78. Set Move.Snake=For %%n in (1 2) Do if %%n==2 ( %\n%
  79. %COMMENT:{i}= Capture args for {axis.y.x}{vector+-}{snake.tail}{Pi.Char}\n%
  80. %COMMENT:{i}= Assume move vaild until proven false \n%
  81. Set "valid=1" %\n%
  82. For /F "Tokens=1,2,3,4 Delims={}" %%G in ("!Move.Dir!")Do If not "%%~J" == "" ( %\n%
  83. %COMMENT:{i}= define offset coords for evaluation of valid move \n%
  84. If /I "%%G" == "x" (Set /A "lx0=!x0!,ly0=!y0!,nx0= !x0! %%H 1")Else (Set /A "nx0=!x0!") %\n%
  85. If /I "%%G" == "y" (Set /A "lx0=!x0!,ly0=!y0!,ny0= !y0! %%H 1")Else (Set /A "ny0=!y0!") %\n%
  86. %COMMENT:{i}= test Cell definition \n%
  87. For /F "Delims=" %%p in ("!ny0!;!nx0!")Do If not "!{%%p!" == "" ( %\n%
  88. Set "valid=0" %\n%
  89. For /F "Delims=" %%v in ("!{%%p!")Do ( %\n%
  90. Set "Cell=%%v" %\n%
  91. %COMMENT:{i}= if true cell is tail gameover \n%
  92. Set "?tail=!Cell:%%I=!" %\n%
  93. If Not "!?Tail!" == "!Cell!" (Goto :Gameover) %\n%
  94. ) %\n%
  95. %COMMENT:{i}= If true cell is Pi makePi \n%
  96. Set "?tail=!Cell:%%J=!" %\n%
  97. If Not "!?Tail!" == "!Cell!" ( %\n%
  98. Set "{!fy!;!fx!="^& Set "fy="^& Set "fx=" %\n%
  99. Set "Valid=1" %\n%
  100. Set /A "Score+=50" %\n%
  101. If "!Delay!" == "0" Set /A "Score+=75" %\n%
  102. Call :makePi %\n%
  103. Set /A "tail.len+=1" %\n%
  104. Set "Tail="!y0!;!x0!",!Tail!" %\n%
  105. Set "{!ny0!;!nx0!=!Snake.Head!" %\n%
  106. ) %\n%
  107. ) %\n%
  108. %COMMENT:{i}= If cell not tail test next \n%
  109. If "!valid!" == "1" ( %\n%
  110. %COMMENT:{i}= If cell is border gameover \n%
  111. If !n%%G0! LEQ !.%%Gmin! (Goto :Gameover)else If !n%%G0! GEQ !.%%Gmax! (Goto :Gameover)Else ( %\n%
  112. %COMMENT:{i}= Erases last Head Pos \n%
  113. If "!Tail.len!" == "0" ( %\n%
  114. Set "{!y0!;!x0!=" %\n%
  115. ^<nul Set /P "=%\E%[!y0!;!x0!!H " %\n%
  116. )Else ( %\n%
  117. %COMMENT:{i}= Grow Tail \n%
  118. Set "Tail="!y0!;!x0!",!Tail!" %\n%
  119. Set "Tail.{i}=0" %\n%
  120. %COMMENT:{i}= Remove last Cell of tail ; define current tail cells \n%
  121. For %%r in (!Tail!)Do ( %\n%
  122. If !Tail.{i}! EQU !Tail.Len! ( %\n%
  123. Set "Tail=!Tail:,"%%~r"=,!" ^& Set "{%%~r="^&^<nul Set /P "=%\E%[%%~r!H " %\n%
  124. )Else ( %\n%
  125. Set "{%%~r=!Snake.Tail!" %\n%
  126. Set /A "Tail.{i}+=1" %\n%
  127. ) %\n%
  128. ) %\n%
  129. ) %\n%
  130. %COMMENT:{i}= Define new Head Pos \n%
  131. Set "{!ny0!;!nx0!=!Snake.Head!" %\n%
  132. Set /A "%%G0=!n%%G0!" %\n%
  133. ) %\n%
  134. ) %\n%
  135. ) %\n%
  136. )Else Set Move.Dir=
  137. Set "Info=<nul Set /P "=%\E%[10;55HScore: !Score!%\E%[11;55HHigh Score:!HighScore!%\E%[12;55HPi Eaten:!Tail.Len!%\E%[15;55HDelay:%\E%[K + !Delay! -""
  138. =============================================================================================
  139. ::::::::::::::::: End macro definition's
  140. =============================================================================================
  141. :start
  142. Title Snake by T3RRY & CLS
  143. =============================================================================================
  144. rem /* Initialise Game variables */
  145. Set /A "Delay=20,x0=24,y0=12,tail.len=0,Score=0,HighScore=0" & REM snake head start position
  146. Set "head.color=35"&Set "tail.color=36" & Set "Tail="& REM snake properties
  147. Set "Snake.Head=@"&Set "Snake.Tail=#"
  148. Set "head=%\E%[!y0!;!x0!H%\E%[!head.color!m#%\E%[0m"
  149. Set "Pi.char=%\E%(0{%\E%(B" & set "Pi.color=32"
  150. Set "}2;55=%\E%[2;55H%\E%[33mControls:"
  151. Set "}3;55=%\E%[3;55H ▲ - Up"
  152. Set "}4;55=%\E%[4;55H ◄ - Left"
  153. Set "}5;55=%\E%[5;55H ▼ - Down"
  154. Set "}6;55=%\E%[6;55H ► - Right"
  155. Set "}7;55=%\E%[7;55HSpace - Pause"
  156. Set "}8;55=%\E%[8;55HEscape - Quit"
  157. rem /* Reference variables for keypress errorlevel { translate errorcode from getkey } */
  158. Set "k-72=Up"&Set "k-80=Down"&Set "k-75=Left"&Set "k-77=Right"&Set "k32=space"&Set "k43=plus"&Set "k45=minus"&Set "k27=ESC"
  159. rem /* Reference variables to get opposing momentum { prevent tail collision by direction change } */
  160. Set ".Left=Right"&Set ".Right=Left"&Set ".Up=Down"&Set ".Down=Up"
  161. rem /* Space as pause key; Start game Paused */
  162. Set "Dir=Space"
  163. ==================================
  164. rem /* Enable Macro's */
  165. Setlocal EnableDelayedExpansion
  166. =============================================================================================
  167. rem /* Generate border with desired {Color}{.Xmin}{.Xmax}{.Ymin}{.Ymax}{Character}*/
  168. %Background.Border%{48;2;150;100;70}{1}{50}{1}{25}{▒}
  169. %UPDATE.Background%
  170. Call :makePi
  171. If exist %Score.Save% (
  172. <%Score.Save% (Set /P "HighScore=")
  173. )Else >%Score.Save% Echo/!Score!
  174. =============================================================================================
  175. :gameloop
  176. rem /* Resume momentum possessed prior to Pause { prevent tail collision by direction change } */
  177. If Defined Last.Dir If /I Not "!Dir!" == "Space" (
  178. Set "Dir=!Last.Dir!"
  179. Set "Last.Dir="
  180. )
  181. If !Score! GTR !HighScore! (
  182. Set "Highscore=!Score!"
  183. >%Score.Save% Echo/!Score!
  184. )
  185. rem /* Expand macro displaying Controls ; Score info */
  186. %INFO%
  187. =============================================================================================
  188. rem /* Test move Doesn't oppose current momentum ; Expand Move.Snake macro with Direction Vector
  189. rem /* and variables containing Collision test Characters. { If not Paused } */
  190. If /I not "!Dir!" == "Space" For %%D in (!%Dir%!)Do %Move.Snake%%%D{%Snake.Tail%}{%Pi.char%}
  191. =============================================================================================
  192. %Show.Snake%{!Snake.Head!}{!Snake.Tail!}
  193. %getKey%
  194. For %%e in (!Errorlevel!)Do If not "!k%%e!" == "" For %%v in (!Dir!)Do if not "!k%%e!" == "!.%%v!" (
  195. Set "Key=!k%%e!"
  196. If not defined Last.dir If /I "!Key!" == "Space" If /I not "!Dir!" == "!Space!" Set "Last.Dir=!Dir!"
  197. If /I "!Key!" == "plus" (If !Delay! LSS 150 (Set /A "Delay+=5")&Goto :gameloop)
  198. If /I "!Key!" == "minus" (If !Delay! GEQ 5 (Set /A "Delay-=5")&Goto :gameloop)
  199. Set "Dir=!k%%e!"
  200. )
  201. If "!Key!" == "" (goto :gameloop)
  202. If /I "!Key!" == "ESC" (goto :End)
  203. rem /* Enact User Adjustable Delay { + - during gameplay } */
  204. If %Delay% GTR 0 ( For /L %%d in (1 1 !Delay!)Do Call :Delay 2> nul )Else Call :Delay 2> nul
  205. Goto :gameloop
  206. =============================================================================================
  207. :Gameover
  208. rem /* update screen elements to highlight death ; End the current session ; restart */
  209. Title Game over
  210. Set "Tail.Color=90"
  211. %Show.Snake%{%\E%[31m!Snake.Head!}{!Snake.Tail!}
  212. %Background.Border%{48;2;75;75;165}{1}{50}{1}{25}{X}
  213. %UPDATE.Background%
  214. Echo/|Choice.exe /N /C:n 2> nul
  215. Timeout /T 2 /NoBreak > nul
  216. Pause > Nul
  217. Del "%TEMP%\%~n0*.~tmp"
  218. CLS
  219. Endlocal
  220. Goto :Start
  221. :End
  222. rem /* restore cmd cursor */
  223. <nul Set /P "=%\E%[?25h"
  224. Endlocal
  225. Del "%TEMP%\%~n0*.~tmp"
  226. Goto :Eof
  227. =============================================================================================
  228. :makePi
  229. Setlocal enableDelayedExpansion
  230. :confirmpos
  231. If not "!fy!" == "" If not "!fx!" == "" (
  232. <nul Set /P "=%\E%[!fy!;!fx!H %\E%[0m"
  233. )
  234. Set "fValid=1"
  235. Set /a fy=!random! %% (!.Ymax! - 2) + 2,fx=!random! %% (!.Xmax! - 2) + 2
  236. For /F "delims=" %%g in ("{!fy!;!fx!")Do If Not "!%%g!" == "" (Set "fValid=0")
  237. For /F "delims=" %%h in ("}!fy!;!fx!")Do If Not "!%%h!" == "" (Set "fValid=0")
  238. If "!fValid!" == "1" (
  239. <nul Set /P "=%\E%[!fy!;!fx!H%\E%[%Pi.color%m!Pi.char!%\E%[0m"
  240. Endlocal & Set "fy=%fy%" & Set "fy=%fx%" & Set "{%fy%;%fx%=%Pi.char%"
  241. Exit /B 0
  242. )
  243. Goto :confirmpos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement