{******************************************************************************* Program to bounce animated balls around screen *******************************************************************************} program balls(input, output); uses gralib; label 99; const ballsize = 21; halfball = ballsize div 2; maxball = 10; frametime = 156; { time between frames, 60 cycle refresh } reprate = 5; { number of moves per frame, should be low } type balrec = record { ball data record } x, y: integer; { current position } lx, ly: integer; { last position } xd, yd: integer; { deltas } c: color { color } end; balinx = 1..maxball; { index for balls } var cd: boolean; { current display flip select } baltbl: array [1..maxball] of balrec; { ball data table } i: balinx; { index for table } nx, ny: integer; { temp coordinates holders } rndseq: integer; { random sequence seed } rc: integer; { repetition counter } procedure chkbrk; var er: evtrec; { event record } begin repeat event(input, er) until (er.etype = ettim) or (er.etype = etterm); if er.etype = etterm then goto 99 end; procedure drawball(c: color; x, y: integer); begin fcolor(output, c); { set color } fellipse(output, x-halfball+1, y-halfball+1, x+halfball-1, y+halfball-1) end; function rand: integer; const a = 16807; m = 2147483647; var gamma: integer; begin gamma := a*(rndseq mod (m div a))-(m mod a)*(rndseq div (m div a)); if gamma > 0 then rndseq := gamma else rndseq := gamma+m; rand := rndseq end; begin rndseq := 1; { set random number generator inital to mid sequence } { initalize ball data } for i := 1 to maxball do with baltbl[i] do begin x := rand mod (maxxg(output)-ballsize)+halfball; y := rand mod (maxyg(output)-ballsize)+halfball; if rand mod 2 = 0 then xd := +1 else xd := -1; if rand mod 2 = 0 then yd := +1 else yd := -1; lx := x; { set last position to same } ly := y; c := color(rand mod 6+ord(red)) { set random color } end; curvis(output, false); { turn off cursor } cd := false; { set 1st display } { place balls on display } for i := 1 to maxball do drawball(baltbl[i].c, baltbl[i].x, baltbl[i].y); rc := 0; { count reps } timer(input, 1, frametime, true); { start frame timer for 60 cycle refresh } while true do begin { select display and update surfaces } select(output, ord(not cd)+1, ord(cd)+1); for i := 1 to maxball do with baltbl[i] do begin { process balls } drawball(white, lx, ly); { erase ball at old position } lx := x; { save last position } ly := y; nx := x+xd; { trial move ball } ny := y+yd; { check out of bounds and reverse direction } if (nx < halfball) or (nx > maxxg(output)-halfball+1) then xd := -xd; if (ny < halfball) or (ny > maxyg(output)-halfball+1) then yd := -yd; x := x+xd; { move ball } y := y+yd; drawball(c, x, y) { place ball at new position } end; cd := not cd; { flip display and update surfaces } rc := rc+1; { count reps } if rc >= reprate then begin chkbrk; { check complete } rc := 0 { clear rep counter } end end; 99: end.