79 lines
2.0 KiB
ObjectPascal
79 lines
2.0 KiB
ObjectPascal
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.
|
|
|