' Bonkers Symphony Number 37.bas SmallBASIC 0.12.9 [B+=MGA] 2017-04-21
def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
gravity = 3
'balls
bR = 10
n = 80
speed = 12
dim x(n), y(n), a(n), c(n), rr(n), gg(n), bb(n), rd(n)
for i = 1 to n
x(i) = rand(xmax/2-10, xmax/2+10)
y(i) = rand(-200, 0)
rd(i) = rand(3, 20)
a(i) = pi/2 + pi/90 * rdir
rr(i) = rand(60, 100)
gg(i) = rand(60, 100)
bb(i) = rand(60, 100)
next
'pins
pR = 25
maxrow = 7
nP = maxrow * (maxrow + 1) * .5
pxo = xmax / (maxrow + 1) 'pin space along x
pyo = ymax / (maxrow + 1) 'pin spacing along y
dim px(nP), py(nP)
for row = 1 to maxrow
for col = 1 TO row
pidx +=1
px(pidx) = pxo*col+(maxrow - row) * .5*pxo
py(pidx) = pyo*row
next
next
clrMode = 1
while 1
k = inkey
if asc(k) = 32 then clrMode *= -1
if clrMode < 0 then cls
'draw pins
for i = 1 to nP
for r = pR to 1 step -1
color rgb(r/pR*255,r/pR*255, r/pR*255)
circle px(i), py(i), r filled
next
next
'calc collsions
for i = 1 to n
for j = 1 to nP
if sqr((x(i)-px(j))^2 + (y(i)-py(j))^2) < rd(i) + pR then
a(i) = atan2(y(i)-py(j), x(i)-px(j))
color 0
circle px(j), py(j), pR filled
sound 120 + py(j)/ ymax * 5000, px(j) / xmax * 55
exit for
end if
next
for j = i + 1 to n
if j <> i and c(j) <> 1 then
if sqr((x(i)-x(j))^2 + (y(i)-y(j))^2) < rd(i) + rd(j) then
a(i) = atan2(y(i)-y(j), x(i)-x(j))
a(j) = atan2(y(j)-y(i), x(j)-x(i))
c(i) = 1 : c(j) = 1
exit for
fi
fi
next
'update balls
dx = cos(a(i)) * speed
dy = sin(a(i)) * speed + gravity
a(i) = atan2(dy, dx)
x(i) = x(i) + cos(a(i)) * speed
y(i) = y(i) + sin(a(i)) * speed
if x(i) < rd(i) or x(i) > xmax + rd(i) or y(i) > ymax + rd(i) then
x(i) = rand(xmax/2-10, xmax/2+10)
y(i) = rand(-250, -bR)
a(i) = pi/2 + pi/90 * rdir
fi
if a(i) > 2 * pi then a(i) = a(i) - 2 * pi
if a(i) < 0 then a(i) = a(i) + 2 * pi
for r = rd(i) to 1 step -1
color rgb(255-rr(i)-150*r/rd(i), 255-gg(i)-150*r/rd(i), 255-bb(i)-150*r/rd(i))
circle x(i), y(i), r filled
next
c(i) = 0
next
showpage
delay 10
wend