unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
LCLType, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
food: TShape;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Snake: TShape;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure gameLoop(Sender: TObject);
procedure collision;
procedure Label3Click(Sender: TObject);
procedure Label4Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
le, ri, up, dow, lost : boolean;
tail: array of tshape;
tailLength, laufVariable: integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.collision;
begin
if (snake.Top > clientheight) or (snake.Top < 0) or (snake.Left > clientwidth) or (snake.Left < 0) then
begin
lost:=true;
end;
for laufVariable := 1 to (tailLength - 1)do
begin
if (snake.Top = tail[laufVariable].Top) and (snake.Left = tail[laufVariable].Left) then lost:=true;
end;
end;
procedure TForm1.Label3Click(Sender: TObject);
begin
label1.Visible:=false;
label2.Visible:=false;
label3.Visible:=false;
label4.Visible:=false;
for Laufvariable := 0 to taillength -1 do
begin
tail[laufVariable].Visible:=false;
end;
setlength(tail,0);
snake.Top:=160;
snake.Left:=200;
ri := true;
le := false;
up:= false;
dow:=false;
lost:=false;
tailLength:=0;
food.Top:=random(30)*20;
food.Left:=random(30)*20;
timer1.Enabled:=true;
end;
procedure TForm1.Label4Click(Sender: TObject);
begin
tail:=nil;
close;
end;
procedure TForm1.gameLoop(Sender: TObject);
begin
collision;
if lost then
begin
timer1.Enabled:=false;
label2.Caption:='Du hast '+ inttostr(taillength) +' Punkte erreicht!';
label1.Visible:=true;
label2.Visible:=true;
label3.Visible:=true;
label4.Visible:=true;
end;
if ri then
begin
laufvariable := taillength -1;
while laufVariable > 0 do
begin
tail[laufVariable].Left:= tail[laufVariable-1].Left;
tail[laufVariable].Top:= tail[laufVariable-1].Top;
dec(laufVariable);
end;
if taillength > 0 then
begin
tail[0].Left:=Snake.Left;
tail[0].Top:=Snake.Top;
end;
Snake.Left:=Snake.Left + 20;
end
else if le then
begin
laufvariable := taillength-1;
while laufVariable > 0 do
begin
tail[laufVariable].Left:= tail[laufVariable-1].Left;
tail[laufVariable].Top:= tail[laufVariable-1].Top;
dec(laufVariable);
end;
if taillength > 0 then
begin
tail[0].Left:=Snake.Left;
tail[0].Top:=Snake.Top;
end;
Snake.left:=Snake.Left - 20;
end
else if dow then
begin
laufvariable := taillength-1;
while laufVariable > 0 do
begin
tail[laufVariable].Left:= tail[laufVariable-1].Left;
tail[laufVariable].Top:= tail[laufVariable-1].Top;
dec(laufVariable);
end;
if taillength > 0 then
begin
tail[0].Left:=Snake.Left;
tail[0].Top:=Snake.Top;
end;
Snake.Top:=Snake.Top + 20;
end
else
begin
laufvariable := taillength-1;
while laufVariable > 0 do
begin
tail[laufVariable].Left:= tail[laufVariable-1].Left;
tail[laufVariable].Top:= tail[laufVariable-1].Top;
dec(laufVariable);
end;
if taillength > 0 then
begin
tail[0].Left:=Snake.Left;
tail[0].Top:=Snake.Top;
end;
Snake.Top:=snake.Top -20;
end;
if (snake.Top=food.Top) and (snake.Left = food.Left) then
begin
food.Top:=random(30)*20;
food.Left:=random(30)*20;
inc(tailLength);
setLength(tail, tailLength);
laufvariable := taillength;
while laufVariable > 0 do
begin
tail[laufVariable]:= tail[laufVariable - 1];
dec(laufVariable);
end;
tail[0]:= Tshape.Create(Form1);
tail[0].Top:=snake.Top;
tail[0].Left:=snake.Left;
tail[0].Height:=snake.Height;
tail[0].Width:=snake.Width;
tail[0].parent:=Form1;
timer1.Interval:=trunc(timer1.Interval * 0.95);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ri := true;
le := false;
up:= false;
dow:=false;
lost:=false;
tailLength:=0;
food.Top:=random(30)*20;
food.Left:=random(30)*20;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
if (key=VK_RIGHT) and (le=false) then
begin
ri := true;
le := false;
up:= false;
dow:=false;
end else
if (key=VK_LEFT) and (ri=false) then
begin
ri := false;
le := true;
up:= false;
dow:=false;
end else
if (key=VK_UP) and (dow=false) then
begin
ri := false;
le := false;
up:= true;
dow:=false;
end else
if (key=VK_DOWN) and (up=false) then
begin
ri := false;
le := false;
up:= false;
dow:=true;
end;
end;
end.