{******************************************************************************* Program to bounce animated balls around screen *******************************************************************************} program ball5(input, output); uses gralib; label 99; const ballsize = 81; halfball = ballsize div 2; maxball = 10; reprate = 1; { 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 } rndseq: integer; { random sequence seed } rc: integer; { repetition counter } procedure chkbrk; var er: evtrec; { event record } begin repeat event(input, er) until (er.etype = etframe) 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; procedure movball(b: balinx); var nx, ny: integer; { temp coordinates holders } begin with baltbl[b] do begin 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 end 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; auto(output, false); curvis(output, false); { turn off cursor } cd := false; { set 1st display } rc := 0; { count reps } frame(output, true); { turn on the framing timer } while true do begin { select display and update surfaces } select(output, ord(not cd)+1, ord(cd)+1); { erase old ball positions } fover(output); for i := 1 to maxball do with baltbl[i] do drawball(white, lx, ly); fxor(output); { move balls to new positions } for rc := 1 to reprate do { repetitions per frame } for i := 1 to maxball do with baltbl[i] do movball(i); for i := 1 to maxball do with baltbl[i] do drawball(c, x, y); cd := not cd; { flip display and update surfaces } chkbrk { check complete } end; 99: end.