Delphi實現貪喫蛇遊戲

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行代碼,不過有一點錯誤。請各位網友通知我

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章