unit snake;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
const
farben:
Array [0..2]
of TColor = (clBlack, clLime, clRed);
raster: integer = 10;
breite: integer = 24;
hoehe: integer = 24;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Timer1: TTimer;
PaintBox1: TPaintBox;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
map:
Array [0..24]
of Array [0..24]
of Integer;
kopf,
richtung,
futter: TPoint;
laenge,
punkte: Integer;
ende: Boolean;
bmp: TBitmap;
procedure enable_buttons(status: Boolean);
procedure spiel_ende;
procedure neues_futter;
procedure paint_map;
procedure calc_snake;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.enable_buttons(status: Boolean);
begin
Button2.Enabled := status;
Button3.Enabled := status;
Button4.Enabled := status;
Button5.Enabled := status;
end;
procedure TForm1.spiel_ende;
begin
ende:= True;
Timer1.Enabled := False;
enable_buttons(False);
Button1.Enabled := True;
end;
procedure TForm1.neues_futter;
begin
futter := Point(Random(breite), Random(hoehe));
while map[futter.X, futter.Y]<>0
do
futter:= Point(Random(breite), Random(hoehe));
map[futter.X,futter.Y] := -1;
end;
procedure TForm1.paint_map;
var i,j: integer; col:TColor;
begin
for i := 0
to breite
do
for j := 0
to hoehe
do
begin
case map[i, j]
of
-1: col := farben[1];
0: col := farben[0];
else
col := farben[2];
end;
PaintBox1.Canvas.Brush.Color:=col;
PaintBox1.Canvas.FillRect(Rect(i*raster, j*raster, (i+1)*raster, (j+1)*raster));
end;
end;
procedure TForm1.calc_snake;
var i,j: Integer;
begin
for i := 0
to breite
do
for j := 0
to hoehe
do
if map[i,j]>0
then
Dec(map[i,j]);
Inc(kopf.X, richtung.X);
Inc(kopf.Y, richtung.Y);
if (kopf.X<0)
or (kopf.X>breite)
or (kopf.Y<0)
or (kopf.Y>hoehe)
then
begin
spiel_ende;
ShowMessage('
Sie haben den Rand berührt!');
end;
if (map[kopf.X, kopf.Y]>0)
and (
not ende)
then
begin
spiel_ende;
ShowMessage('
Sie haben sich selbst gebissen!');
end;
if (kopf.X=futter.X)
and (kopf.Y=futter.Y)
then
begin
Inc(laenge);
neues_futter;
end;
map[kopf.X, kopf.Y] := laenge;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,j: Integer;
begin
Button1.Enabled := False;
for i:=0
to breite
do
for j:=0
to hoehe
do
map[i,j] := 0;
kopf:= Point(1,1);
laenge:= 5;
richtung:= Point(0,1);
map[kopf.X,kopf.Y] := laenge;
neues_futter;
paint_map;
enable_buttons(True);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
calc_snake;
if not ende
then
paint_map;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if richtung.Y=0
then
richtung :=Point(0,-1);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if richtung.X=0
then
richtung:=point(-1,0);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if richtung.X=0
then
richtung:=Point(1,0);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if richtung.Y=0
then
richtung:= Point(0,1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
end;
end.