setscreen ("offscreenonly")
type coords :
record
x : real
y : real
end record
const xdecay := 0.99
const ydecay := 0.99
const gravity := -0.1
const balls := 35
const speed := 5
const ballRadius := 15
const elasticity := 1.6
var ball : array 1 .. balls of coords
for i : 1 .. balls
ball (i).x := maxx div 2
ball (i).y := maxy div 2
end for
var dx, dy : real := 0
var oldx, oldy : int
var clr : array 1 .. balls of int
var mx, my, md : int
var holding : boolean := false
fcn checkIfHolding (x, y : int) : boolean
if Math.Distance (ball (1).x, ball (1).y, x, y) < ballRadius and md = 1 then
holding := true
result true
else
result false
end if
end checkIfHolding
proc updateBalls
mousewhere (mx, my, md)
if md = 1 then
if checkIfHolding (mx, my) or holding = true then
dx := 0
dy := 0
ball (1).x := mx
ball (1).y := my
else
holding := false
ball (1).x += dx
ball (1).y += dy
dx *= xdecay
if abs (dy) > 1 or ball (1).y > 1 then
dy += gravity
else
ball (1).y := 0
end if
dy *= ydecay
end if
else
if holding = true then
dx := mx - oldx
dy := my - oldy
end if
holding := false
ball (1).x += dx
ball (1).y += dy
dx *= xdecay
if abs (dy) > 1 or ball (1).y > 1 then
dy += gravity
else
ball (1).y := 0
end if
dy *= ydecay
end if
for i : 2 .. balls
ball (i).x -= (ball (i).x - ball (i - 1).x) / speed
ball (i).y -= (ball (i).y - ball (i - 1).y) / speed
end for
if ball (1).x < 0 or ball (1).x > maxx then
dx *= -xdecay
if ball (1).x < 0 then
ball (1).x := 0
else
ball (1).x := maxx
end if
end if
if ball (1).y < 0 then
dy *= -elasticity
if ball (1).y < 0 then
ball (1).y := 0
end if
end if
oldx := mx
oldy := my
end updateBalls
proc drawBalls
for decreasing i : balls .. 1
drawfilloval (round (ball (i).x), round (ball (i).y), ballRadius - i, ballRadius - i, clr (i))
end for
end drawBalls
proc makeColors (clrs : int)
var a : real := 0.6
for i : 1 .. clrs
%clr (i) := RGB.AddColor (i / clrs, i / clrs, i / clrs)
clr (i) := RGB.AddColor (a, a,a+ i / clrs)
end for
end makeColors
makeColors (balls)
loop
updateBalls
drawBalls
View.Update
delay (10)
cls
end loop
|