手机游戏-贪吃蛇源代码
unit Unit1;
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls;
type TForm1 = class(TForm) Timer1: TTimer;
GroupBox1: TGroupBox; PaintBox1: TPaintBox; SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton; SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton; SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton; Label1: TLabel; Label2: TLabel;
SpeedButton7: TSpeedButton; UpDown1: TUpDown; Edit1: TEdit;
Label5: TLabel;
SpeedButton8: TSpeedButton; procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormKeyDown(Sender: TObject;
var
Key: Word;
Shift: TShiftState);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure SpeedButton7Click(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton8Click(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;
x,y,xxx,yyy:array[1..100] of integer; //
y:array[1..100] of integer;
fx:word;
sx,sy,ss:integer;
implementation {$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
PaintBox1.Canvas.Brush.Color:=clblue;
for i:=10 downto 1 do begin x[i]:=10+i*10;
y[i]:=197;
PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
end;
fx:=3; timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i:integer;
t:string;
begin //擦去最后一个点
Randomize;
PaintBox1.Canvas.pen.Color:=clsilver;
PaintBox1.Canvas.Brush.Color:=clsilver;
PaintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
//PaintBox1.Canvas.pen.Color:=clblue;
PaintBox1.Canvas.Brush.Color:=clblue;
//zx:=x[1];zy:=y[1];
for i:=1 to ss-1 do begin x[i]:=x[i+1];
y[i]:=y[i+1];
end; //判断左右上下的情况
if fx=1 then x[ss]:=x[ss]-10;
if fx=2 then y[ss]:=y[ss]-10;
if fx=3 then x[ss]:=x[ss]+10;
if fx=4 then y[ss]:=y[ss]+10; //如果蛇头碰到了四边 PaintBox1.Canvas.Pen.Color :=clblack; PaintBox1.Canvas.Rectangle(sx,sy,sx+10,sy+10);
if (x[ss]>PaintBox1.Width-10) or (x[ss]<0) or (y[ss]<0) or (y[ss]>PaintBox1.Height)then begin timer1.Enabled:=false;
UpDown1.Enabled:=true; edit1.Enabled:=true;
application.MessageBox('你输了!','贪吃蛇',MB_OK);
label2.Caption:='0';
PaintBox1.Canvas.pen.Color:=clsilver;
PaintBox1.Canvas.Brush.Color:=clsilver; PaintBox1.Canvas.Rectangle(0,0,PaintBox1.Width,PaintBox1.Height);
//PaintBox1.Canvas.Rectangle(0,0,290,210);
end; //如果蛇头碰到了蛇的身体
for i:=1 to ss-1 do begin if (x[ss]=x[i]) and (y[ss]=y[i]) then begin timer1.Enabled:=false; UpDown1.Enabled:=true;
edit1.Enabled:=true;
application.MessageBox('你输了!','游戏结束',MB_OK);
label2.Caption:='0'; PaintBox1.Canvas.pen.Color:=clsilver;
PaintBox1.Canvas.Brush.Color:=clsilver;
PaintBox1.Canvas.Rectangle(0,0,PaintBox1.Width,PaintBox1.Height); end;
end; //如果蛇吃到了事物
if (x[ss]=sx) and (y[ss]=sy) then begin
// for i:=1 to ss do // begin // end; //计算得分 label2.caption:=inttostr(strtoint(label2.caption)+strtoint(edit1.text));
PaintBox1.Canvas.Pen.Color :=clblack;
PaintBox1.Canvas.Brush.Color:=clblue;
// while i<>0 do
// begin sx:=random(19)*10; sy:=random(11)*10;
for i:=1 to ss do begin //t:=t+inttostr(x[i])+','+inttostr(y[i])+':'+inttostr(sx)+','+inttostr(sy)+'|';
//memo1.text:=t;
// t:=t+inttostr(sx)+','+inttostr(sy)+' ';
if (x[i]=sx) and (y[i]=sy) then begin
//label3.caption:='重叠';
sx:=random(19)*10;
sy:=random(11)*10;
PaintBox1.Canvas.Rectangle(sx,sy,sx+10,sy+10);
end;
//caption:=inttostr(sx)+','+inttostr(sy);
end;
if (x[i]<>sx) and (y[i]<>sy) then begin // label3.caption:='label3';
PaintBox1.Canvas.Rectangle(sx,sy,sx+10,sy+10);
ss:=ss+1;
x[ss]:=x[ss-1];
y[ss]:=y[ss-1];
exit; // end; // memo1.Lines.Add(t);
end;
end; //PaintBox1.Canvas.Rectangle(sx,sy,sx+10,sy+10);
//ss:=ss+1;
//x[ss]:=x[ss-1];
//y[ss]:=y[ss-1];
//end;
//画蛇身
for i:=1 to ss do begin PaintBox1.Canvas.Pen.Color :=clsilver;
// t:=t+inttostr(x[i])+','+inttostr(y[i])+' ';
// richedit1.lines.Insert(0,t);
PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
// xxx[i]:=1;
yyy[i]:=1;
end;
//caption:=inttostr(x[ss])+','+inttostr(y[ss]);
//label4.caption:=inttostr(ss);
//label1.caption:=inttostr(sx)+','+inttostr(sy);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin PaintBox1.Canvas.Brush.Color:=clsilver; PaintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
PaintBox1.Canvas.Brush.Color:=clblue;
for i:=1 to 9 do begin x[i]:=x[i+1];
y[i]:=y[i+1];
end;
x[10]:=x[10]+10;
for i:=1 to 10 do PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
// for i:=10 downto 1 do // begin // x[i]:=x[i]+10; // x[i]:=x[i+1];
// y[i]:=y[i+1]; // PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
// end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin if (key=37) and (fx<>3) then fx:=1;
if (key=38) and (fx<>4) then fx:=2;
if (key=39) and (fx<>1) then fx:=3;
if (key=40) and (fx<>2) then fx:=4;
//caption:=inttostr(key)
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin if fx<>4 then fx:=2;
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
//var //i:integer;
begin {
PaintBox1.Canvas.Brush.Color:=clsilver;
PaintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
PaintBox1.Canvas.Brush.Color:=clblue;
for i:=1 to 9 do begin x[i]:=x[i+1];
y[i]:=y[i+1];
end;
x[10]:=x[10]-10;
for i:=1 to 10 do PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
// end;}
if fx<>3 then fx:=1;
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
//var //i:integer;
begin {
PaintBox1.Canvas.Brush.Color:=clsilver;
PaintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
PaintBox1.Canvas.Brush.Color:=clblue;
for i:=1 to 9 do begin x[i]:=x[i+1];
y[i]:=y[i+1];
end;
y[10]:=y[10]+10;
for i:=1 to 10 do PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
}
if fx<>2 then fx:=4 end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
//var //i:integer;
begin {
PaintBox1.Canvas.Brush.Color:=clsilver;
PaintBox1.Canvas.Rectangle(x[1],y[1],x[1]+10,y[1]+10);
PaintBox1.Canvas.Brush.Color:=clblue;
for i:=1 to 9 do begin x[i]:=x[i+1]; y[i]:=y[i+1];
end;
x[10]:=x[10]+10;
for i:=1 to 10 do PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
}
if fx<>1 then fx:=3;
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
var
i:integer;
begin
UpDown1.Enabled:=false;
edit1.Enabled:=false;
form1.SetFocus;
PaintBox1.Canvas.Pen.Color :=clblack; PaintBox1.Canvas.Brush.Color:=clblue; sx:=100;sy:=60;
PaintBox1.Canvas.Rectangle(sx,sy,sx+10,sy+10);
PaintBox1.Canvas.Pen.Color :=clsilver; PaintBox1.Canvas.Brush.Color:=clblue;
ss:=10;
for i:=ss downto 1 do begin x[i]:=10+i*10;
y[i]:=PaintBox1.Height-10;
PaintBox1.Canvas.Rectangle(x[i],y[i],x[i]+10,y[i]+10);
end;
fx:=3;
timer1.Interval:=1000-(strtoint(edit1.text)*100);
//label5.caption:=inttostr(timer1.interval);
timer1.Enabled:=true; end;
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
if speedbutton6.Caption='暂停' then begin Timer1.Enabled:=false;
UpDown1.Enabled:=true;
edit1.Enabled:=true;
speedbutton6.Caption:='继续' end else begin Timer1.Enabled:=true;
UpDown1.Enabled:=false;
edit1.Enabled:=false;
speedbutton6.Caption:='暂停'
end;
end;
procedure TForm1.SpeedButton7Click(Sender: TObject);
begin timer1.Enabled:=false;
UpDown1.Enabled:=true;
edit1.Enabled:=tru