diff options
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.
+
|
