Advertisement
IcarusLives

Object oriented - Bouncing balls

Mar 24th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.71 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2.  
  3. call :canvas 50 40
  4.  
  5. set "balls=6"
  6.  
  7. for /l %%b in (1,1,%balls%) do ( call :generate_balls %%b )
  8.  
  9. for /l %%# in () do ( for /l %%b in (1,1,%balls%) do (
  10.         call :moveObjects %%b
  11.         call :edges %%b
  12.         call :plot !x[%%b]! !y[%%b]! !c[%%b]!
  13.         call :circle !x[%%b]! !y[%%b]! !r[%%b]! .
  14.     )
  15.     call :showCanvas
  16.     call :updateCanvas
  17. )
  18.  
  19. :moveObjects
  20.     set /a "x[%1]+=i[%1]"
  21.     set /a "y[%1]+=j[%1]"
  22. goto :eof
  23.  
  24. :edges
  25.         if !x[%1]! geq !bmaxW[%1]! set /a "x[%1]=bmaxW[%1]", "i[%1]*=-1"
  26.         if !y[%1]! geq !bmaxH[%1]! set /a "y[%1]=bmaxH[%1]", "j[%1]*=-1"
  27.         if !x[%1]! leq !bmin[%1]!  set /a "x[%1]=bmin[%1]",  "i[%1]*=-1"
  28.         if !y[%1]! leq !bmin[%1]!  set /a "y[%1]=bmin[%1]",  "j[%1]*=-1"
  29. goto :eof
  30.  
  31. :generate_balls
  32.     set /a "r[%1]=3"
  33.     set /a "x[%1]=!random! %% width + r[%1]"
  34.     set /a "y[%1]=!random! %% height + r[%1]"
  35.     set /a "i[%1]=!random! %% 3 + 1"
  36.     set /a "j[%1]=!random! %% 2 + 1"
  37.     set /a "c[%1]=%1"
  38.     set /a "bmaxW[%1]=width - r[%1]"
  39.     set /a "bmaxH[%1]=height - r[%1]"
  40.     set /a "bmin[%1]=r[%1]"
  41. goto :eof
  42.  
  43. :canvas
  44.         set /a "width=%~1 - 1", "height=%~2 - 1", "conWidth=width + 5", "conHeight=height + 6", "_=0"
  45.         for /l %%a in (-2,1,%width%) do set "outerBuffer=!outerBuffer!#"
  46.         for /l %%a in (0,1,%width%)  do set "widthBuffer=!widthBuffer! "
  47.         call :updateCanvas
  48.         call :cursorpos
  49.         if exist "%temp%\cursorpos.exe" ( set "cls="%temp%\cursorpos.exe" 0 1" ) else ( set "cls=cls" )
  50.         mode con: cols=%conWidth% lines=%conHeight%
  51.     goto :eof
  52.    
  53.     :updateCanvas
  54.         for /l %%a in (0,1,%height%) do set "_[%%a]=%widthBuffer%"
  55.     goto :eof
  56.    
  57.     :showCanvas
  58.         %cls%
  59.         echo= %outerBuffer%
  60.         for /l %%a in (0,1,%height%) do echo= #!_[%%a]!#
  61.         echo= %outerBuffer%
  62.         if "%~1" equ "/u" for /l %%a in (0,1,%height%) do set "_[%%a]=%widthBuffer%"
  63.     goto :eof
  64. goto :eof
  65.  
  66. :plot x y
  67.     setlocal
  68.         set "c=%~3"
  69.         set /a "_y=%~2", "_x=%~1", "_x2=%~1 + 1"
  70.         (endlocal
  71.             set "_[%_y%]=!_[%_y%]:~0,%_x%!%c:~0,1%!_[%_y%]:~%_x2%!"
  72.         )
  73. goto :eof
  74.  
  75. :circle x y r
  76.     set /a "___rr=%~3"
  77.         for /l %%y in (-%___rr%,1,%___rr%) do for /l %%x in (-%___rr%,1,%___rr%) do (
  78.            
  79.             set /a "t=-___rr - 1", "S=(%%x * %%x) + (%%y * %%y) - (___rr * ___rr) - t - 1"
  80.             if !S! geq !t! if !S! leq 1 (  
  81.                 set /a "_x=%%x + %~1", "_y=%%y + %~2"
  82.                 set /a "_x2=_x + 1"
  83.                 call set "_[!_y!]=%%_[!_y!]:~0,!_x!%%%~4%%_[!_y!]:~!_x2!%%"
  84.             )  
  85.         )
  86.         for %%a in (t S _x _y _x2 ___rr) do set "%%a="
  87. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement