Introduction to OOP: Chapter 6: Case Study: A Billiards Game [next] [previous] [audio] [real] [text]

Balls move when updated

procedure Ball.update;
var
	hptr : Hole;
	wptr : Wall;
	bptr : Ball;
	dx, dy : integer;
	theIntersection : Rect;
begin
	if energy > 0.5 then begin
		ballMoved := true;
			(* erase ball *)
		erase;
			(* decrease energy *)
		energy := energy - 0.05;
			(* move ball *)
		dx := trunc(5.0 * cos(direction));
		dy := trunc(5.0 * sin(direction));
		offsetRect(region, dx, dy);
			(* redraw ball *)
		draw;
			(* see if we hit a hole *)
		hptr := listOfHoles;
		while (hptr <> nil) do 
			if SectRect (region, hptr.region, 
				theIntersection) then begin
				hptr.hitBy(self);
				hptr := nil;
			end
			else
				hptr := hptr.link;

			(* see if we hit a wall *)
		wptr := listOfWalls;
		while (wptr <> nil) do 
			if SectRect (region, wptr.region, 
				theIntersection) then begin
				wptr.hitBy(self);
				wptr := nil;
			end
			else
				wptr := wptr.link;

			(* see if we hit a ball *)
		bptr := listOfBalls;
		bhit := nil;
		while (bptr <> nil) do 
			if (bptr <> self) and 
				SectRect (region, bptr.region, 
				theIntersection) then begin
				bptr.hitBy(self);
				bptr := nil;
			end
			else
				bptr := bptr.link;
	end;
end;
Intro OOP, Chapter 6, Slide 8