unit Snake;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Windows;
type
{ TForm1 }
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
// procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{ private declarations }
public
{ public declarations }
end;
procedure Zeichnen(StartPoint_Rect: TPoint; Transparenz: Integer);
var
Form1: TForm1;
Koerper:
Array of TPoint;
Richtung: Integer;
const
Groesse_Pixel: Integer = 5;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
Repeat
Memo1.Clear;
For i := 0
to Length(Koerper) -1
do // Schleife um alle Abschnitte zu zeichnen
begin
Zeichnen(Koerper[i],0);
Memo1.Lines.Insert(i, InttoStr(i) + '
= ' + InttoStr(Koerper[i].X) + '
,' + InttoStr(Koerper[i].Y));
end;
Zeichnen(Koerper[Length(Koerper)-1],1);
sleep(1000);
For i:= High(Koerper)
downto 0
do // Verschieben der abschnitte auf den Abschnitt näher des Kopfes
begin
If i = 0
Then // Kopfrichtung bestimmen
begin
If Richtung = 1
Then
begin
Koerper[0].Y := Koerper[0].Y + Groesse_Pixel;
end
else if Richtung = 2
Then
begin
Koerper[0].X := Koerper[0].X + Groesse_Pixel;
end
else if Richtung = 3
Then
begin
Koerper[0].Y := Koerper[0].Y - Groesse_Pixel;
end
else if Richtung = 4
Then
begin
Koerper[0].X := Koerper[0].X - Groesse_Pixel;
end;
Memo1.Lines.Insert(High(Koerper),InttoStr(Richtung));
end
else Koerper[i] := Koerper[i-1];
end;
Application.ProcessMessages;
Until Koerper[0].X >= Form1.Width;
end;
{procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
if key = vk_up then Richtung := 1;
if key = vk_right then Richtung := 2;
if key = vk_down then Richtung := 3;
if key = vk_left then Richtung := 4;
end;}
Procedure Zeichnen(StartPoint_Rect: TPoint; Transparenz: Integer);
begin
If Transparenz = 0
Then
Form1.Canvas.Brush.Color := clblack
else if Transparenz = 1
Then
Form1.Canvas.Brush.Color := clbtnFace;
Form1.Canvas.FillRect(Rect(StartPoint_Rect.X,StartPoint_Rect.Y, StartPoint_Rect.X + Groesse_Pixel,StartPoint_Rect.Y + Groesse_Pixel));
Form1.Canvas.Brush.Color := clblack;
end;
begin
Setlength(Koerper,7);
Koerper[0] := Point(30,100);
Koerper[1] := Point(25,100);
Koerper[2] := Point(20,100);
Koerper[3] := Point(15,100);
Koerper[4] := Point(10,100);
Koerper[5] := Point(5,100);
Koerper[6] := Point(0,100);
end.