主题:我编的贪吃蛇.
程序我看没错,但运行后蛇身无法动,高手们看看:
uses crt,graph;
var
gd,gm,g,p1,p2,i,j,e,x,y:integer;
ss:array[1..5459]of record a1,a2,a3:integer;end;{用于储藏每一节蛇的坐标,A1是记录每节蛇的先后次序;A2是纵坐标;A3是横坐标}
ch:char;ok:integer;
procedure food(var x,y:integer);(随即形成一个食物的坐标}
begin
repeat
x:= random(640); until x>1;
repeat
y:= random(480);until y>1;
end;
procedure run(i,j:integer;var g:integer);{记录每一节蛇的坐标}
var p,q:integer;
begin
for p:=1 to 5459 do{蛇向前移动或吃一个食物时,增加一节蛇,并记录坐标}
with ss[p] do
if a1=0
then begin a1:=g;a2:=i;a3:=j;break;end;
g:=g+1;
if ok=2 {如果它没吃食物,那擦掉最后一节坐标}
then begin
for p:=1 to 5459 do
with ss[p] do
if a1<>0
then q:=a1;end;
for p:=1 to 5459 do
with ss[p] do
if (a1<>0)and(a1<q)
then q:=a1;
with ss[q] do begin
a1:=0;a2:=0;a3:=0;end;
end;
procedure where(e:integer;var p1,p2:integer);{把从键盘读入的方位赋给方位变量E}
begin
case e of
77:begin p1:=8;p2:=0;end;
75:begin p1:=-8;p2:=0;end;
72:begin p1:=0;p2:=-8;end;
80:begin p1:=0;p2:=8;end;end;end;
procedure pic;{把数组中的蛇画出来}
var o:integer;
begin
for o:=1 to 5459 do
with ss[o] do begin
if a1<>0
then begin setcolor(0);circle(a2,a3,4);
end;end;end;
procedure eat(e:integer);{判断它有没有吃食物}
var s1,s2:integer;
begin
case e of
77:begin
if (x-8=i)and(y=j) then begin ok:=1;run(i,j,g);food(x,y);end else run(i,j,g)end;
75:begin
if (x+8=i)and(y=j) then begin ok:=1;run(i,j,g);food(x,y);end else run(i,j,g)end;
72:begin
if(y+8=i)and(x=j) then begin ok:=1;run(i,j,g);food(x,y);end else run(i,j,g)end;
80:begin
if (y-8=i)and(x=j) then begin ok:=1;run(i,j,g);food(x,y);end else run(i,j,g)end;
end;end;
procedure over (e,i,j:integer);{判断游戏是否结束}
var b1:integer;
begin
for b1:=1 to 5459 do
with ss[b1] do
if (a1<>0)and(a2<>0)and(a1=i)and(a2=j)
then begin clrscr;write('game over');delay(2000);halt;end;
if((e=77)and(i=640))or((e=75)and(i=0))
or((e=72)and(j=0))or((e=80)and(j=480))
then begin
clrscr;write('game over');delay(2000);halt;end;
end;
begin
gd:=detect;
initgraph(gd,gm,' ');for i:=1 to 5459 do
begin with ss[i] do begin{清空数组}
a1:=0;a2:=0;a3:=0;end;end;
i:=10;j:=10;p1:=8;p2:=0;g:=1;e:=77;
ok:=0;ch:=#255;food(x,y);
repeat
i:=i+p1;j:=j+p2;
if keypressed
then begin
ch:=readkey;
if ch=#0 then
begin
ch:=readkey;e:=ord(ch);where(e,p1,p2);end;end;
run(i,j,g);pic;
over(e,i,j);eat(e);setcolor(4);circle(x,y,2);
clrscr;
until ch=#27;
end.