Advertisement
IcarusLives

In-line - Bouncing balls v1.2 MACROS

Jun 9th, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.04 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2.  
  3. :: create array of lines height(40) with a string length of width(50)
  4.     set /a "width=50 - 1", "height=40 - 1", "conWidth=width + 5", "conHeight=height + 6", "_=0"
  5.     for /l %%a in (-2,1,%width%) do set "outerBuffer=!outerBuffer!#"
  6.     for /l %%a in (0,1,%width%)  do set "widthBuffer=!widthBuffer! "
  7.     for /l %%a in (0,1,%height%) do set "_[%%a]=%widthBuffer%"
  8.     mode con: cols=%conWidth% lines=%conHeight%
  9.  
  10. :: create %balls% number of ball objects.
  11.     set "balls=5"
  12.  
  13.     for /l %%b in (1,1,%balls%) do (
  14.         rem r = radius
  15.         set /a "r[%%b]=!random! %% 5 + 2"
  16.         rem x position
  17.         set /a "x[%%b]=!random! %% width + r[%%b]"
  18.         rem y position
  19.         set /a "y[%%b]=!random! %% height + r[%%b]"
  20.         rem i = x direction
  21.         set /a "i[%%b]=!random! %% 3 + 1"
  22.         rem j = y direction
  23.         set /a "j[%%b]=!random! %% 2 + 1"
  24.         rem c = character
  25.         set /a "c[%%b]=%%b"
  26.         rem bounce limits
  27.         set /a "bmaxW[%%b]=width - r[%%b]"
  28.         set /a "bmaxH[%%b]=height - r[%%b]"
  29.         set /a "bmin[%%b]=r[%%b]"
  30.     )
  31.  
  32. :: create %plot% and %circle% macro functions.--------------------------------------------------------
  33. set ^"LF=^
  34.  
  35. ^" Above empty line is required - do not remove
  36. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  37.  
  38. set plot=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-3" %%1 in ("^!args^!") do (%\n%
  39.     set /a "_y=%%2", "_x=%%1", "_x2=%%1 + 1"%\n%
  40.     for /f "tokens=1-4" %%a in ("_y ^!_y^! ^!_x^! ^!_x2^!") do set "_[^!%%a^!]=^!_[%%b]:~0,%%c^!%%3^!_[%%b]:~%%d^!"%\n%
  41. )) else set args=
  42.  
  43. :: %CIRCLE%
  44. set circle=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
  45.         for /l %%y in (-%%3,1,%%3) do for /l %%x in (-%%3,1,%%3) do (%\n%
  46.             set /a "S=(%%x * %%x) + (%%y * %%y) - (%%3 * %%3)", "_3div2=%%3 / 2"%\n%
  47.             for /f "tokens=1,2" %%a in ("^!S^! ^!_3div2^!") do (%\n%
  48.                        if "%%5" equ "/f" ( if %%a leq 1    ^!plot^! %%x+%%1 %%y+%%2 %%4 %\n%
  49.                 ) else if "%%5" equ "/n" ( if %%a geq -%%3 ^!plot^! %%x+%%1 %%y+%%2 %%4 %\n%
  50.                 ) else if %%a geq -%%3 if %%a leq %%b      ^!plot^! %%x+%%1 %%y+%%2 %%4 %\n%
  51.         ))%\n%
  52.         set "s="%\n%
  53. )) else set args=
  54. :: end of macros -------------------------------------------------------------------------------------
  55.  
  56.  
  57.  
  58.  
  59. rem infinite loop of bouncing balls
  60. for /l %%# in () do ( for /l %%b in (1,1,%balls%) do (
  61.         set /a "x[%%b]+=i[%%b]"
  62.         set /a "y[%%b]+=j[%%b]"
  63.        
  64.         if !x[%%b]! geq !bmaxW[%%b]! set /a "x[%%b]=bmaxW[%%b]", "i[%%b]*=-1"
  65.         if !y[%%b]! geq !bmaxH[%%b]! set /a "y[%%b]=bmaxH[%%b]", "j[%%b]*=-1"
  66.         if !x[%%b]! leq !bmin[%%b]!  set /a "x[%%b]=bmin[%%b]",  "i[%%b]*=-1"
  67.         if !y[%%b]! leq !bmin[%%b]!  set /a "y[%%b]=bmin[%%b]",  "j[%%b]*=-1"
  68.        
  69.         %plot%   !x[%%b]! !y[%%b]! !c[%%b]!
  70.         %circle% !x[%%b]! !y[%%b]! !r[%%b]! .
  71.     )
  72.     cls
  73.         echo= %outerBuffer%
  74.         for /l %%a in (0,1,%height%) do echo= #!_[%%a]!#
  75.         echo= %outerBuffer%
  76.     for /l %%a in (0,1,%height%) do set "_[%%a]=%widthBuffer%"
  77. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement