diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-04-10 09:56:05 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-04-10 09:56:05 +0200 |
| commit | 8987b96aeb0937290320e6d9ed2e18ff706f3723 (patch) | |
| tree | 48693d27a9ca4899498b69c9b1934b79ac2040b1 /Занимательное программирование/7/6_tetris/unit2.pas | |
| parent | e4dd7b74168344d41abe501a3c8f5b8438cca58a (diff) | |
| download | book-exercises-8987b96aeb0937290320e6d9ed2e18ff706f3723.tar.gz | |
Закончил тетрис из 7-й главы, шестого упражнения
Diffstat (limited to 'Занимательное программирование/7/6_tetris/unit2.pas')
| -rw-r--r-- | Занимательное программирование/7/6_tetris/unit2.pas | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Занимательное программирование/7/6_tetris/unit2.pas b/Занимательное программирование/7/6_tetris/unit2.pas new file mode 100644 index 0000000..2ac9fea --- /dev/null +++ b/Занимательное программирование/7/6_tetris/unit2.pas @@ -0,0 +1,78 @@ +unit Unit2;
+
+{$mode ObjFPC}
+
+interface
+
+uses
+ Classes, SysUtils, Graphics;
+
+type
+ IGameHandler = interface
+ procedure DoIteration;
+ procedure Reset;
+ procedure Finish;
+ function IsFinished: Boolean;
+ end;
+
+ TActionThread = class(TThread)
+ public
+ GameHandler: IGameHandler;
+
+ constructor Create(AGameHandler: IGameHandler);
+ procedure Execute; override;
+ end;
+
+const
+ FieldHeight = 20;
+ FieldWidth = 10; { высота и ширина игрового поля }
+ Delay = 6; { Задержка падения фигуры (в кадрах). }
+ LinesPerLevel = 10;
+
+var
+ ActionThread: TActionThread;
+ MSecsPerFrame: Integer; { миллисекунд на кадр }
+
+ Key_Space, Key_Left, Key_Right, Key_Down: Boolean; { состояния клавиш }
+ Bitmaps: array[0..4] of TBitmap; { "строительные блоки" }
+ Field: array[-1..FieldWidth, 0..FieldHeight] of Integer; { игровое поле }
+ Pieces: array[1..7, 0..3, 0..3] of Integer = (
+ ((1,1,1,1), (0,0,0,0), (0,0,0,0), (0,0,0,0)),
+ ((1,1,0,0), (0,1,1,0), (0,0,0,0), (0,0,0,0)),
+ ((1,1,1,0), (0,0,1,0), (0,0,0,0), (0,0,0,0)),
+ ((1,1,0,0), (1,1,0,0), (0,0,0,0), (0,0,0,0)),
+ ((1,0,0,0), (1,1,0,0), (1,0,0,0), (0,0,0,0)),
+ ((0,0,1,0), (1,1,1,0), (0,0,0,0), (0,0,0,0)),
+ ((0,1,1,0), (1,1,0,0), (0,0,0,0), (0,0,0,0))
+ );
+
+implementation
+
+constructor TActionThread.Create(AGameHandler: IGameHandler);
+begin
+ inherited Create(false);
+ GameHandler := AGameHandler
+end;
+
+procedure TActionThread.Execute;
+var
+ OldTime: TDateTime;
+ ToWait: Integer;
+begin
+ while not GameHandler.IsFinished do
+ begin
+ OldTime := Now;
+ Synchronize(@GameHandler.DoIteration);
+ { Синхронизация с таймером. }
+ ToWait := Round(MSecsPerFrame - (Now - OldTime) * MSecsPerDay);
+ if ToWait > 0 then
+ Sleep(ToWait)
+ end;
+end;
+
+exports
+ MSecsPerFrame, Bitmaps, Field, Pieces,
+ Key_Space, Key_Left, Key_Right, Key_Down;
+
+end.
+
|
