Here, i modified your code so that it only draws a filled circle only once and the grabs the image of the circle, it makes drawing filled circles faster. I had to add a function for adding a ball, so you cant really just change the ballcount variable and make a new ball, you actually have to call the addBall() function.
set display mode 1024,768,16
sync rate 30
// TYPES ////
type BallObject
x as float
y as float
xvel as float
yvel as float
xdir as integer
ydir as integer
spd as float
size as integer
color as integer
inmotion as integer `whether or not the space key has been pressed... yeah i know
endtype
// GLOBALS ////
e# = .75 `coefficient of restitution
global balllimit = 100
global ballcount = 0
screenwidth = screen width( )
screenheight = screen height( )
leftclicked = 0
// INITIALIZE ////
set text font "arial"
set text size 12
dim balls(balllimit) as BallObject
// MAIN LOOP ////
addBall()
balls(0).x=screenwidth/2
balls(0).y=screenheight/2
while not escapekey( )
cls
`input
for i = 0 to ( ballcount )
if spacekey( ) and balls(i).inmotion = 0
balls(i).x = screen width( ) / 2
balls(i).y = screen height( ) / 2
balls(i).xdir = rnd(1) + 1
balls(i).ydir = rnd(1) + 1
balls(i).xvel = rnd(29) + 1
balls(i).yvel = rnd(29) + 1
if balls(i).xdir > 1 then balls(i).xdir = -( balls(i).xdir - 1 )
if balls(i).ydir > 1 then balls(i).ydir = -( balls(i).ydir - 1 )
balls(i).inmotion = 1
else
if not spacekey( ) then balls(i).inmotion = 0
endif
`update
`wall collisions detection and response
if balls(i).x + ( balls(i).xvel * balls(i).xdir ) > screen width( ) - balls(i).size
balls(i).xdir = -balls(i).xdir
balls(i).yvel = balls(i).yvel * e#
balls(i).xvel = balls(i).xvel * e#
endif
if balls(i).y + ( balls(i).yvel * balls(i).ydir ) > screen height( ) - balls(i).size
balls(i).ydir = -balls(i).ydir
balls(i).xvel = balls(i).xvel * e#
balls(i).yvel = balls(i).yvel * e#
else
balls(i).yvel = balls(i).yvel + .4 * balls(i).ydir
endif
if balls(i).x + ( balls(i).xvel * balls(i).xdir ) < balls(i).size
balls(i).xdir = -balls(i).xdir
balls(i).yvel = balls(i).yvel * e#
balls(i).xvel = balls(i).xvel * e#
endif
if balls(i).y + ( balls(i).yvel * balls(i).ydir ) < balls(i).size
balls(i).ydir = -balls(i).ydir
balls(i).xvel = balls(i).xvel * e#
balls(i).yvel = balls(i).yvel * e#
endif
`move ball
balls(i).x = balls(i).x + ( balls(i).xvel * 2 * balls(i).xdir )
balls(i).y = balls(i).y + ( balls(i).yvel * 2 * balls(i).ydir )
`draw image
if image exist(i+1)
paste image i+1,balls(i).x-balls(i).size,balls(i).y-balls(i).size,1
endif
next i
`more input
mousezdir = mousemovez( ) / 120
if ballcount > 1 and mousezdir <= -1 then ballcount = ballcount + mousezdir
if ballcount < balllimit - 1 and mousezdir >= 1 then ballcount = ballcount + mousezdir
if mouseclick( ) = 1
if ballcount < balllimit - 1 and leftclicked = 0
addBall()
leftclicked = 1
endif
else
leftclicked = 0
endif
`more drawing
ink rgb(40,220,0), rgb(0,0,0)
print "FPS: " + str$(screen fps( ))
print ""
print "o Simple bouncing ball physics o"
print ""
print "Control Legend:"
print "Space - Randomize Balls"
print "Left Click - Drop Ball"
print "Mouse Wheel - Increase/Decrease Ball Amount"
endwhile
`end of the program
end
function addBall()
balls(ballcount).x = mousex( )
balls(ballcount).y = mousey( )
balls(ballcount).xvel = 0
balls(ballcount).yvel = 0
balls(ballcount).xdir = 1
balls(ballcount).ydir = 1
balls(ballcount).spd = 0
balls(ballcount).size = rnd(16) + 16
balls(ballcount).color = rgb(20+rnd(235),20+rnd(235),20+rnd(235))
balls(ballcount).inmotion = 0
`draw
cls
ink balls(ballcount).color, rgb(0,0,0)
ellipse balls(ballcount).size, balls(ballcount).size, balls(ballcount).size, balls(ballcount).size
fillArea(balls(ballcount).size/2, balls(ballcount).size/2, 0)
if image exist(ballcount+1) then delete image ballcount+1
hs=balls(ballcount).size
get image ballcount+1,0,0,balls(ballcount).size*2, balls(ballcount).size*2
ballcount = ballcount + 1
endfunction
`this function fills a point if it is the right color
`if it is the right color than it also calls itself for the surrounding points
function fillArea(x,y,col)
`check for a diffrent color
col2=point(x,y)
col2r=rgbr(col2)
col2g=rgbg(col2)
col2b=rgbb(col2)
colr=rgbr(col)
colg=rgbg(col)
colb=rgbb(col)
if colr<>col2r and colg<>col2g and colb<>col2b then exitfunction
`output
dot x,y
`recursive calls
fillArea(x+1,y,col)
fillArea(x-1,y,col)
fillArea(x,y+1,col)
fillArea(x,y-1,col)
endfunction