{******************************************************************************* Program to bounce animated square around screen *******************************************************************************} program square(input, output); uses gralib; label 99; const squaresize = 21; halfsquare = squaresize div 2; var x, y: integer; nx, ny: integer; lx, ly: integer; xd, yd: integer; er: evtrec; cd: boolean; { current display flip select } { wait time in 100 microseconds } procedure wait(t: integer); begin timer(input, 1, t, false); repeat event(input, er) until er.etype = ettim end; procedure chkbrk; begin timer(input, 1, 1, false); repeat event(input, er) until (er.etype = ettim) 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; begin curvis(output, false); { turn off cursor } select(output, 2, 2); curvis(output, false); select(output, 1, 1); x := halfsquare; { set inital square location } y := halfsquare; xd := +1; { set movements } yd := +1; lx := x; { set last position to same } ly := y; cd := false; { set 1st display } drawsquare(green, x, y); { place square at fisrt position } while true do begin { select display and update surfaces } select(output, ord(not cd)+1, ord(cd)+1); drawsquare(white, lx, ly); { erase square at old position } lx := x; { save last position } ly := y; 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; drawsquare(green, x, y); { place square at new position } cd := not cd; { flip display and update surfaces } {wait(10);} { wait } chkbrk { check complete } end; 99: end.