Bouncing Balls
Bouncing Balls
program Balls;
uses Types, QuickDraw, Events, MiscTool;
type
Ball = Record
x, y, sx, sy: Integer
end;
var
i: Integer;
tick: LongInt;
r: Rect;
b: Ball;
balls: Array[1..15] of Ball;
begin
graphics(320);
clearScreen(black);
hideCursor;
tick := getTick;
setPenSize(3, 3);
{ Setup balls }
for i := 1 to 15 do begin
balls[i].x := random mod 300 + 10;
balls[i].y := random mod 180 + 10;
if random > 0 then
balls[i].sx := random mod 2 + 1
else balls[i].sx := -(random mod 2 + 1);
if random > 0 then
balls[i].sy := random mod 2 + 1
else balls[i].sy := -(random mod 2 + 1);
end;
repeat
for i := 1 to 15 do begin
b := balls[i];
{ Draw balls }
setRect(r, b.x, b.y, b.x + 5 + i,
b.y + 5 + i);
setSolidPenPat(i);
paintOval(r);
setRect(r, b.x - 2, b.y - 2, b.x + 7 + i,
b.y + 7 + i);
setSolidPenPat(black);
frameOval(r);
{ Move balls }
balls[i].x := b.x + b.sx;
balls[i].y := b.y + b.sy;
if (balls[i].x < 0) or (balls[i].x > 310)
then balls[i].sx := -b.sx;
if (balls[i].y < 0) or (balls[i].y > 190)
then balls[i].sy := -b.sy;
end;
repeat until getTick > tick;
tick := tick + 1;
until button(0);
end.
Many bouncing balls.
This program teaches the use of array to create multiple objects.
You can see the alternate Orca/Pascal and GSoft BASIC versions here. Orca/Pascal version is written using Object Pascal, an object oriented extension of the Pascal language.