主题:[原创]贪食蛇者......
听说论坛上有人想知道贪食蛇怎样做
我编了一个,供大家交流交流,以便相互学习.
uses crt;
const speed=400;
type
suffer=^confuse;
confuse=record
x,y:integer;
front,next:suffer;
end;
var
a,b:suffer;
x2,y2,direction:integer;
procedure init;
var i:integer;
begin
clrscr;
for i:=1 to 60 do
write('=');
gotoxy(1,25);
for i:=1 to 60 do
write('=');
for i:=2 to 24 do
begin
gotoxy(1,i);write('|');
gotoxy(60,i);write('|');
end;
gotoxy(64,17);write('''p'':pause');
gotoxy(64,18);write('''Esc'':end');
gotoxy(64,19);write('be careful!');
gotoxy(64,10);write('made by gen');
end;
function canmove(x1,y1:integer):boolean;
var purpose:suffer;cycle:boolean;
begin
cycle:=(x1<2) or (x1>60) or (y1<2) or (y1>24);
purpose:=a^.next;
if not(cycle) then
repeat
cycle:=(x1=purpose^.x) and (y1=purpose^.y);
purpose:=purpose^.next;
until (purpose=nil) or cycle;
canmove:=not(cycle);
end;
procedure forfood;
var bt:suffer;
begin
bt:=a;
repeat
x2:=random(58)+2;
y2:=random(23)+2;
until canmove(x2,y2);
gotoxy(x2,y2);
write('#');gotoxy(70,1);
end;
procedure gameover;
begin
gotoxy(27,13);
write('gameover!');
gotoxy(70,1);readln;halt;
end;
procedure peasant;
var clown:suffer;x1,y1:integer;
begin
new(clown);
x1:=a^.x;y1:=a^.y;
case direction of
1:dec(y1);
2:dec(x1);
3:inc(x1);
4:inc(y1);
end;
clown^.x:=x1;clown^.y:=y1;
clown^.next:=a;a^.front:=clown;a:=clown;
if not(canmove(x1,y1)) then gameover;
gotoxy(x1,y1);write('*');
if (x2=x1) and (y2=y1) then forfood
else begin
gotoxy(b^.x,b^.y);write(' ');
b:=b^.front;dispose(b^.next);
gotoxy(70,1);
end;
end;
procedure play;
begin
new(a);new(b);
a^.x:=30;a^.y:=13;
b^.x:=30;b^.y:=14;
direction:=1;
a^.next:=b;b^.front:=a;
forfood;
repeat
if keypressed then
case upcase(readkey) of
#27:gameover;
'P':begin
gotoxy(64,12);write('pause now');
gotoxy(70,1);
repeat delay(500) until keypressed;
gotoxy(64,12);write(' ');
gotoxy(70,1);
end;
#0:case readkey of
#72:direction:=1;
#75:direction:=2;
#77:direction:=3;
#80:direction:=4;
end;
end;
peasant;delay(speed);
until false;
end;
begin
randomize;
init;
play;
end.
我编了一个,供大家交流交流,以便相互学习.
uses crt;
const speed=400;
type
suffer=^confuse;
confuse=record
x,y:integer;
front,next:suffer;
end;
var
a,b:suffer;
x2,y2,direction:integer;
procedure init;
var i:integer;
begin
clrscr;
for i:=1 to 60 do
write('=');
gotoxy(1,25);
for i:=1 to 60 do
write('=');
for i:=2 to 24 do
begin
gotoxy(1,i);write('|');
gotoxy(60,i);write('|');
end;
gotoxy(64,17);write('''p'':pause');
gotoxy(64,18);write('''Esc'':end');
gotoxy(64,19);write('be careful!');
gotoxy(64,10);write('made by gen');
end;
function canmove(x1,y1:integer):boolean;
var purpose:suffer;cycle:boolean;
begin
cycle:=(x1<2) or (x1>60) or (y1<2) or (y1>24);
purpose:=a^.next;
if not(cycle) then
repeat
cycle:=(x1=purpose^.x) and (y1=purpose^.y);
purpose:=purpose^.next;
until (purpose=nil) or cycle;
canmove:=not(cycle);
end;
procedure forfood;
var bt:suffer;
begin
bt:=a;
repeat
x2:=random(58)+2;
y2:=random(23)+2;
until canmove(x2,y2);
gotoxy(x2,y2);
write('#');gotoxy(70,1);
end;
procedure gameover;
begin
gotoxy(27,13);
write('gameover!');
gotoxy(70,1);readln;halt;
end;
procedure peasant;
var clown:suffer;x1,y1:integer;
begin
new(clown);
x1:=a^.x;y1:=a^.y;
case direction of
1:dec(y1);
2:dec(x1);
3:inc(x1);
4:inc(y1);
end;
clown^.x:=x1;clown^.y:=y1;
clown^.next:=a;a^.front:=clown;a:=clown;
if not(canmove(x1,y1)) then gameover;
gotoxy(x1,y1);write('*');
if (x2=x1) and (y2=y1) then forfood
else begin
gotoxy(b^.x,b^.y);write(' ');
b:=b^.front;dispose(b^.next);
gotoxy(70,1);
end;
end;
procedure play;
begin
new(a);new(b);
a^.x:=30;a^.y:=13;
b^.x:=30;b^.y:=14;
direction:=1;
a^.next:=b;b^.front:=a;
forfood;
repeat
if keypressed then
case upcase(readkey) of
#27:gameover;
'P':begin
gotoxy(64,12);write('pause now');
gotoxy(70,1);
repeat delay(500) until keypressed;
gotoxy(64,12);write(' ');
gotoxy(70,1);
end;
#0:case readkey of
#72:direction:=1;
#75:direction:=2;
#77:direction:=3;
#80:direction:=4;
end;
end;
peasant;delay(speed);
until false;
end;
begin
randomize;
init;
play;
end.