IcarusLives

Breakout Batch Arcade game

Mar 12th, 2017
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.02 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2. if "%~1" neq "" goto %~1
  3.  
  4. call :canvas 40 35
  5.  
  6. :: generate ball
  7. call :generate_ball
  8.  
  9. :: generate self
  10. set /a "mx=14", "my=height - 5", "mxl=mx + 9"
  11. set /a "lives=5"
  12.  
  13. :: generate_objects
  14. set "list="7,5" "13,5" "19,5" "25,5" "31,5" "7,7" "13,7" "19,7" "25,7" "31,7" "7,11" "13,11" "19,11" "25,11" "31,11" "win""
  15.  
  16. if exist %temp%\out.txt del %temp%\out.txt
  17. "%~F0" Input >%temp%\out.txt | "%~F0" Engine <%temp%\out.txt
  18. goto :eof
  19.  
  20. :Engine
  21.  
  22.     for /l %%. in () do (
  23.        rem pick up input from out.txt
  24.         set "com=" & set /p "com="
  25.        
  26.         rem use player input
  27.         if /i "!com!" equ "A" if !mx!  geq 2       set /a "mx-=2"
  28.         if /i "!com!" equ "D" if !mxl! lss %width% set /a "mx+=2"
  29.  
  30.         rem move the ball, and myself
  31.         set /a "bx+=bi","_x2=bx + 1"
  32.         set /a "by+=bj", "mxl=mx + 9"
  33.        
  34.         rem Bounce off top, bottom, and right walls
  35.         if !bx! geq %width%  set /a "bx=width",  "bi*=-1"
  36.         if !bx! leq 0        set /a "bx=0",      "bi*=-1"
  37.         if !by! leq 0        set /a "by=0",      "bj*=-1"
  38.        
  39.         rem Detect if we miss the ball
  40.         if !by! gtr %height% (
  41.             call :generate_ball
  42.             set /a "lives-=1"
  43.             if !points! geq 0 set /a "points-=5"
  44.             if !lives! equ 0 (
  45.                 cls & for /l %%a in (1,1,17) do echo.
  46.                 echo                   You lose! & pause > nul & exit
  47.             )
  48.         )
  49.        
  50.         rem Detect if we hit the ball
  51.         if !by! equ !my! if !bx! geq !mx! if !bx! leq !mxl! set /a "bj*=-1", "points+=1"
  52.        
  53.         rem Detect if it hits object
  54.         if !by! leq 12 (
  55.             set "newList="
  56.             for %%a in (!list!) do for /f "tokens=1,2 delims=," %%0 in ("%%~a") do (
  57.                 if !bx! equ %%0 if !by! equ %%1 (
  58.                     call set "_[!by!]=%%_[!by!]:~0,!bx!%% %%_[!by!]:~!_x2!%%"
  59.                     set /a "bj*=-1", "points+=3"
  60.                     set "newList=!list:"%%0,%%1"=!"
  61.                 )
  62.             )
  63.             if defined newList set "list=!newList!"
  64.         )
  65.  
  66.         rem Detect if we hit all the objects
  67.         if "!list!" equ "win" (
  68.             cls
  69.             for /l %%a in (1,1,17) do echo.
  70.             echo                   You Win!& echo.
  71.             echo                  SCORE: !points!
  72.             pause > nul & exit
  73.         )
  74.        
  75.         rem Place leftover Objects
  76.         for %%a in (!list!) do for /f "tokens=1,2 delims=," %%0 in ("%%~a") do (
  77.             set /a "_x4=%%0 + 1"
  78.             call set "_[%%1]=%%_[%%1]:~0,%%0%%X%%_[%%1]:~!_x4!%%"
  79.         )
  80.        
  81.         rem This is ball!
  82.         call set "_[!by!]=%%_[!by!]:~0,!bx!%%O%%_[!by!]:~!_x2!%%"
  83.  
  84.         rem This is me!
  85.         call set "_[!my!]=%%_[!my!]:~0,!mx!%%---------%%_[!my!]:~!mxl!%%"
  86.        
  87.         call :showCanvas
  88.         call :updateCanvas
  89.         echo= A/D - Move   Points = !points!    Lives = !lives!
  90.     )
  91. exit /b
  92.  
  93. rem collect user input
  94. :Input
  95.     for /f "tokens=*" %%a in ('choice /c:ad /n') do echo %%a
  96. goto :Input
  97.  
  98. :generate_ball
  99.     set /a "bx=19", "by=13", "bi=(!random! %% 2) - 1", "bj=(!random! %% 2) - 1"
  100.     set /a "cfi=!random! %% 2 + 1", "cfj=!random! %% 2 + 1"
  101.     if !bj! equ 0 if !cfj! equ 2 ( set /a "bj-=1" ) else ( set /a "bj+=1" )
  102.     if !bi! equ 0 if !cfi! equ 2 ( set /a "bi-=1" ) else ( set /a "bi+=1" )
  103. goto :eof
  104.  
  105. :canvas
  106.         set /a "width=%~1 - 1", "height=%~2 - 1", "conWidth=width + 5", "conHeight=height + 6"
  107.         for /l %%a in (-2,1,%width%) do set "outerBuffer=!outerBuffer!#"
  108.         for /l %%a in (0,1,%width%)  do set "widthBuffer=!widthBuffer! "
  109.         call :updateCanvas
  110.         if exist "%temp%\cursorpos.exe" ( set "cls="%temp%\cursorpos.exe" 0 0") else ( set "cls=cls")
  111.         mode con: cols=%conWidth% lines=%conHeight%
  112.     goto :eof
  113.    
  114.     :updateCanvas
  115.             for /l %%a in (0,1,%height%) do set "_[%%a]=%widthBuffer%"
  116.     goto :eof
  117.    
  118.     :showCanvas
  119.         cls
  120.         echo. & echo= %outerBuffer%
  121.         for /l %%a in (0,1,%height%) do echo= #!_[%%a]!#
  122.         echo= %outerBuffer%
  123.     goto :eof
  124. goto :eof
Add Comment
Please, Sign In to add comment