CSDN博客

img jjzhk

Delphi实现贪吃蛇游戏

发表于2003/7/19 0:35:00  1088人阅读

分类: 其他文章

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
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;
然后,在新建一个Form,Form2,Name:=frmAbout.里面加个Label,Label1.写上自己的信息。在代码编辑区里,选择Unit1,在{$R *.dfm}下面加入 Uese Unit2; 单击about,加入代码后为:
procedure TForm1.AboutMe1Click(Sender: TObject);
begin
frmAbout.ShowModal();
end;
至此,全部完工了,共273行代码,不过有一点错误。请各位网友通知我

0 0

相关博文

我的热门文章

img
取 消
img