Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for snake.bat

(view source code of snake.bat as plain text)

  1. ::::::::::::::::::::::::::::::::::
  2. :: Windows 10 Console ANSI Demo ::
  3. ::  Robert F Van Etta III 2018  ::
  4. ::::::::::::::::::::::::::::::::::
  5.  
  6. :initialization
  7.  
  8. 	::Don't show commands, don't retain variables, enable delayed expansion, clear the screen
  9. 	@echo off & setlocal & setlocal enabledelayedexpansion & cls
  10.  
  11. 	::Initialize Row and Column history arrays
  12. 	for /l %%a in (1,1,16) do (set RowArr=  0!RowArr!) & set ColArr=!RowArr!
  13.  
  14. 	::Define Default Screen Dimensions and set starting position/direction
  15. 	set /a MaxRow=40, MaxCol=120, CurRow=1, CurCol=1, CurRowD=1, CurColD=1
  16.  
  17. 	::Update the Window title to help users terminate the script...
  18. 	title Press Control+C (Twice sometimes) to Stop
  19.  
  20. 	::Use forfiles to capture non-typable ASCII characters
  21. 	for /f "tokens=* delims=" %%a in ('forfiles /p %~dps0 /m %~nxs0 /c "cmd /c echo.0x200xB00xB10xB20xDB0x1B"') do @set arrChar=%%a
  22. 	set Esc=!arrChar:~5,1!
  23.  
  24. 	::16 sets of 4 bytes that represent a gray-scale fade from white to black
  25. 	set FadeArray=7140713771277117704701170127001701370140013000370120701001100040
  26.  
  27. 	::Use results of "mode con" to determine console buffer dimensions
  28. 	for /f "tokens=1,2" %%a in ('mode con') do (
  29. 		if "%%a"=="Lines:" set MaxRow=%%b
  30. 		if "%%a"=="Columns:" set MaxCol=%%b
  31. 		)
  32.  
  33. 	::Limit console row buffer/window size to 40 rows
  34. 	if %MaxRow% GTR 40 set MaxRow=40
  35.  
  36. 	::Lockdown the window dimensions
  37. 	mode con cols=%MaxCol% lines=%MaxRow%
  38.  
  39. :Loop
  40. 	::If at top or left then "bounce"
  41. 	if %CurRow% equ 1 set CurRowD=1
  42. 	if %CurCol% equ 1 set CurColD=1
  43.  
  44. 	::If at bottom or right "bounce"
  45. 	if %CurCol% equ %MaxCol% set CurColD=-1
  46. 	if %CurRow% equ %MaxRow% set CurRowD=-1
  47.  
  48. 	::Calculate new location and format it with fixed size for array
  49. 	set /a CurCol=CurCol+CurColD, CurRow=CurRow+CurRowD
  50. 	set CurCol=  %CurCol%& set CurRow=  %CurRow%
  51.  
  52. 	::Push current positions to front and remove last value
  53.     set ColArr=!CurCol:~-3!!ColArr:~,45!& set RowArr=!CurRow:~-3!!RowArr:~,45!
  54.  
  55. 	::Draw from new position, fading out to black in 16 steps (0-15)
  56. 	for /l %%a in (0,1,15) do (
  57.  
  58. 		::Each PacChar item is 4 bytes. Each value of RowArr/ColArr is 3 bytes. 
  59. 		set /a ia=%%a*4, ib=%%a*3
  60.  
  61. 		::Use For loops to assist in extracting values from the arrays (strings)
  62. 		for %%b in (!ib!) do set /a row=!RowArr:~%%b,3!, col=!ColArr:~%%b,3!
  63. 		for %%b in (!ia!) do set PacChar=!FadeArray:~%%b,4!
  64.  
  65. 		::PacChar consist of 4 bytes: FGColor, Intensity, intChar, BGColor
  66. 		set fg=!PacChar:~0,1!&set intChar=!PacChar:~2,1!& set bg=!PacChar:~3,1!
  67. 		set /a i=!PacChar:~1,1!, bg=bg+40, fg=fg+30
  68.  
  69. 		::Char references the ASCII characters stored in arrChar on line 21
  70. 		for %%b in (!intChar!) do set Char=!arrChar:~%%b,1!
  71.  
  72. 		::Change: row, col, intensity, fgcolor, bgcolor/Display char/Reset colors/Move to 0,0
  73. 	    echo %Esc%[!row!;!col!H%Esc%[!i!;!fg!m%Esc%[!bg!m!Char!%Esc%[0m%Esc%[0;0H
  74. 	)
  75. goto :Loop
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  

page last uploaded: 2018-12-04, 10:47