View.Set ("graphics:600;400,offscreenonly")
% VARIABLES
var Color : int
var x1, y1, dx1, dy1, x2, y2, dy2, dx2 : int
var count : int := 0
var tempx, tempy : int
% TOP AND BOTTOM BORDER
for i : 1 .. maxx by 20
Color := Rand.Int (1, 60)
if Color = 31 then
Color := Rand.Int (1, 60)
end if
Draw.FillStar (i, 0, i + 20, 20, Color)
Draw.FillStar (i, maxy  20, i + 20, maxy, Color)
end for
% LEFT AND RIGHT BORDER
for k : 20 .. maxy  20 by 20
Color := Rand.Int (1, 60)
if Color = 31 then
Color := Rand.Int (1, 60)
end if
Draw.FillStar (0, k, 20, k + 20, Color)
Draw.FillStar (maxx  20, k, maxx, k + 20, Color)
end for
x1 := maxx  41
y1 := maxy  61
x2 := maxy  41
y2 := maxy  41
dx1 := 4
dy1 := 0
dx2 := 0
dy2 := 4
% COLLISION DETECTION
function collided (x1, x2, y1, y2, radius : int) : boolean
for xCheck : x1  radius .. x1 + radius
if xCheck >= x2  radius and xCheck <= x2 + radius then
for yCheck : y1  radius .. y1 + radius
if yCheck >= y2  radius and yCheck <= y2 + radius then
result (true)
end if
end for
end if
end for
result (false)
end collided
% WHEN TO BOUNCE OFF WALL
loop
x1 := x1 + dx1
x2 := x2 + dx2
y1 := y1 + dy1
y2 := y2 + dy2
if x1 > maxx  41 or x1 < 41 then
dx1 := dx1
end if
if y1 > maxy  41 or y1 < 41 then
dy1 := dy1
end if
if x2 > maxx  41 or x2 < 41 then
dx2 := dx2
end if
if y2 > maxy  41 or y2 < 41 then
dy2 := dy2
end if
% WHEN THEY COLLIDE BALLS BOUNCE IN DIFFERENT DIRECTIONS
if collided (x1, x2, y1, y2, 20) then
tempx := dx1
tempy := dy1
dx1 := dx1
dy1 := dy1
dx1 := round (dx1 / 2 + dx2 / 2)
dy1 := round (dy1 / 2 + dy2 / 2)
dx2 := dx2
dy2 := dy2
dx2 := round (dx2 / 2 + tempx / 2)
dy2 := round (dy2 / 2 + tempy / 2)
count := count + 1
end if
locate (maxrow 2,4)
colour (blue)
put "Collisions : ", count ..
colour (black)
% THE BALLS
Draw.FillOval (x1, y1, 20, 20, blue)
Draw.FillOval (x2, y2, 20, 20, black)
View.Update
delay (10)
Draw.FillOval (x1, y1, 20, 20, 0)
Draw.FillOval (x2, y2, 20, 20, 0)
end loop
