1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
| program objects;
uses crt, graph;
var dx,dy,dx1,dy1 : integer;
type Location = object
x,y,XMax,YMax : integer;
visible : boolean;
Procedure Init(a,b:integer);
end;
TCircle = object(Location)
Radius,dx,dy: integer;
Procedure Init(a,b,R,shiftX,shiftY:integer);
Procedure SwitchOn; {virtual;}
Procedure SwitchOff; {virtual;}
Procedure Move;
end;
TSquare = object(Location)
Radius,dx1,dy1: integer;
Procedure Init(a,b,shiftX,shiftY:integer);
Procedure SwitchOn; {virtual;}
Procedure SwitchOff; {virtual;}
Procedure Move;
end;
Procedure Location.Init(a,b:integer);
begin x:=a; y:=b; visible:=false end;
Procedure TCircle.Init(a,b,R,shiftX,shiftY:integer);
begin Location.Init(a,b); Radius:=R; dx:=ShiftX; dy:=ShiftY;end;
Procedure TCircle.SwitchOn;
begin visible:=true; SetColor(4); Circle(x,y,Radius);
SetFillStyle(1,4); FloodFill(x,y,4); end;
Procedure TCircle.SwitchOff;
begin visible:=false; SetColor(GetBkColor);
Circle(x,y,Radius); SetFillStyle(1,GetBkColor);
FloodFill(x,y,GetBkColor); end;
Procedure TCircle.Move;
begin
SwitchOff;
XMax:=GetMaxX;
YMax:=GetMaxY;
if (x+dx)>XMax then dx:=(-1)*dx;
if (y+dy)>YMax then dy:=(-1)*dy;
if (x+dx)<0 then dx:=(-1)*dx;
if (y+dy)<0 then dy:=(-1)*dy;
x:=x+dx; y:=y+dy;
SwitchOn;
end;
Procedure TSquare.Init(a,b,shiftX,shiftY:integer);
begin Location.Init(a,b); dx1:=ShiftX; dy1:=ShiftY;end;
Procedure TSquare.SwitchOn;
begin visible:=true; SetColor(4); Rectangle(x-10,y-10,x+10,y+10);
SetFillStyle(1,4); FloodFill(x,y,4); end;
Procedure TSquare.SwitchOff;
begin visible:=false; SetColor(GetBkColor);
Rectangle(x-10,y-10,x+10,y+10); SetFillStyle(1,GetBkColor);
FloodFill(x,y,GetBkColor); end;
Procedure TSquare.Move;
begin
SwitchOff;
XMax:=GetMaxX;
YMax:=GetMaxY;
if (x-dx1)>XMax then dx1:=(-1)*dx1;
if (y-dy1)>YMax then dy1:=(-1)*dy1;
if (x-dx1)<0 then dx1:=(-1)*dx1;
if (y-dy1)<0 then dy1:=(-1)*dy1;
x:=x-dx1; y:=y-dy1;
SwitchOn;
end;
var Circle1 : ^ TCircle;
ch : char;
XMax,YMax : integer;
Square1 : ^ TSquare;
procedure graphinit;
var grdriver,grmode,ErrCode:integer;
begin
grdriver:=Detect;
InitGraph(grdriver,grmode,'C:\langs\bp\bgi');
ErrCode:=GraphResult;
If ErrCode<>grOK then
begin
writeln('Graphic Error: ', GraphErrorMsg(ErrCode));
readln;
halt;
end;
end;
Begin
GraphInit;
XMax:=GetMaxX;
YMax:=GetMaxY;
New(Circle1);
Circle1^.Init(10,10,20,20,20);
Circle1^.SwitchOn;
New(Square1);
Square1^.Init(250,250,20,20);
Square1^.SwitchOn;
repeat
Circle1^.Move;
Square1^.Move;
Delay(130);
if ((Circle1^.x+Circle1^.dx)=(Square1^.x-Square1^.dx1)) and ((Circle1^.y+Circle1^.dy)=(Square1^.y-Square1^.dy1)) then
begin Circle1^.dx:=-1*Circle1^.dx; Square1^.dx1:=-1*Square1^.dx1; end;
until keypressed;
end. |