# Bouncing balls

A simple program in BB4W BASIC featuring some coloured bouncing balls.  Students will like to change the various parameters for more fun!  Will run in the free version of BB4W.

Change the size of the balls; the total number of balls; the height dropped; the strength of gravity and the 'bounciness' of the balls.

Source code below:

REM balls bouncing under gravity
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

Y_OFFSET% = 100 : REM the minimum distance from the ground

GRAVITY = 9.8 : REM strength of gravity

BOUNCINESS = 0.91 : REM how bouncy the balls are

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

\                         velocity , \ integer 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%-Y_OFFSET%)+Y_OFFSET%
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%)}.velocity)
this{(i%)}.velocity -= GRAVITY
REM check for going off the bottom of the screen

IF this{(i%)}.y < this{(i%)}.r% THEN

REM bounce!

this{(i%)}.velocity = ABS(this{(i%)}.velocity) * BOUNCINESS
ENDIF
NEXT
ENDPROC