Introduction to OOP: Chapter 6: Case Study: A Billiards Game
[next]
[previous]
[audio]
[real]
[text]
Objects can be Hit
procedure Wall.hitBy (aBall : Ball);
begin
(* bounce the ball off the wall *)
aBall.setDirection(convertFactor - aBall.direction);
end;
procedure Hole.hitBy (aBall : Ball);
begin
(* drain energy from ball, remove it *)
aBall.energy := 0.0;
aBall.erase;
(* move ball *)
if aBall = CueBall then
aBall.setCenter(50, 100)
else begin
saveRack := saveRack + 1;
aBall.setCenter (10 + saveRack * 15, 250);
end;
(* redraw ball *) aBall.draw;
end;
procedure Ball.hitBy (aBall : Ball);
var
da : real;
begin
(* cut the energy of the hitting ball in half *)
aBall.energy := aBall.energy / 2;
(* and add it to our own *)
energy := energy + aBall.energy;
(* set our new direction *)
setDirection(hitAngle(self.x - aBall.x, self.y - aBall.y));
(* and set the hitting balls direction *)
da := aBall.direction - direction;
aBall.setDirection (aBall.direction + da);
end;
Intro OOP, Chapter 6, Slide 7