unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Graphics, ExtCtrls, LCLType, LCLIntf, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
ApplicationProperties1: TApplicationProperties;
Label1: TLabel;
RectShape: TShape;
BallShape: TShape;
Timer1: TTimer;
procedure ApplicationProperties1Activate(Sender: TObject);
procedure ApplicationProperties1Deactivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
BallVelocityX: Integer;
BallVelocityY: Integer;
Hit: Integer;
Missed: Integer;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if (BallShape.Top+BallShape.Height > RectShape.Top) and
(BallShape.Left > RectShape.Left) and
(BallShape.Left+BallShape.Width < RectShape.Left+RectShape.Width) then
begin
BallShape.Top := RectShape.Top - BallShape.Height;
BallVelocityY := -Random(10) -1;
Inc(Hit);
Label1.Caption := 'Hit ' + Hit.ToString + ' | Missed ' + Missed.ToString;
end;
if BallShape.Left < 0 then
BallVelocityX := Random(10) +1;
if BallShape.Left+BallShape.Width > Width then
BallVelocityX := -Random(10) -1;
if BallShape.Top < 0 then
BallVelocityY := Random(10) +1;
if BallShape.Top > Height then begin
BallShape.Top := 10;
Inc(Missed);
Label1.Caption := 'Hit ' + Hit.ToString + ' | Missed ' + Missed.ToString;
end;
BallShape.Left := BallShape.Left + BallVelocityX;
BallShape.Top := BallShape.Top + BallVelocityY;
if (GetKeyState(VK_LEFT) < 0) then RectShape.Left := RectShape.Left - 10;
if (GetKeyState(VK_RIGHT) < 0) then RectShape.Left := RectShape.Left + 10;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Constraints.MinWidth := 320;
Constraints.MinHeight := 240;
Label1.Caption := '';
BallVelocityX := Random(20)-10;
BallVelocityY := Random(20)-10;
if BallVelocityX = 0 then
BallVelocityX := 1;
if BallVelocityY = 0 then
BallVelocityY := 1;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
if BallShape.Left+BallShape.Width > Width then
BallShape.Left := Width - BallShape.Width - 10;
if BallShape.Top+BallShape.Height > Height then
BallShape.Top := Hit - BallShape.Height - 10;
if RectShape.Left+RectShape.Width > Width then
RectShape.Left := Width - RectShape.Width -10;
end;
procedure TForm1.ApplicationProperties1Deactivate(Sender: TObject);
begin
Timer1.Enabled := False;
end;
procedure TForm1.ApplicationProperties1Activate(Sender: TObject);
begin
Timer1.Enabled := True;
end;
end.