unit delphigame;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
function ColCheck:Boolean;
procedure MovePlayer;
procedure SpawnEnemy;
procedure FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure FormKeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure IdleEventHandler(Sender: TObject;
var Done: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
movingUp : boolean;
movingDown : boolean;
movingRight : boolean;
movingLeft : boolean;
x,y : Integer;
oldx, oldy : Integer;
x1,y1,x2,y2 : Integer;
the_time : TDateTime;
GameStarted : Boolean;
Level: Integer;
Enemies : Integer;
EnemySpawned : Boolean;
implementation
procedure TForm1.IdleEventHandler(Sender: TObject;
var Done: Boolean);
begin
If GameStarted
then
begin
Oldx := x;
Oldy := y;
MovePlayer;
if (Mouse.CursorPos.x > Left)
And (Mouse.CursorPos.x < (Form1.Width + Left))
then
x:= Mouse.CursorPos.x - left-50;
if (Mouse.CursorPos.y > top)
And (Mouse.CursorPos.y < (Form1.height + top))
then
y:=Mouse.CursorPos.Y - top-50;
SpawnEnemy;
Sleep(5);
Form1.Canvas.Brush := Brush;
//select brush
Form1.Canvas.FillRect(Form1.ClientRect);
//redraw form
If ColCheck
then
begin
Canvas.Brush.Color := clRed
end else
Canvas.Brush.Color := clBlack;
Form1.Canvas.Rectangle(x,y,100+x,100+y);
Form1.Canvas.Rectangle(x1,y1,x2,y2);
// Label2.Caption := IntTOStr(x);
// Label3.Caption := IntTOStr(y);
{Do a small bit of work here}
Done := false;
If Colcheck
then
begin
Button1.Visible := TRUE;
Label4.Visible := TRUE;
Label5.Caption := '
Score: '+IntToStr(Level);
Label5.Visible := True;
GameStarted := FALSE;
end;
end;
end;
{$R *.dfm}
procedure TForm1.FormKeyDown(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
case Key
of
vk_left:
begin
movingLeft := True;
movingRight := False;
end;
vk_right:
begin
movingLeft := False;
movingRight := True;
end;
vk_up:
begin
movingUp := True;
movingDown := False;
end;
vk_down:
begin
movingUp := False;
movingDown := True;
end;
// vk_space: BulletFired;
end;
end;
procedure TForm1.MovePlayer;
begin
if movingUp
AND (y>=0)
then y:=y-2;
if movingDown
AND (y <= Form1.Height-100)
then y:=y+2;
if movingLeft
AND (x>=0)
then x:=x-2;
if movingRight
AND (x <= Form1.Width-100)
then x:=x+2;
end;
function TForm1.ColCheck:Boolean;
begin
//Check for collision.
result := false;
If ((x2 >= x)
AND (x2<=x+100)
AND (y1 >= y)
AND (y1<=y+100))
OR
((x1 >= x)
AND (x1<=x+100)
AND (y1 >= y)
AND (y1<=y+100))
OR
((x1 >= x)
AND (x1<=x+100)
AND (y2 >= y)
AND (y2<=y+100))
OR
((x2 >= x)
AND (x2<=x+100)
AND (y2 >= y)
AND (y2<=y+100))
then
begin
Label1.Caption := '
BOOOOOOOOOM ';
result:=true;
end;
end;
procedure TForm1.SpawnEnemy;
begin
If not EnemySpawned
then
begin
Randomize;
Level := Level+1;
x1 := 0+Random(Form1.Width);
y1 := Form1.Height;
x2 := x1+Random(50)+50;
y2 := Form1.Height+Random(50)+50;
Form1.Canvas.Rectangle(x1,y1,x2,y2);
EnemySpawned := TRUE;
end;
//Move Enemy Upwards
y1 := y1 - Level;
y2 := y2 - Level;
If y2 < 0
then
EnemySpawned := FALSE;
end;
procedure TForm1.FormKeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
begin
case Key
of
vk_left : movingLeft := False;
vk_right : movingRight := False;
vk_up : movingUp := False;
vk_down : movingDown := False;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
the_time := Time;
GameStarted := TRUE;
Application.OnIdle := IdleEventHandler;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GameStarted := TRUE;
Button1.Visible := FALSE;
Level := 1;
end;
end.