Advertisement
T3RRYT3RR0R

Batch Escape the Ampersand Mini Game V3

Feb 18th, 2020
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.54 KB | None | 0 0
  1. REM ::: Batch Minigame Tutorial By T3RRY & CLS
  2.  
  3. @ECHO OFF & TITLE Escape the Ampersand Demo Game
  4. :start
  5.  
  6. ::: REM EXIT /B 0 is used to return from Functions with a Zero Errorlevel. This is done due to the frequent use of Choice Command with Errorlevel.
  7.  
  8. ::: REM Define 'Play Field' Parameters. Allows Play Field Characteristics to be easily Changed. Only Hieght and Width need changing
  9. :: Outer Border
  10. REM Hieght Limit = 38
  11. REM Width Limit = 150
  12.     Set "Hieght=30"
  13.     Set "Width=40"
  14.  
  15.     Set "Lines=%Hieght%"
  16.     Set /a "Lines+=5"
  17.     Set "Columns=%Width%"
  18.  
  19. ::: Establish Screen size. Larger screen sizes will result in slower Load times and Gameplay.
  20.  
  21. cls & Mode Con cols=%Columns% Lines=%Lines%
  22.  
  23. :: Inner Border
  24.     Set "Axis_mins=2"
  25.     Set /A "X_Axis_Boundary=%Width% - 1"
  26.     Set /A "Y_Axis_Boundary=%Hieght% - 1"
  27. :: Move Limits within Borders
  28.     Set "Limit_Axis_Min=3"
  29.     Set /A "Limit_X_Max=%X_Axis_Boundary% - 1"
  30.     Set /A "Limit_Y_Max=%Y_Axis_Boundary% - 1"
  31. :: Character Displayed as Outside Border
  32.     Set "space= "
  33. :: Character Displayed as Enenmy
  34.     Set "P_C=@"
  35.     Set "E_C=^&"
  36.     Set "Esc=^"
  37.     Set "ENEMY=%E_C%"
  38.     Set "Game_Over=Goto :end"
  39. :: Instruction Line Pos
  40.     Set /A Instructions=%hieght% + 2
  41.  
  42.  
  43. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Macro Definition
  44.  
  45. setlocal DisableDelayedExpansion
  46. set LF=^
  47.  
  48.  
  49. ::Above 2 blank lines are required - do not remove
  50. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  51.  
  52.  
  53. Set @COL=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  54.       for /F "tokens=1 delims=," %%G in ("!argv!") do (%\n%
  55. Set /a "Rand_Col=!random! * 5 / 32768 + 1" %\n%
  56. IF !Rand_Col!==5 (Set "%%G=!E_C!") %\n%
  57. IF !Rand_Col!==4 (Set "%%G=!E_C!") %\n%
  58. IF !Rand_Col!==3 (Set "%%G=!E_C!") %\n%
  59. IF !Rand_Col!==2 (Set "%%G=!E_C!") %\n%
  60. IF !Rand_Col!==1 (Set "%%G=!E_C!") %\n%
  61.       ) %\n%
  62. ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  63.  
  64. Set @POS=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  65.       for /F "tokens=1 delims=, " %%G in ("!argv!") do (%\n%
  66. ECHO([!L_Y_Pos!;!L_X_Pos!H.%\n%
  67. ECHO([!L_AI_Y!;!L_AI_X!H.%\n%
  68. ECHO([!L_AI2_Y!;!L_AI2_X!H.%\n%
  69. ECHO([!Y_Pos!;!X_Pos!H!P_C!%\n%
  70.       ) %\n%
  71. ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  72.  
  73.  
  74. Set @REFRESH=for /L %%n in (1 1 2) do if %%n==2 (%\n%
  75.       for /F "tokens=1 delims=, " %%G in ("!argv!") do (%\n%
  76.     IF !L_X_Pos!==!X_Pos! IF !L_Y_Pos!==!Y_Pos! ECHO([!Y_Pos!;!X_Pos!H!P_C!%\n%
  77.     IF !X_Pos!==!AI_X! IF !Y_Pos!==!AI_Y! (%\n%
  78.         ECHO([!Y_Pos!;!X_Pos!HX%\n%
  79.         Set "G_Over=You were Nommed by the AI"%\n%
  80.         !Game_Over!%\n%
  81.     ) %\n%
  82.     IF !X_Pos!==!AI2_X! IF !Y_Pos!==!AI2_Y! (%\n%
  83.         ECHO([!Y_Pos!;!X_Pos!HX%\n%
  84.         Set "G_Over=You were Nommed by the AI"%\n%
  85.         !Game_Over!%\n%
  86.     ) %\n%
  87.     IF !X_Pos!==!X_B! IF !Y_Pos!==!Y_B! (%\n%
  88.         ECHO([!Y_Pos!;!X_Pos!HX%\n%
  89.         Set "G_Over=You Hit the Bomb"%\n%
  90.         !Game_Over!%\n%
  91.     ) %\n%
  92.     IF !X_Pos!==!X_Esc! IF !Y_Pos!==!Y_Esc! (%\n%
  93.         ECHO([!Y_Pos!;!X_Pos!H!Esc!%\n%
  94.         ECHO([!AI_Y!;!AI_X!H!space!%\n%
  95.         ECHO([!AI2_Y!;!AI2_X!H!space!%\n%
  96.         Set "G_Over= You've Found the Escape"%\n%
  97.         !Game_Over!%\n%
  98.     ) %\n%
  99.       ) %\n%
  100. ) ELSE setlocal enableDelayedExpansion ^& set argv=,
  101.  
  102.  
  103. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Macro's
  104.  
  105. ::: REM Build Screen Background using for /L loop to repeat action for the desired number of times in each Line and Column Position
  106. ::: REM (cont) Defined within the Nested Loops.
  107.  
  108.     For /L %%A IN (1,1,%Width%) DO (
  109.         For /L %%B IN (1,1,%hieght%) DO (
  110.         ECHO([%%B;%%AHX
  111.         )
  112.     )
  113.    
  114.     For /L %%A IN (1,1,%hieght%) DO (
  115.         For %%B IN (%Axis_mins%,%X_Axis_Boundary%) DO (
  116.         ECHO([%%A;%%BH+
  117.         )
  118.     )
  119.  
  120.     For /L %%A IN (1,1,%Width%) DO (
  121.         For %%B IN (%Axis_mins%,%Y_Axis_Boundary%) DO (
  122.         ECHO([%%B;%%AH+
  123.         )
  124.     )
  125.  
  126.     For /L %%A IN (1,1,%hieght%) DO (
  127.         ECHO([%%A;1H%space%
  128.         ECHO([%%A;%Width%H%space%
  129.     )
  130.  
  131.     For /L %%A IN (1,1,%Width%) DO (
  132.         ECHO([1;%%AH%space%
  133.         ECHO([%hieght%;%%AH%space%
  134.     )
  135.  
  136. ::: REM Define Starting Positions
  137. :startpos
  138. Set /A "X_Pos=%random% %%%Limit_X_Max% + 2"
  139. Set /A "Y_Pos=%random% %%%Limit_Y_Max% + 2"
  140.  
  141. Set /A AI_X=%random% %%%Limit_X_Max% + 2
  142. Set /A AI_Y=%random% %%%Limit_Y_Max% + 2
  143.  
  144. Set /A AI2_X=%random% %%%Limit_X_Max% + 2
  145. Set /A AI2_Y=%random% %%%Limit_Y_Max% + 2
  146.  
  147. IF %X_Pos%==%AI_X% IF %Y_Pos%==%AI_Y% GOTO :startpos
  148. IF %X_Pos%==%AI2_X% IF %Y_Pos%==%AI2_Y% GOTO :startpos
  149. IF %AI2_X%==%AI_X% IF %AI2_Y%==%AI_Y% GOTO :startpos
  150.  
  151.     FOR %%A in (%X_Pos%,%AI_X%,%AI2_X%) Do (
  152.         IF %%A LSS %Limit_Axis_Min% GOTO :startpos
  153.         IF %%A GEQ %Limit_X_Max% GOTO :startpos
  154.     )
  155.    
  156.     FOR %%A in (%Y_Pos%,%AI_Y%,%AI2_Y%) Do (
  157.         IF %%A LSS %Limit_Axis_Min% GOTO :startpos 
  158.         IF %%A GEQ %Limit_Y_Max% GOTO :startpos
  159.     )
  160.  
  161. ::: REM Initialise Last Position variables
  162.  
  163. Set "L_Y_Pos=%Y_Pos%"
  164. Set "L_X_Pos=%X_Pos%"
  165. Set "L_AI_X=%AI2_X%"
  166. Set "L_AI_Y=%AI2_Y%"
  167. Set "L_AI2_X=%AI_X%"
  168. Set "L_AI2_Y=%AI_Y%"
  169.  
  170. :bomb
  171.  
  172. ::: REM Define Random Position For "Bomb" as loss condition, ensuring value within 'play field'
  173.  
  174. Set /a "X_B=%random% %%48 + 1"
  175. Set /a "Y_B=%random% %%28 + 1"
  176. IF %X_B% LSS 3 GOTO :bomb
  177. IF %Y_B% LSS 3 GOTO :bomb
  178.  
  179.     IF %X_B%==%X_Pos% (
  180.         IF %Y_B%==%Y_Pos% (
  181.             GOTO :bomb
  182.         )
  183.     )
  184.  
  185. ::: REM Define Random Position For "Escape" as win condition, ensuring value within 'play field', and Different to "bomb" position
  186.  
  187. :escape
  188. Set /a "X_Esc=%random% %%48 + 1"
  189. Set /a "Y_Esc=%random% %%28 + 1"
  190. IF %X_Esc% LSS 3 (GOTO :escape)
  191. IF %Y_Esc% LSS 3 (GOTO :escape)
  192.  
  193.     IF %X_Esc%==%X_Pos% (
  194.         IF %Y_Esc%==%Y_Pos% (
  195.             GOTO :escape
  196.         )
  197.     )
  198.  
  199.     IF %X_Esc%==%X_B% (
  200.         IF %Y_Esc%==%Y_B% (
  201.             GOTO :escape
  202.         )
  203.     )
  204.  
  205. ::: REM Call refresh Function to Display initial Position, and Update Position after Movement within 'move' loop, Update Last X and Y Positions prior to Movement
  206.  
  207. :Move
  208. %@POS% refresh
  209. Endlocal
  210. %@COL% ENEMY
  211. ECHO([%AI_Y%;%AI_X%H%ENEMY%
  212. Endlocal
  213. %@COL% ENEMY
  214. ECHO([%AI2_Y%;%AI2_X%H%ENEMY%
  215. Endlocal
  216. %@REFRESH% screen
  217. Endlocal
  218.  
  219. Set "L_AI_X=%AI_X%"
  220. Set "L_AI_Y=%AI_Y%"
  221. Set "L_AI2_X=%AI2_X%"
  222. Set "L_AI2_Y=%AI2_Y%"
  223. Set "L_Y_Pos=%Y_Pos%"
  224. Set "L_X_Pos=%X_Pos%"
  225.  
  226. ECHO([%instructions%;1H Escape the Ampersand. [W A S D]
  227.  
  228. REM :: Default switch results in a Free Move for the AI
  229.  
  230. CHOICE /T 1 /N /C wasd0 /M "" /D 0 >nul
  231. CALL :Direction%ERRORLEVEL%
  232.  
  233. Set /a AI_Move=%random% %%2 +1
  234. Set /a AI2_Move=%random% %%2 +1
  235. CALL :AImove%AI_Move%
  236. CALL :AI2move%AI2_Move%
  237.  
  238. GOTO :Move
  239.  
  240. :AImove1
  241.    
  242.     IF %AI_X%==%X_Pos% (
  243.         IF %AI_Y% LSS %Y_POS% (Set /a "AI_Y+=1")
  244.         IF %AI_Y% GTR %Y_POS% (Set /a "AI_Y-=1")
  245.     ) else (
  246.         IF %AI_X% LSS %X_POS% (Set /a "AI_X+=1")
  247.         IF %AI_X% GTR %X_POS% (Set /a "AI_X-=1")
  248.     )
  249.     EXIT /B 0
  250.  
  251. :AI2move1
  252.  
  253.     IF %AI2_X%==%X_Pos% (
  254.         IF %AI2_Y% LSS %Y_POS% (Set /a "AI2_Y+=1")
  255.         IF %AI2_Y% GTR %Y_POS% (Set /a "AI2_Y-=1")
  256.     ) else (
  257.         IF %AI2_X% LSS %X_POS% (Set /a "AI2_X+=1")
  258.         IF %AI2_X% GTR %X_POS% (Set /a "AI2_X-=1")
  259.     )
  260.     EXIT /B 0
  261.  
  262. :AImove2
  263.  
  264. ::: REM Y Pos Equal Comparison Disabled to Balance AI Tracking Ability. Remove ::: In Function to Make AI more Challenging
  265.  
  266.     IF %AI_Y%==%Y_Pos% (
  267.         IF %AI_X% LSS %X_POS% (Set /a "AI_X+=1")
  268.         IF %AI_X% GTR %X_POS% (Set /a "AI_X-=1")
  269.     ) else (
  270.         IF %AI_Y% LSS %Y_POS% (Set /a "AI_Y+=1")
  271.         IF %AI_Y% GTR %Y_POS% (Set /a "AI_Y-=1")
  272.     )
  273.     EXIT /B 0
  274.  
  275. :AI2move2
  276.  
  277.     IF %AI2_Y%==%Y_Pos% (
  278.         IF %AI2_X% LSS %X_POS% (Set /a "AI2_X+=1")
  279.         IF %AI2_X% GTR %X_POS% (Set /a "AI2_X-=1")
  280.     ) else (
  281.         IF %AI2_Y% LSS %Y_POS% (Set /a "AI2_Y+=1")
  282.         IF %AI2_Y% GTR %Y_POS% (Set /a "AI2_Y-=1")
  283.     )
  284.     EXIT /B 0
  285.  
  286. REM :: AI Advance
  287. :Direction5
  288.     EXIT /B 0
  289.  
  290. REM :: Right    Test If at Rightmost Limit of X axis before allowing Movement, By Increasing Y axis count by one.
  291. :Direction4
  292.  
  293.     IF NOT %X_Pos%==%Limit_X_Max% (Set /a "X_Pos+=1")
  294.     EXIT /B 0
  295.  
  296. REM :: Down Test If at Lower Limit of Y axis before allowing Movement, By Increasing Y axis count by one.
  297. :Direction3
  298.  
  299.     IF NOT %Y_Pos%==%Limit_Y_Max% (Set /a "Y_Pos+=1")
  300.     EXIT /B 0
  301.  
  302. REM :: Left Test If at Leftmost Limit of X axis before allowing Movement, By reducing X axis count by one.
  303. :Direction2
  304.  
  305.     IF NOT %X_Pos%==%Limit_Axis_Min% (Set /a "X_Pos-=1")
  306.     EXIT /B 0
  307.    
  308. REM :: Up.  Test If at Upper Limit of Y axis before allowing Movement, By reducing Y axis count by one.
  309. :Direction1
  310.  
  311.     IF NOT %Y_Pos%==%Limit_Axis_Min% (Set /a "Y_Pos-=1")
  312.     EXIT /B 0
  313.  
  314. :end
  315.     ECHO(%G_over%
  316.     ECHO([E]xit [R]eplay
  317. CHOICE /N /C re /M "" >nul
  318.     IF %ERRORLEVEL%==2 (EXIT)
  319.  
  320.     ENDLOCAL & GOTO :start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement