-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.pas
145 lines (128 loc) · 4.56 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Forms, Controls, Dialogs, TheTetris;
type
TFormMain = class(TForm)
//Ìèíèìàëüíîå êîë-âî ìåòîäîâ îáðàáîòêè ñîáûòèé
procedure FormCreate(Sender: TObject);
procedure FormDeactivate(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure FormPaint(Sender: TObject);
end;
var
FormMain:TFormMain;
TetrisGame:TTetrisGame; //Äâèæîê èãðû
implementation
uses Load;
{$R *.dfm}
//Äâèæîê òåòðèñà ìîæíî ïîäêëþ÷èòü ê ëþáîìó ïðîåêòó â âèäå ìîäóëÿ
//Èíèöèàëèçèðîâàòü åãî íà ëþáîé êàíàâå (æåëàòåëüíî ðàçìåðîì 440x440)
//À òàêæå íåîáõîäèìî îáðàáîòàòü íåêîòîðûå ñîáûòèÿ, òàêèå êàê äâèæåíèå ìûøè, íàæàòèå êëàâèø, äåàêòèâàöèÿ îêíà è äð. (Âñåãî 7 ñîáûòèé)
procedure TFormMain.FormCreate(Sender: TObject);
begin
BorderStyle:=bsSingle;
//Ñîçäàåì è ïîêàçûâàåì îêíî ñ èíô. î çàãðóçêå
FormLoading:=TFormLoading.Create(Self);
FormLoading.Show;
Application.ProcessMessages;
//Ïðèíóäèòåëüíî óñòàíàâëèâàåì ðàçìåð êëèåíòñêîé îáëàñòè
ClientHeight:=460;
ClientWidth:=440;
//Óñòàíàâëèâàåì åãî äëÿ êëèåíòñêîé ÷àñòè
Cursor:=1;
//Ïåðåõâàòûâàåì ñîáûòèå äàêòèâàöèè îêíà (äëÿ ïàóçû)
Application.OnDeactivate:=FormDeactivate;
//Ïîïûòêà èíèöèàëèçèðîâàòü äâèæîê
try
TetrisGame:=TTetrisGame.Create(Canvas);
if not TetrisGame.Load then Application.Terminate;
except
Application.Terminate;
end;
//Óíè÷òîæàåì îêíî ñ çàãðóçêîé
FormLoading.Free;
end;
procedure TFormMain.FormDeactivate(Sender: TObject);
begin
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if TetrisGame.Shutdowning then Exit;
//Åñëè èäåò èãðà - ñòàâèì ïàóçó
if TetrisGame.GameState = gsPlay then TetrisGame.PauseGame;
end;
procedure TFormMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Ïîñûëàåì èãðå ñèãíàë î ïåðåäâèæåíèè ìûøè
TetrisGame.OnMouseMove(Shift, X, Y);
end;
procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Ïîñûëàåì èãðå ñèãíàë î íàæàòèè êëàâèøè
TetrisGame.OnKeyDown(Key, Shift);
end;
procedure TFormMain.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Ïîñûëàåì èãðå ñèãíàë î êëèêå ìàøüþ
TetrisGame.OnMouseUp(Button, Shift, X, Y);
end;
procedure TFormMain.FormPaint(Sender: TObject);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
TetrisGame.Draw;
end;
procedure TFormMain.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Ïîñûëàåì èãðå ñèãíàë î íàæàòèè ìûøè
TetrisGame.OnMouseDown(Button, Shift, X, Y);
end;
procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Çàïðåùàåì çàêðûâàòü îêíî áåç ñïðîñà èãîâîãî äâèæêà
CanClose:=False;
//Ïîñûëàåì èãðå ñèãíàë î çàêðûòèè îêíà
TetrisGame.Shutdown;
end;
procedure TFormMain.FormKeyPress(Sender: TObject; var Key: Char);
begin
//Åñëè èãðà âûêë÷àåòñÿ - âûõîäèì
if Application.Terminated then Exit;
//Åñëè äâèæîê íå ñîçäàí - âûõîäèì
if not Assigned(TetrisGame) then Exit;
//Ïîñûëàåì èãðå ñèãíàë î íàæàòèè è îòïóùåííîé êëàâèøå
TetrisGame.OnKeyPress(Key);
end;
end.