Delphi实现贪吃蛇游戏

2008-04-10 02:57:34来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

首先,打开New菜单,新建一个Form1,将它的Caption属性命名为贪吃蛇,打开System选项卡,在表单中添加一个PaintBox控件,大小自己控制,再在表单中添加两个Timer控件,timer1(控制蛇),timer2(控制食物),加入一个TMainMenu控件,它有三个顶层菜单:打开,级别,关于。打开下面又分为New(Name:=New2),Stop(Name:=Stop1)和away.级别下面又分为One(name:=one1)以此类推,级别个数可以有自己决定。关于下面about。
其次,进入代码区,打开“打开”菜单,单击New,进入代码编辑区,找到变量定义的地方,加入如下代码:

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);假如代码后变为:
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;

双击Timer1,假如代码后变为:
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;
在object inspector中选中Form1的Event页,双击OnKeyDown,假如代码后为://获得用户输入;
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;
至此,用户可以控制蛇的运动。
下面,我们介绍食物的产生,双击Timer2,加入代码后为:
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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用 Delphi 实现报表的横向分组打印

下一篇:Delphi7的IDE概述(一)