-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.pas
130 lines (101 loc) · 2.78 KB
/
main.pas
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(*
http://corpsman.de/index.php?doc=beispiele/pingpong
*)
unit main;
{$MODE OBJFPC}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
BGRABitmap, BGRABitmapTypes,
Ball;
type
{ TForm1 }
TForm1 = class(TForm)
AppPro: TApplicationProperties;
procedure AppProIdle(Sender: TObject; var Done: Boolean);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
Balls: array of TBall;
Initialized: Boolean = FALSE;
FormBitmap: TBGRABitmap;
implementation
{$R *.lfm}
procedure Render;
var
i, j: Integer;
begin
if not Initialized then
Exit;
FormBitmap.Fill(BGRABlack);
for i := 0 to High(Balls) do
Balls[i].Render(FormBitmap);
FormBitmap.Draw(Form1.Canvas, 0, 0, True);
for i := 0 to High(Balls) do
Balls[i].Move;
for i := 0 to High(Balls) do
for j := i + 1 to High(Balls) do
begin
Balls[i].Collide(Balls[j]);
end;
for i := 0 to High(Balls) do
Balls[i].BorderCollision(Form1.ClientRect);
end;
{ TForm1 }
procedure TForm1.AppProIdle(Sender: TObject; var Done: Boolean);
begin
Render;
Done := FALSE;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var
i: Integer;
begin
Initialized := FALSE;
for i := 0 to High(Balls) do
Balls[i].Free;
FormBitmap.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i, w, h: Integer;
begin
BorderStyle := bsNone;
Top := 0;
Left := 0;
Width := Screen.Width;
Height := Screen.Height;
FormBitmap := TBGRABitmap.Create(ClientWidth, ClientHeight);
SetLength(Balls, 10);
for i := 0 to High(Balls) do
begin
Balls[i] := TBall.Create(PointF(0, 0), PointF(0, 0), 20 + Random(20) + 1, 0);
Balls[i].CalculateMass;
end;
w := Width div 5;
h := Height div 4;
Balls[0].position := PointF(w * 1, h * 2);
Balls[1].Position := PointF(w * 2, h * 1);
Balls[2].Position := PointF(w * 3, h * 1);
Balls[3].Position := PointF(w * 4, h * 1);
Balls[4].Position := PointF(w * 2, h * 2);
Balls[5].Position := PointF(w * 3, h * 2);
Balls[6].Position := PointF(w * 4, h * 2);
Balls[7].Position := PointF(w * 2, h * 3);
Balls[8].Position := PointF(w * 3, h * 3);
Balls[9].Position := PointF(w * 4, h * 3);
Balls[0].Speed := PointF(Cos((-45) * PI / 180) * 4, Sin(-45 * PI / 180) * 4);
Initialized := TRUE;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
Close;
end;
end.