Bouncing balls 2

Continuing our series of short animation programs, or as I will refer to them now - 'programs written while I wait for my tea to cook' - it is a simple hack to change yesterday's program to make the balls bounce off the walls rather than just the floor.  No longer acted on by gravity, the balls are initialized at random positions with random velocities and float around in space like some sort of cosmic blue-green bubbles.


Again, the keen student will want to change the parameters for more fun.

Come back soon and I'll have the balls bouncing off each other as well.

These programs are small enough to run in the free version of BB4Win, but contact me if you require the Windows executable versions.

Bouncy balls particle system.  Balls now bounce off the walls of the screen.

Source code follows:

     REM bouncing balls 2
     REM T Street
     REM 2014-11-25
     
MODE 10  : OFF
     
REM screen dimensions
     
SCREEN_WIDTH% = 1440
     SCREEN_HEIGHT% = 1152

     REM --------------------------------------------------
     REM CHANGE THESE PARAMETERS FOR MORE FUN
     REM --------------------------------------------------
     
MAX_BALL_SIZE% = 70 : REM max size of a ball

     REM don't set too high unless you have a fast computer
     
NUM_BALLS% = 30

    
 MAX_SPEED% = 20
     DELAY% = 7
     REM --------------------------------------------------


     REM some colours
     
COLOUR 1, 0, 0, 0 : REM black

     REM the ball object
     
DIM ball{( NUM_BALLS% -1) x, y,      \ coordinates
     
\                         r%,        \ ball radius
     
\                         dx, dy,    \ component of velocity
     
\                         green%,    \ amount of green
     
\                         blue%      \ amounf of blue
     
\ }


     PROC_createBalls( ball{()} )

     REM MAIN LOOP
     
REPEAT
       PROC
_showBalls( ball{()} )
       WAIT DELAY%
       PROC_deleteBalls( ball{()} )
       PROC_moveBalls( ball{()} )
     UNTIL FALSE




     
DEFPROC_createBalls( this{()} )
     REM give each ball a random position and velocity
     
LOCAL i%
     FOR i% = 0 TO NUM_BALLS% - 1
       this{(i%)}.x = RND(SCREEN_WIDTH%)
       this{(i%)}.y = RND(SCREEN_HEIGHT%)
       this{(i%)}.dx = RND(MAX_SPEED%)-(MAX_SPEED% DIV 2)
       this{(i%)}.dy = RND(MAX_SPEED%)-(MAX_SPEED% DIV 2)
       this{(i%)}.r% = 10 + RND(MAX_BALL_SIZE%)
       this{(i%)}.green% = RND(255)
       this{(i%)}.blue% = RND(255)
     NEXT
     ENDPROC


     
DEFPROC_showBalls( this{()} )
     REM draw all the balls on the screen
     
LOCAL i%
     *refresh off
     FOR i% = 0 TO NUM_BALLS%-1
       GCOL 0,1
       CIRCLE FILL this{(i%)}.x, this{(i%)}.y, this{(i%)}.r%
       REM get colour for ball
       
COLOUR 2, 0, this{(i%)}.green%, this{(i%)}.blue%
       GCOL 0, 2
       CIRCLE FILL this{(i%)}.x, this{(i%)}.y, this{(i%)}.r% - 4
     NEXT
     
*refresh on
     ENDPROC


     
DEFPROC_deleteBalls( this{()} )
     REM remove all the balls on the screen
     
LOCAL i%
     *refresh off
     FOR i% = 0 TO NUM_BALLS%-1
       GCOL 0,1
       CIRCLE FILL this{(i%)}.x, this{(i%)}.y, this{(i%)}.r%
       REM get colour for ball
     
NEXT
     
*refresh on
     ENDPROC


     
DEFPROC_moveBalls( this{()} )
     REM move the balls
     
LOCAL i%
     FOR i% = 0 TO NUM_BALLS%-1
       this{(i%)}.y += (this{(i%)}.dy)
       this{(i%)}.x += (this{(i%)}.dx)

       REM check for going off the screen
       
IF this{(i%)}.y < 0 OR this{(i%)}.y > SCREEN_HEIGHT% THEN
         
REM bounce!
         
this{(i%)}.dy = this{(i%)}.dy * -1
       ENDIF

       IF 
this{(i%)}.x < 0 OR this{(i%)}.x > SCREEN_WIDTH% THEN
         
REM bounce!
         
this{(i%)}.dx = this{(i%)}.dx * -1
       ENDIF

     NEXT
     ENDPROC