unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, ExtCtrls, LCLType, Controls;
type
{ TForm1 }
TForm1 = class(TForm)
Shape1: TShape;
Timer1: TTimer;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Timer1Timer(Sender: TObject);
end;
TDirection = (None, MoveUp, MoveDown, MoveLeft, MoveRight);
var
Form1: TForm1;
Direction: TDirection = None;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
case Key of
VK_UP: Direction := MoveUp;
VK_DOWN: Direction := MoveDown;
VK_LEFT: Direction := MoveLeft;
VK_RIGHT: Direction := MoveRight;
VK_ESCAPE: Halt; // Press Esc to quit
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
case Direction of
MoveUp:
begin
Shape1.Top := Shape1.Top - 3;
if Shape1.Top < 0 then Shape1.Top := 0;
end;
MoveDown:
begin
Shape1.Top := Shape1.Top + 3;
if Shape1.Top > (Form1.Height-Shape1.Height) then Shape1.Top := Form1.Height-Shape1.Height;
end;
MoveLeft:
begin
Shape1.Left := Shape1.Left - 3;
if Shape1.Left < 0 then Shape1.Left := 0;
end;
MoveRight:
begin
Shape1.Left := Shape1.Left + 3;
if Shape1.Left > (Form1.Width-Shape1.Width) then Shape1.Left := Form1.Width-Shape1.Width;
end;
end;
end;
end.