{******************************************************************************* Program to bounce animated squares around screen *******************************************************************************} program squares(input, output); uses gralib; label 99; const squaresize = 81; halfsquare = squaresize div 2; maxsquare = 10; reprate = 1; { number of moves per frame, should be low } type balrec = record { square data record } x, y: integer; { current position } lx, ly: integer; { last position } xd, yd: integer; { deltas } c: color { color } end; balinx = 1..maxsquare; { index for squares } var cd: boolean; { current display flip select } baltbl: array [1..maxsquare] of balrec; { square 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 = etframe) or (er.etype = etterm); if er.etype = etterm then goto 99 end; procedure drawsquare(c: color; x, y: integer); begin fcolor(output, c); { set color } frect(output, x-halfsquare+1, y-halfsquare+1, x+halfsquare-1, y+halfsquare-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 movesquare(s: balinx); begin with baltbl[s] do begin nx := x+xd; { trial move square } ny := y+yd; { check out of bounds and reverse direction } if (nx < halfsquare) or (nx > maxxg(output)-halfsquare+1) then xd := -xd; if (ny < halfsquare) or (ny > maxyg(output)-halfsquare+1) then yd := -yd; x := x+xd; { move square } y := y+yd end end; begin rndseq := 1; { set random number generator inital to mid sequence } { initalize square data } for i := 1 to maxsquare do with baltbl[i] do begin x := rand mod (maxxg(output)-squaresize)+halfsquare; y := rand mod (maxyg(output)-squaresize)+halfsquare; 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 squares on display } for i := 1 to maxsquare do drawsquare(baltbl[i].c, baltbl[i].x, baltbl[i].y); frame(output, true); { start frame timer } while true do begin { select display and update surfaces } select(output, ord(not cd)+1, ord(cd)+1); { erase old square positions } fover(output); for i := 1 to maxsquare do with baltbl[i] do drawsquare(white, lx, ly); fxor(output); { save old positions } for i := 1 to maxsquare do with baltbl[i] do begin lx := x; { save last position } ly := y end; { move squares } for rc := 1 to reprate do { repeats per frame } for i := 1 to maxsquare do movesquare(i); { process squares } { draw squares } for i := 1 to maxsquare do with baltbl[i] do drawsquare(c, x, y); cd := not cd; { flip display and update surfaces } chkbrk { check complete } end; 99: end.