Advertisement
T3RRYT3RR0R

High FPS Batch game tech demo

Feb 5th, 2022 (edited)
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.39 KB | None | 0 0
  1. REM Author : T3RRY
  2. REM a simple survival game Demo exampling advanced batch scripting methods.
  3. REM - Features: Collision detection using a Bounding box algorithm
  4. REM             Background fading / Sprite transparency / Sprite colorShifting
  5. REM             Pure Batch Non blocking input
  6. REM             Application of selective frameRates
  7. REM             Pure batch music / sfx player
  8.  
  9. REM Newer Version: https://pastebin.com/Ws5aKkzS
  10.  
  11. REM See it at: https://www.youtube.com/watch?v=QSS7S5M38D4
  12.  
  13.  @Echo off & Cls
  14.  
  15.  If not "%~1"=="" Goto:%1
  16.  CHCP 65001 > nul
  17.  
  18.  If "!!"=="" (
  19.     Echo(Delayed Expansion must be disabled prior to starting %~n0
  20.     Exit /b 1
  21.  )
  22.  
  23.  Call:TestVT || (
  24.     Echo(Virtual terminal sequences not supported
  25.     Exit /b 1
  26.  )
  27.  
  28.  SET every="1/(((~(0-(frames %% #))>>31)&1)&((~((frames %% #)-0)>>31)&1))"
  29.  
  30.  REM properties
  31.  Set /A "Height=35","Width=140","Sprites=0","turn=0"
  32.  Set "BG=10;10;10"
  33.  Set "FG=10;10;10"
  34.  
  35.  Set "S1cR=50"
  36.  Call:DefSprite "╔═══╗\n╬!\E![96m{!\E![91m!\E![48;2;;160;140m☻!\E![96m!\E![48;2;!BG!m}!\E![38;2;!S1cR!;220;!s1cR!m╬\n!\E![33m▀!\E![31m░!\E![33m▀!\E![31m░!\E![33m▀" 5           5         3 5 "!S1cR!;220;!s1cR!" "!BG!"
  37.  Call:DefSprite "╔═╗\n║!\E![33m!\E![48;2;80;60;m♦!\E![38;2;200;;m!\E![48;2;!BG!m║\n╚═╝"       Height/2    Width/2-1 3 3 "200;;"     "!BG!"   2
  38.  Call:DefSprite "╔╗\n╚╝"              Height/2-2  Width/2-2 2 2 "!RandRR!;120;!randBB!"  "!BG!" 3
  39.  Call:DefSprite "╔╗\n╚╝"              Height/2+2  Width/2+2 2 2 "!RandRR!;140;!randBB!"  "!BG!" 4
  40.  Call:DefSprite "╔╗\n╚╝"              Height/2-4  Width/2-4 2 2 "!RandRR!;160;!randBB!"  "!BG!" 3
  41.  Call:DefSprite "╔╗\n╚╝"              Height/2+4  Width/2+4 2 2 "!RandRR!;180;!randBB!"  "!BG!" 4
  42.  Call:DefSprite "╔╗\n╚╝"              Height/2-6  Width/2-6 2 2 "!RandRR!;200;!randBB!"  "!BG!" 3
  43.  Call:DefSprite "╔╗\n╚╝"              Height/2+6  Width/2+6 2 2 "!RandRR!;220;!randBB!"  "!BG!" 4
  44.  Call:DefSprite "╔╗\n╚╝"              Height/2-8  Width/2-8 2 2 "!RandRR!;240;!randBB!"  "!BG!" 3
  45.  Call:DefSprite "╔╗\n╚╝"              Height/2+8  Width/2+8 2 2 "!RandRR!;220;!randBB!"  "!BG!" 4
  46.  Call:DefSprite "╔╗\n╚╝"              Height/2-10 Width/2-8 2 2 "110;!RandGG!;160" "!BG!"       3
  47.  Call:DefSprite "╔╗\n╚╝"              Height/2+10 Width/2+8 2 2 "130;!RandGG!;160" "!BG!"       4
  48.  Call:DefSprite "╔╗\n╚╝"              Height/2-12 Width/2-6 2 2 "150;!RandGG!;180" "!BG!"       3
  49.  Call:DefSprite "╔╗\n╚╝"              Height/2+12 Width/2+6 2 2 "170;!RandGG!;180" "!BG!"       4
  50.  Call:DefSprite "╔╗\n╚╝"              Height/2-14 Width/2-4 2 2 "190;!RandGG!;220" "!BG!"       3
  51.  Call:DefSprite "╔╗\n╚╝"              Height/2+14 Width/2+4 2 2 "210;!RandGG!;220" "!BG!"       4
  52.  Call:DefSprite "╔╗\n╚╝"              Height/2-16 Width/2-2 2 2 "230;!RandGG!;240" "!BG!"       3
  53.  Call:DefSprite "╔╗\n╚╝"              Height/2+16 Width/2+2 2 2 "250;!RandGG!;240" "!BG!"       4
  54.  
  55. REM Control Key assignments.
  56.  Set "kD=Right" & Set "k6=Right"
  57.  Set "kA=Left"  & Set "k4=Left"
  58.  Set "kW=Up"    & Set "k8=Up"
  59.  Set "kS=Down"  & Set "k2=Down"
  60.  
  61. REM Define player sprite movement macros
  62.  Set "Right=Set /A "1/((S1RB)/(S1X))" && Set /A "S1LX=S1X","S1LY=S1Y","S1X+=1" ||Set /A "S1LX=S1X","S1LY=S1Y""
  63.  Set "Left= Set /A "1/(S1LB-S1X)"     && Set /A "S1LX=S1X","S1LY=S1Y","S1X-=1" ||Set /A "S1LX=S1X","S1LY=S1Y""
  64.  Set "Up=   Set /A "1/(S1UB-S1Y)"     && Set /A "S1LX=S1X","S1LY=S1Y","S1Y-=1" ||Set /A "S1LX=S1X","S1LY=S1Y""
  65.  Set "Down= Set /A "1/(S1BB/S1Y)"     && Set /A "S1LX=S1X","S1LY=S1Y","S1Y+=1" ||Set /A "S1LX=S1X","S1LY=S1Y""
  66.  Set "Jump= Set /A "1/(S1BB/(S1Y+1))" || (Set /A "S1LX=S1X","S1LY=S1Y","S1Y-=10")"
  67.  
  68. REM Define non player sprite movement macros
  69.  Set sRight=Set /A "1/(S%%iRB/S%%iX)" ^&^& Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY","S%%iX+=1" ^|^|Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY"
  70.  Set sLeft= Set /A "1/(S%%iLB-S%%iX)" ^&^& Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY","S%%iX-=1" ^|^|Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY"
  71.  Set sUp=   Set /A "1/(S%%iUB-S%%iY)" ^&^& Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY","S%%iY-=1" ^|^|Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY"
  72.  Set sDown= Set /A "1/(S%%iBB/S%%iY)" ^&^& Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY","S%%iY+=1" ^|^|Set /A "S%%iLX=S%%iX","S%%iLY=S%%iY"
  73.  
  74.  Setlocal EnableDelayedExpansion
  75.  
  76.  Echo(Survive^^^!
  77.  Call:PlayMusic "%WINDIR%\Media\windows unlock.wav" 70
  78.  
  79.  Set "Title=Move: W A S D Quit: 'L'.  Turns Survived: ^!turn^!"
  80.  
  81.  Mode %Width%,%Height%
  82.  Title %Title%
  83.  Set /A "Delay=5000","frames=0","RandBB=!Random! %% 130 + 120","RandGG=!Random! %% 130 + 120","RandRR=!Random! %% 70 + 60"
  84.  
  85.  Echo(%\E%[?25l%\E%[1;1H%\E%[48;2;!BG!m%\E%[38;2;!FG!m%\E%[2J%Sprite[1]%%Sprite[2]%%Sprite[3]%%Sprite[4]%%Sprite[5]%%Sprite[6]%%Sprite[7]%%Sprite[8]%%Sprite[9]%%Sprite[10]%%Sprite[11]%%Sprite[12]%%Sprite[13]%%Sprite[14]%%Sprite[15]%%Sprite[16]%%Sprite[17]%%Sprite[18]%
  86.  
  87. Rem Use control function as input to game via pipline.
  88.  "%~F0" CONTROL W >"%temp%\%!!|%~F0" GAME <"%temp%\%~n0_signal.txt"
  89.  EXIT
  90.  
  91.  :GAME
  92.  Setlocal EnableDelayedExpansion
  93.  2> nul (
  94.     For /l %%. in () Do (
  95.                 For /l %%# in (1 1 !Delay!) Do Rem Delay
  96.                 Set /A "frames+=1","BGRm=((S1Y+10)*100)/130","BGBm=((S4X+10)*100)/130"
  97.                 Set /A !every:#=10! && Set /A "Delay-=(Delay/100)","Speed=(99*100)/Delay"
  98.                 Set /A !every:#=2! && (
  99.                 Set /A "BGc+=5","S1cR+=1","turn+=1"
  100.  
  101.                 %= Move sprites. Sprites 3+ are defined as examples of basic patrolling =%
  102.                 For /L %%i in (2 1 !Sprites!)Do %= Implement sprite behaviours =% (
  103.                     If %%i LSS 3 (
  104.                         If !S%%iY! GTR !S1Y! ( Set /A "S%%iYd=S%%iY-S1Y" )Else Set /A "S%%iYd=S1Y-S%%iY"
  105.                         If !S%%iX! GTR !S1X! ( Set /A "S%%iXd=S%%iX-S1Y" )Else Set /A "S%%iXd=S1X-S%%iX"
  106.                         If !S%%iX! GTR !S1X! Set "S%%iDir=3"
  107.                         If !S%%iYd! GTR !S%%iXd! (
  108.                             If !S%%iY! GTR !S1Y! Set "S%%iDir=1"
  109.                             If !S%%iY! LSS !S1Y! Set "S%%iDir=2"
  110.                         )Else (
  111.                             If !S%%iX! GTR !S1X! Set "S%%iDir=3"
  112.                             If !S%%iX! LSS !S1X! Set "S%%iDir=4"
  113.                         )
  114.                         If !S%%iDir! EQU 1 %sUp%
  115.                         If !S%%iDir! EQU 2 %sDown%
  116.                         If !S%%iDir! EQU 3 %sLeft%
  117.                         If !S%%iDir! EQU 4 %sRight%
  118.                     )
  119.                     If %%i GEQ 3 (
  120.                     If !s%%iDir! EQU 3 If !S%%iX! NEQ !S%%iLB! ( Set /A "S%%iLX=S%%iX","S%%iX-=1" )Else Set "s%%iDir=4"
  121.                         If !s%%iDir! EQU 4 If !S%%iX! NEQ !S%%iRB! ( Set /A "S%%iLX=S%%iX","S%%iX+=1" )Else Set "s%%iDir=3"
  122.                 )
  123.  
  124.                     %= Implementation of Bounding box collison test via Bitshifted Or algorithm. Assesses the following conditions and triggers a divide by zero if no condition true =%
  125.                         %= Conditions =% %= If Base1 LSS Top2   =% %=     If Top1 GTR Base2       =% %=    If Right1 LSS Left2    =% %=    If Left1 GTR Right2     =%
  126.                     Set /A "1/(((((S1Y+(S1H-1))-S%%!!)>>)&)|((((%%iY+(S%%!!)))>>)&)|(((()(%%!!))>>)&)|((((%%iX+S%%iW)-1-S1X)>>31)&1))" || (
  127.                         Echo(%\E%[1;1H%\E%[48;2;!BG!m%\E%[38;2;!FG!m%\E%[0J%Sprite[1]%%Sprite[2]%%Sprite[3]%%Sprite[4]%%Sprite[5]%%Sprite[6]%%Sprite[7]%%Sprite[8]%%Sprite[9]%%Sprite[10]%%Sprite[11]%%Sprite[12]%%Sprite[13]%%Sprite[14]%%Sprite[15]%%Sprite[16]%%Sprite[17]%%Sprite[18]%
  128.                     For /F delims^= %%v in ("!Sprite[%%i]:H=H%\E%[7m!")Do Call Echo(%%v
  129.                     Call:PlayMusic "%Windir%\media\Windows Error.wav" 70
  130.                         Cls
  131.                         (Title )
  132.                         Endlocal & Endlocal
  133.                         Start /b "" "%~f0"
  134.                         EXIT
  135.                     )
  136.                 )
  137.                 If !S1cR! GTR 250 Set "S1cR=50"
  138.                 Set "BG=!BGRm!;50;!BGBm!"
  139.                 Set "FG=!BG!"
  140.         )
  141.  
  142.         %= Read last key press from input buffer without waiting. aka Non blocking input =%
  143.                 Set "NewKey="
  144.                 Set /P "NewKey="
  145.         If Defined NewKey For %%v in (!NewKey!)Do (
  146.                     If not "!k%%v!"=="" Set "key=!k%%v!"
  147.                     If %%v == L EXIT
  148.                 )
  149.         If Defined Key (
  150.                 If "!Key!"=="Up" (
  151.                 %Up%
  152.                 Set "LastYkey=Up"
  153.             )
  154.                 If "!Key!"=="Down" (
  155.                 %Down%
  156.                 Set "LastYkey=Down"
  157.             )
  158.                 If "!Key!"=="Left" (
  159.                 %Left%
  160.                 Set "LastXkey=Left"
  161.             )
  162.                 If "!Key!"=="Right" (
  163.                 %Right%
  164.                 Set "LastXkey=Right"
  165.             )
  166.                 If "!Key!"=="Jump" %Jump%
  167.             Title %Title% Speed !Speed!%%
  168.         )
  169.         Echo(%\E%[1;1H%\E%[48;2;!BG!m%\E%[38;2;!FG!m%\E%[0J%Sprite[1]%%Sprite[2]%%Sprite[3]%%Sprite[4]%%Sprite[5]%%Sprite[6]%%Sprite[7]%%Sprite[8]%%Sprite[9]%%Sprite[10]%%Sprite[11]%%Sprite[12]%%Sprite[13]%%Sprite[14]%%Sprite[15]%%Sprite[16]%%Sprite[17]%%Sprite[18]%
  170.     )
  171.  )
  172.  
  173. ===============================================================
  174.  
  175.  :DefSprite "CELLline\nCELLline" Y X H W "RR GG BB(foreground)" "RR GG BB(background)" [1|2|3|4]
  176. REM   Args 1                    2 3 4 5  6                      7                      8 (Starting Direction)
  177.  
  178.     Set /A Sprites+=1
  179.     Set "cells=%~1"
  180.     Set /A "SH=%~4"
  181.     Set /A "SW=%~5"
  182.     Set "FGcol=%~6"
  183.     Set "BGcol=%~7"
  184.         Set "Spacing= "
  185.         For /L %%i in (2 1 %SW%)Do Call Set "Spacing=%%Spacing%% "
  186.     Set /A "S%Sprites%LY=%~2,S%Sprites%Y=%~2","S%Sprites%LX=%~3,S%Sprites%X=%~3","S%Sprites%H=SH","S%Sprites%W=SW"
  187.     Set /A "S%Sprites%UB=2","S%Sprites%BBG=Height-SH","S%Sprites%BB=Height-SH-1","S%Sprites%LB=2","S%Sprites%RB=Width-SW-1","S%Sprites%RBe=Width-SW"
  188.     Call Set "cells=%%Cells:\n=!\E![!S%Sprites%X!G!\E![1B%%"
  189.         Set "ClearCells=!\E![48;2;!BG!m%\E%[38;2;!FG!m!\E![!S%Sprites%LY!;!\E![!S%Sprites%LX!H%Spacing%"
  190.         For /L %%i in (2 1 %SH%)Do Call Set "ClearCells=%%ClearCells%%!\E![1B!\E![!S%Sprites%LX!G%Spacing%"
  191.     Set "Sprite[%Sprites%]=%ClearCells%!\E![!S%Sprites%Y!;!S%Sprites%X!H!\E![48;2;%BGcol%m!\E![38;2;%FGcol%m%Cells%!\E![0m"
  192.     If not "%~8"=="" Set "S%Sprites%Dir=%~8"
  193. Exit /b 0
  194.  
  195.  :TestVT Author:  T3RRY ; Released: 21/01/2022 ==========================================================================
  196. REM     PURPOSE: For use with scripts that utilise Vertual terminal sequences.
  197. REM See:         https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  198. REM     METHOD:  Utilizes powershell to Identify if the terminal running the batch script is succesfully executing virual terminal sequences,
  199. REM              by assessing which character occupies the buffer cell the cursor is currently positioned at.
  200. REM              As this method is slow, an ADS of this file or a temporary file is used to remember if virtual terminal supported
  201.  Cls
  202.  
  203.  For /f "delims=" %%e in ('Echo(Prompt $E^|cmd')Do set "\E=%%e"
  204.  
  205.  2> nul (
  206.     Set "NTFSdrive=true"
  207.     (Echo(verify) >"%~f0:ntfs.test" && (
  208.         Set "SupportINFO=%~f0:VTSupport.dat"
  209.     ) || (
  210.         Set "NTFSdrive="
  211.         For /f delims^= %%G in ("%~f0")Do Set "SupportINFO=%TEMP%\%%~nG_VTSupport.dat"
  212.     )
  213.  )
  214.  
  215.  2> nul (
  216.     More < "%SupportINFO%" > nul && (
  217.         Exit /b 0
  218.     ) || (
  219.         <Nul Set /P "=Verifying Compatability %\E%[2D" 1> CON
  220.         for /F "delims=" %%a in ('"PowerShell.exe $console=$Host.UI.RawUI; $curPos=$console.CursorPosition; $rect=new-object System.Management.Automation.Host.Rectangle $curPos.X,$curPos.Y,$curPos.X,$curPos.Y; $BufCellArray=$console.GetBufferContents($rect); Write-Host $BufCellArray[0,0].Character;"') do (
  221.             Cls
  222.             If "%%a" == "y" (
  223.                 (Echo(true) >"%SupportINFO%"
  224.                 Exit /b 0
  225.             )else (
  226.                 Exit /b 1
  227.             )
  228.         )
  229.     )
  230.  )
  231.  Exit /b 2
  232.  
  233.  
  234. ===============================================================
  235.  
  236. :playMusic
  237.     If "%~1" == "" (
  238.         Echo Play Music Usage:
  239.         Echo/Parameters required For Player: "filepath.ext" 0-100
  240.         pause
  241.         Exit /B
  242.     )
  243.  
  244.     Set "MusicPath=%~1"
  245.     Set /A vol=Loop_TF=0
  246.     Set /A "vol+=%~2 + 0" 2> nul
  247.     Set "Loop_TF=0"
  248.     If not Exist "%~1" Exit /b 1
  249.  
  250. Rem Creates a vbs Script to play audio
  251.     >"%~dp0Play%~3_mp3.vbs" (
  252.         echo Set Sound = CreateObject^("WMPlayer.OCX.7"^)
  253.         echo Sound.URL = "%MusicPath%"
  254.         echo Sound.settings.volume = %vol%
  255.         echo Sound.settings.setMode "loop", %Loop_TF%
  256.         echo Sound.Controls.play
  257.         echo While Sound.playState ^<^> 1
  258.         echo WScript.Sleep 100
  259.         echo Wend
  260.     )
  261.     If "%~3"=="" (
  262.         start /wait /min "" "%~dp0Play_mp3.vbs"
  263.     )Else (
  264.         start /min "" "%~dp0Play%~3_mp3.vbs"
  265.     )
  266.  
  267. Exit /b 0
  268.  
  269. ===============================================================
  270.  
  271. :CONTROL
  272. FOR /L %%C in () do (
  273.     FOR /F "tokens=*" %%A in ('%SystemRoot%\System32\CHOICE.exe /C:abcdefghijklmnopqrstuvwxyz0123456789 /N') DO (
  274.         <NUL SET /P ".=%%A"
  275.         If %%A == L (
  276.             EXIT
  277.         )
  278.     )
  279. )
  280. EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement