## CSDN博客

### Delphi实现贪吃蛇游戏

Borland公司推出的开发工具Delphi6.0功能强大，我现在为大家介绍一下利用Delphi来制作手机游戏贪吃蛇。

x,y : array [1..100] of integer; //snake
n : integer;
sum :integer;
fx : integer; //director
a,b : integer;
ss1,ss2,ss3,ss4 : integer; //control snake.

procedure TForm1.New2Click(Sender: TObject);
var
i : integer;
begin
for i := 1 to 10 do
begin
x[i] := 10 + i * 10;
y[i] := 260;
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clgreen;
PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
end;
Timer1.Enabled := True;
Timer2.Enabled := True;
fx := 4; //表示初始时，方向向右；
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
i : integer;
begin
sum := 0;
if (fx = 4) then //diretor is right;
begin
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clsilver;
paintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
for i := 1 to 9 do
begin
x[i] := x[i + 1];
y[i] := y[i + 1];
end;
PaintBox1.Canvas.Brush.Color := clgreen;
PaintBox1.Canvas.Rectangle(x[9]+10,y[9],x[9] + 20,y[9] + 10);
x[10] := x[9] + 10;
y[10] := y[9];;
end;
if (fx = 2) then //diretor is down;
begin
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clsilver;
paintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
for i := 1 to 9 do
begin
x[i] := x[i + 1];
y[i] := y[i + 1];
end;
PaintBox1.Canvas.Brush.Color := clgreen;
PaintBox1.Canvas.Rectangle(x[9]+10,y[9],x[9] + 20,y[9] + 10);
x[10] := x[9];
y[10] := y[9] + 10;
end;
if (fx = 1) then //diretor is up;
begin
if (x[1] = x[2]) then
begin
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clsilver;
paintBox1.Canvas.Rectangle(x[2],y[2]+10,x[2]+10,y[2]+20);
end;
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clsilver;
paintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
for i := 1 to 9 do
begin
x[i] := x[i + 1];
y[i] := y[i + 1];
end;
PaintBox1.Canvas.Brush.Color := clgreen;
PaintBox1.Canvas.Rectangle(x[9]+10,y[9],x[9] + 20,y[9] + 10);
x[10] := x[9];
y[10] := y[9] - 10;
end;
if (fx = 3) then //diretor is left;
begin
PaintBox1.Canvas.Pen.Color := clsilver;
PaintBox1.Canvas.Brush.Color := clsilver;
paintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
for i := 1 to 9 do
begin
x[i] := x[i + 1];
y[i] := y[i + 1];
end;
PaintBox1.Canvas.Brush.Color := clgreen;
PaintBox1.Canvas.Rectangle(x[9]+10,y[9],x[9] + 20,y[9] + 10);
x[10] := x[9] - 10;
y[10] := y[9];
end;
if (x[10] <= ss3 + 10) then
begin
Timer1.Enabled := False;
timer2.Enabled := False;
Form2.Show;
end;
if (x[10] >= ss4 - 10) then
begin
Timer1.Enabled := False;
timer2.Enabled := False;
Form2.Show;
end;
if (y[10] <= ss1 + 10) then
begin
Timer1.Enabled := False;
timer2.Enabled := False;
Form2.Show;
end;
if (y[10] >= ss2 - 10) then
begin
Timer1.Enabled := False;
timer2.Enabled := False;
Form2.Show;
end;
if ((x[10] = a)and(y [10] = b)) then
begin
PaintBox1.Canvas.Brush.Color := clsilver;
PaintBox1.Canvas.Rectangle(a,b,a+10,b+10);
timer2.Enabled := true;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);begin
ss1 := PaintBox1.Top;
ss2 := PaintBox1.Height + ss1;
ss3 := PaintBox1.Left;
ss4 := paintBox1.Width + ss3;
n := 0;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
begin
//VK_LEFT = 37;
//VK_UP = 38;
//VK_RIGHT = 39;
//VK_DOWN = 40;
if ((key = 37)and(fx <> 4)) then
fx := 3;
if ((key = 38)and(fx <> 2)) then
fx := 1;
if ((key = 39)and(fx <> 3)) then
fx := 4;
if ((key = 40)and(fx <> 1)) then
fx := 2;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
a := random(501);
b := random(301);
PaintBox1.Canvas.Brush.Color := clYellow;
PaintBox1.Canvas.Rectangle(a,b,a+10,b+10);
if ((a > 10 ) and (a < 501)and(b > 10)and(b < 301)) then
begin
Timer2.Enabled := false;
end
else
begin
PaintBox1.Canvas.Brush.Color := clsilver;
PaintBox1.Canvas.Rectangle(a,b,a+10,b+10);
timer2.Enabled := true;
end;
end;

procedure TForm1.Stop1Click(Sender: TObject);
begin
if n = 0 then
begin
Stop1.Caption := 'Continue';
Timer1.Enabled := False;
n := 1;
end
else
begin
Stop1.Caption := 'Stop';
Timer1.Enabled := True;
n := 0;
end;
end;

procedure TForm1.Away1Click(Sender: TObject);
begin
close();
end;
procedure TForm1.One1Click(Sender: TObject);
begin
timer1.Interval := 400;
end;
procedure TForm1.two1Click(Sender: TObject);
begin
timer1.Interval := 150;
end;

procedure TForm1.three1Click(Sender: TObject);
begin
timer1.Interval := 95;
end;
procedure TForm1.Four1Click(Sender: TObject);
begin
timer1.Interval := 50;
end;