Advertisement
T3RRYT3RR0R

Batch Game - Snake

Dec 6th, 2020 (edited)
1,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.02 KB | None | 0 0
  1. @Echo off
  2. ==================================================
  3. ::: Snake by T3RRY Version 1.3 08/12/2020
  4. ::: To play, Download from https://drive.google.com/file/d/1_ivtZsSzxEiig3bdCurNFg7SqGES4T53/view?usp=sharing
  5. ::: * This paste is a newer version than contained in the download file *
  6. ::: Download contains:
  7. ::: Getkey.Exe by Antonio {https://stackoverflow.com/users/778560/aacini} for Extended key controls.
  8. ::: Snake.bat by T3RRY {https://stackoverflow.com/users/12343998/t3rr0r}
  9. ==================================================
  10.  If "!![" == "[" (Echo/Delayed expansion Must not be enabled prior to starting %~n0.bat & Pause & Exit /B 0)
  11.  CHCP 65001 > nul
  12. (Set \n=^^^
  13.  
  14. %= Macro Newline Do not Modify this code block =%)
  15.  wmic OS get OSArchitecture,caption | FIND "10" > nul || (ECHO/Windows 10 required for ascii escape codes & Exit /B)
  16.  for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "\E=%%a"
  17.  For /F "Delims=" %%C in ('dir "%~dp0*GetKey.exe" /B /S') Do If not defined GetKey Set "GetKey="%%C" /n"
  18.  Echo/%GetKey%|findstr.exe /LIC:"GetKey.exe" > nul || (Echo/GetKey.Exe not found in Directory. Game cannot be played without GetKey.exe &Pause &Exit /B 0)
  19.  (For /F "Tokens=1,2 Delims==" %%G in ('Set "}" 2^> Nul ')Do Set "%%~G=") 2> nul
  20.  If not exist "%TEMP%" (Set "Save.Dir=%~dp0")Else Set "Save.Dir=%TEMP%\"
  21.  Set Background="%Save.Dir%%~n0_background.~tmp"
  22.  Set Foreground="%Save.Dir%%~n0_foreground.~tmp"
  23.  Set Score.Save="%Save.Dir%%~n0_highscore.~sav"
  24.  mode 80,26
  25.  
  26.  Set Background.Border=Set "Border=" ^& For %%n in (1 2)Do If %%n==2 (%\n%
  27.   For /F "Tokens=1,2,3,4,5,6 Delims={}" %%G in ("!Border!")Do If not "%%L" == "" (%\n%
  28.    Set /A ".Xmin=%%H,.Xmax=%%I,.Ymin=%%J,.Ymax=%%K"%\n%
  29.    Set ".Char=%%L"%\n%
  30.    For %%w in (%%H %%I)Do for /L %%h in (%%J,1,%%K)Do (%\n%
  31.     Set "}%%h;%%w=%\E%[%%h;%%wH%\E%[%%~Gm%%~L"%\n%
  32.     For %%i in (%%J %%K)Do If %%h Equ %%i (%\n%
  33.      For /L %%W in (%%H 1 %%I)Do (%\n%
  34.       Set "}%%h;%%W=%\E%[%%h;%%WH%\E%[%%~Gm%%~L"%\n%
  35.   ))))Else ( Echo/Missing Args for %%Background.Border%% ^& Pause ^> nul )%\n%
  36.  )Else Set Border=
  37.  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%"
  38.  Set show.snake=For %%n in (1 2)Do if %%n==2 ( %\n%
  39.   (For /F "Tokens=1,2 Delims={}" %%A in ("!Chars!")Do ( %\n%
  40.     For %%h in ("!head:#=%%~A!")Do ( %\n%
  41.      ^<nul Set /P "=%%~h%\E%[!.End!H" %\n%
  42.     ) %\n%
  43.     For %%t in (!tail!)Do ( %\n%
  44.     ^<nul Set /P "=%\E%[%%~tH%\E%[!Tail.Color!m%%B%\E%[!.End!H%\E%[0m" %\n%
  45.   )))^>%Foreground% ^& Type %foreground% %\n%
  46.  )Else Set Chars=
  47.  Set "Left={x}{-}"&Set "Right={x}{+}"&Set "Up={y}{-}"&Set "Down={y}{+}"
  48.  Set Move.Snake=For %%n in (1 2) Do if %%n==2 ( %\n%
  49.   Set "valid=1" %\n%
  50.   For /F "Tokens=1,2,3,4 Delims={}" %%G in ("!Move.Dir!")Do If not "%%~J" == "" ( %\n%
  51.    If /I "%%G" == "x" (Set /A "lx0=!x0!,ly0=!y0!,nx0= !x0! %%H 1")Else (Set /A "nx0=!x0!") %\n%
  52.    If /I "%%G" == "y" (Set /A "lx0=!x0!,ly0=!y0!,ny0= !y0! %%H 1")Else (Set /A "ny0=!y0!") %\n%
  53.    For /F "Delims=" %%p in ("!ny0!;!nx0!")Do If not "!{%%p!" == "" ( %\n%
  54.     Set "valid=0" %\n%
  55.     For /F "Delims=" %%v in ("!{%%p!")Do ( %\n%
  56.      Set "Cell=%%v" %\n%
  57.      Set "?tail=!Cell:%%I=!" %\n%
  58.      If Not "!?Tail!" == "!Cell!" (Goto :Gameover) %\n%
  59.     ) %\n%
  60.     Set "?tail=!Cell:%%J=!" %\n%
  61.     If Not "!?Tail!" == "!Cell!" ( %\n%
  62.      Set "{!fy!;!fx!="^& Set "fy="^& Set "fx=" %\n%
  63.      Set "Valid=1" %\n%
  64.      Set /A "Score+=50" %\n%
  65.      If "!Delay!" == "0" Set /A "Score+=75" %\n%
  66.      Call :makePi %\n%
  67.      Set /A "tail.len+=1" %\n%
  68.      Set "Tail="!y0!;!x0!",!Tail!" %\n%
  69.      Set "{!ny0!;!nx0!=!Snake.Head!" %\n%
  70.    )) %\n%
  71.    If "!valid!" == "1" ( %\n%
  72.     If !n%%G0! LEQ !.%%Gmin! (Goto :Gameover)else If !n%%G0! GEQ !.%%Gmax! (Goto :Gameover)Else ( %\n%
  73.      If "!Tail.len!" == "0" ( %\n%
  74.       Set "{!y0!;!x0!=" %\n%
  75.       ^<nul Set /P "=%\E%[!y0!;!x0!!H " %\n%
  76.      )Else ( %\n%
  77.       Set "Tail="!y0!;!x0!",!Tail!" %\n%
  78.       Set "Tail.{i}=0" %\n%
  79.       For %%r in (!Tail!)Do ( %\n%
  80.        If !Tail.{i}! EQU !Tail.Len! ( %\n%
  81.         Set "Tail=!Tail:,"%%~r"=,!" ^& Set "{%%~r="^&^<nul Set /P "=%\E%[%%~r!H " %\n%
  82.        )Else ( %\n%
  83.         Set "{%%~r=!Snake.Tail!" %\n%
  84.         Set /A "Tail.{i}+=1" %\n%
  85.      ))) %\n%
  86.      Set "{!ny0!;!nx0!=!Snake.Head!" %\n%
  87.      Set /A "%%G0=!n%%G0!" %\n%
  88.  ))))Else Set Move.Dir=
  89.  Set "head=%\E%[!y0!;!x0!H%\E%[!head.color!m#%\E%[0m"
  90.  Set "DefArray=For %%n in (1 2)Do if %%n==2 (Set /A "{i}=0"& For %%G in (!list!)Do (Set "$V[!{i}!]=%%G"& Set /A "{i}+=1"))Else Set List="
  91.  Set "Info=For %%i in (!Theme!)Do <nul Set /P "=%\E%[9;55HTheme  - 0~9 %\E%[!Border.Color[%%i]!m!Theme!%\E%[0m%\E%[10;55HScore: !Score!%\E%[11;55HHigh Score:!HighScore!%\E%[12;55HPi Eaten:!Tail.Len!%\E%[0m%\E%[13;55HDelay:%\E%[K + !Delay! -%\E%[15;55H%\E%[K%\E%[90mHeading: !Dir:space=Pause!     %\E%[16;55H%\E%[90mLast Move: !Key!    ""
  92.  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"
  93.  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"
  94.  Set /A "k48=0,k49=1,k50=2,k51=3,k52=4,k53=5,k54=6,k55=7,k56=8,k57=9,Theme=0,HighScore=0"
  95.  Set ".Left=Right"&Set ".Right=Left"&Set ".Up=Down"&Set ".Down=Up"
  96.  Set "Dir=Space"
  97. :start
  98.  Title Snake by T3RRY & CLS
  99.  Set /A "Delay=10,x0=24,y0=12,tail.len=0,Score=0"
  100.  Set "Pi.char=%\E%(0{%\E%(B" & set "Pi.color=37"
  101.  Set "}2;55=%\E%[2;55H%\E%[33mControls:"
  102.  Set "}3;55=%\E%[3;55H  ▲    - Up"
  103.  Set "}4;55=%\E%[4;55H  ◄    - Left"
  104.  Set "}5;55=%\E%[5;55H  ▼    - Down"
  105.  Set "}6;55=%\E%[6;55H  ►    - Right"
  106.  Set "}7;55=%\E%[7;55HSpace  - Pause"
  107.  Set "}8;55=%\E%[8;55HEscape - Quit"
  108.  Setlocal EnableExtensions EnableDelayedExpansion
  109.  %DefArray:$V=Head.Color% 32 33 34 35 36 37 93 94 95 96
  110.  %DefArray:$V=Tail.Color% 96 95 94 93 37 36 35 33 34 32
  111.  %DefArray:$V=Snake.Head% @  D  O  E  8  K  #  +  ]  [
  112.  %DefArray:$V=Snake.Tail% #  x  D  O  E  [  ]  .  -  +
  113.  %DefArray:$V=Border.Char%     "☺" "#" "@" "♦" "◙" "◘" "○" "▓" "▒" "░"
  114.  %DefArray:$V=Border.Color%    31 93 33 92 "38;2;155;125;60" 35 96 32 36 24
  115.  For %%i in (%Theme%)Do For %%o in (head tail)Do If not "!%%o.Color[%%i]!" == "" (Set "%%o.Color=!%%o.Color[%%i]!"&Set "Snake.%%o=!Snake.%%o[%%i]!"&Set "Border.Color=!Border.Color[%%i]!"&Set "Border.Char=!Border.Char[%%i]!")
  116.  %Background.Border%{!Border.Color[%Theme%]!}{1}{50}{1}{25}{!Border.Char[%Theme%]!}
  117.  %UPDATE.Background%
  118.  Call :makePi
  119.  If exist %Score.Save% (
  120.   <%Score.Save% (
  121.     Set /P "HighScore="
  122.    )
  123.  )
  124. set /A Highscore+=0
  125. :gameloop
  126.  If Defined Last.Dir If /I Not "!Dir!" == "Space" (
  127.   Set "Dir=!Last.Dir!" & Set "Last.Dir="
  128.  )
  129.  If %Score% GTR %HighScore% (
  130.   Set /A "Highscore=%Score%"
  131.   >%Score.Save% Echo/%Score%
  132.  )
  133.  %INFO%
  134.  If /I not "!Dir!" == "Space" For %%D in (!%Dir%!)Do %Move.Snake%%%D{%Snake.Tail%}{%Pi.char%}
  135.  %Show.Snake%{!Snake.Head!}{!Snake.Tail!}
  136.  %getKey%
  137.  For %%e in (!Errorlevel!)Do If not "!k%%e!" == "" For %%v in (!Dir!)Do if not "!k%%e!" == "!.%%v!" (
  138.   Set "cc=0"
  139.   For %%i in (!k%%e!)Do For %%o in (head tail)Do If not "!%%o.Color[%%i]!" == "" (
  140.    Set "%%o.Color=!%%o.Color[%%i]!"&Set "Snake.%%o=!Snake.%%o[%%i]!"&Set /A "cc=1,Theme=%%i"
  141.    %Background.Border%{!Border.Color[%%i]!}{!.Xmin!}{!.Xmax!}{!.Ymin!}{!.Ymax!}{!Border.Char[%%i]!}
  142.    %UPDATE.Background%
  143.   )
  144.   If Not "!cc!" == "1" (Set "Key=!k%%e!")Else Goto :gameloop
  145.   If not defined Last.dir If /I "!Key!" == "Space" If /I not "!Dir!" == "!Space!" Set "Last.Dir=!Dir!"
  146.   If /I "!Key!" == "plus" (If !Delay! LSS 75 (Set /A "Delay+=1")&Goto :gameloop)
  147.   If /I "!Key!" == "minus" (If !Delay! GEQ 1 (Set /A "Delay-=1")&Goto :gameloop)
  148.   If /I not "!Key!" == "minus" If /I not "!key!" == "plus" Set "Dir=!k%%e!"
  149.  )
  150.  If "!Key!" == "" (goto :gameloop)
  151.  If /I "!Key!" == "ESC" (goto :End)
  152.  If %Delay% GTR 0 ( For /L %%d in (1 1 !Delay!)Do Call :Delay 2> nul )Else Call :Delay 2> nul
  153. Goto :gameloop
  154. :Gameover
  155.  Title Game over
  156.  Set "Tail.Color=90"
  157.  Echo/|Choice.exe /N /C:n 2> nul
  158.  %Background.Border%{48;2;75;75;165}{!.Xmin!}{!.Xmax!}{!.Ymin!}{!.Ymax!}{!Border.Char[%%i]!}{#}
  159.  %UPDATE.Background%
  160.  %Show.Snake%{%\E%[31m!Snake.Head!}{!Snake.Tail!}
  161.  Timeout /T 2 /NoBreak > nul
  162.  Del "%Save.Dir%%~n0*.~tmp"
  163.  Pause > Nul
  164.  CLS & Endlocal
  165. Goto :Start
  166. :End
  167.  <nul Set /P "=%\E%[?25h"
  168.  Del "%Save.Dir%%~n0*.~tmp"
  169.  Endlocal
  170.  Goto :Eof
  171. :makePi function
  172. Setlocal enableDelayedExpansion
  173. :confirmpos
  174.  If not "!fy!" == "" If not "!fx!" == "" ( <nul Set /P "=%\E%[!fy!;!fx!H %\E%[0m" )
  175.  Set "fValid=1"
  176.  Set /a fy=!random! %% (!.Ymax! - 2) + 2,fx=!random! %% (!.Xmax! - 2) + 2
  177.  For /F "delims=" %%g in ("{!fy!;!fx!")Do If Not "!%%g!" == "" (Set "fValid=0")
  178.  For /F "delims=" %%h in ("}!fy!;!fx!")Do If Not "!%%h!" == "" (Set "fValid=0")
  179.  If "!fValid!" == "1" (
  180.   <nul Set /P "=%\E%[!fy!;!fx!H%\E%[%Pi.color%m!Pi.char!%\E%[0m"
  181.   Endlocal & Set "fy=%fy%" & Set "fx=%fx%" & Set "{%fy%;%fx%=%Pi.char%"
  182.   Exit /B 0
  183.  )
  184. Goto :confirmpos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement