{******************************************************************************* Program to bounce animated ball around screen *******************************************************************************} program ball2(input, output); uses gralib; label 99; const ballsize = 21; halfball = ballsize div 2; frametime = 156; { time between frames, 60 cycle refresh } ballaccel = 5; { ball acceleration } var x, y: integer; nx, ny: integer; lx, ly: integer; xd, yd: integer; er: evtrec; tc: integer; 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) 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; begin curvis(output, false); { turn off cursor } x := halfball; { set inital ball location } y := halfball; xd := +1; { set movements } yd := +1; lx := x; { set last position to same } ly := y; cd := false; { set 1st display } drawball(green, x, y); { place ball at fisrt position } while true do begin { select display and update surfaces } select(output, ord(not cd)+1, ord(cd)+1); drawball(white, lx, ly); { erase ball at old position } lx := x; { save last position } ly := y; for tc := 1 to ballaccel do begin { move ball } 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; drawball(green, x, y); { place ball at new position } cd := not cd; { flip display and update surfaces } wait(frametime) { wait } end; 99: curvis(output, true) { turn on cursor } end.