回 帖 发 新 帖 刷新版面

主题:我编的贪吃蛇.

偶第一次编小游戏,不对之处请多多指教~贪吃蛇没用指针,原因是指针我还不理解[em12]
程序我看没错,但运行后蛇身无法动,高手们看看:
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.

回复列表 (共29个回复)

沙发

不错不错,以前我也编过,可惜源程序不见了,否则一定交流交流

板凳

好象不能用也~~~~

3 楼

看看这个帖吧:
http://www.programfan.com/club/showbbs.asp?id=100128

4 楼

真的用不了啊~
整个屏幕灰色,左下角有一点红色,其余什么也没有……

5 楼

你的蛇把我的电脑搞死了
一运行就全黑了

6 楼

不好意思,这次发个真确的,这东西我编了5天,我是新手,此程序不当之处请高手多多指教:
uses crt,graph;
var
s,gd,gm,g,p1,p2,i,j,e,x,y:integer;
ss:array[1..5459]of record a1,a2,a3:integer;end;
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 fen (s:integer);
var q1,q2,q3,q4,q5:integer;
begin
q1:=0;q2:=s;q4:=1;repeat
q2:=q2 div 10;
q1:=q1+1;
until q2=0;
for q3:=1 to q1-1 do
begin
q4:=q4*10;
end;
for q5:=1 to q1 do
begin setcolor(7);
case s div q4 of
1:outtext('1');
2:outtext('2');
3:outtext('3');
4:outtext('4');
5:outtext('5');
6:outtext('6');
7:outtext('7');
8:outtext('8');
9:outtext('9');
0:outtext('0');end;
s:=s mod q4;q4:=q4 div 10;
end;end;

procedure run(i,j:integer;var g:integer);
var p,q,r: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 begin
with ss[p] do begin
if a1<>0
then begin q:=a1;r:=p;end;end;break;end;
for p:=1 to 5459 do
with ss[p] do
if (a1<>0)and(a1<q)
then begin q:=a1;r:=p;end;
with ss[r] do begin
a1:=0;a2:=0;a3:=0;end;
end; end;

procedure where(e:integer;var p1,p2:integer);
begin
case e of
77:begin p1:=16;p2:=0;end;
75:begin p1:=-16;p2:=0;end;
72:begin p1:=0;p2:=-16;end;
80:begin p1:=0;p2:=16;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(7);circle(a2,a3,8);
end;end;end;

procedure eat(e:integer);
begin
case e of
77:begin
if (((x-i)<=6)and ((abs(y-j)<=11))) then begin ok:=1;run(i,j,g);food(x,y);s:=s+3 end;end;
75:begin
if (((i-x)<=6)and ((abs(y-j)<=11))) then begin ok:=1;run(i,j,g);food(x,y);s:=s+3 end;end;
72:begin
if(((j-y)<=6)and ((abs(x-i)<=11))) then begin ok:=1;run(i,j,g);food(x,y);s:=s+3 end;end;
80:begin
if (((y-j)<=6)and ((abs(x-i)<11))) then begin ok:=1;run(i,j,g);food(x,y);s:=s+3 end;end;end;
end;

procedure head (e,i,j:integer);
begin setcolor(5);
case e of
72:begin
circle(i-2,j-2,1);
circle(i+2,j-2,1);
line(i,j-8,i,j-17);line(i,j-17,i-3,j-20);line(i,j-17,i+3,j-20);end;
80:begin
circle(i-2,j+2,1);
circle(i+2,j+2,1);
line(i,j+8,i,j+17);line(i,j+17,i-3,j+20);line(i,j+17,i+3,j+20);end;
75:begin
circle(i-2,j-2,1);
circle(i-2,j+2,1);
line(i-8,j,i-17,j);line(i-17,j,i-20,j-3);line(i-17,j,i-20,j+3);end;
77 :begin
circle(i+2,j-2,1);
circle(i+2,j+2,1);
line(i+8,j,i+17,j);line(i+17,j,i+20,j-3);line(i+17,j,i+20,j+3);
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(a1<>(g-1))and(a2=i)and(a3=j)
then begin ClearDevice;outtextxy(300,240,'game over');delay(2000);ch:=#27;end;
if(i>640)or(i<+0)or(j<0)or(j>480)
then begin
ClearDevice;outtextxy(300,240,'game over');delay(2000);ch:=#27;end;
end;

begin  randomize;
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:=20;j:=20;p1:=16;p2:=0;g:=1;e:=77;
ok:=0;ch:=#255;food(x,y);s:=0;
run(i,j,g);pic;
repeat
i:=i+p1;j:=j+p2;ok:=2;fen(s);
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;delay(50);
over(e,i,j);eat(e);setcolor(3);circle(x,y,4);head(e,i,j);delay(50);
ClearDevice;
until ch=#27;
end.

7 楼

嗯,不错!
但是那条蛇和蛇的食物闪烁得厉害,我的眼睛受不了。所以不敢多玩几遍。

8 楼

建议:不要总是ClearDevice,蛇尾用黑色的Circle覆盖掉即可。

9 楼

还想请问两问题,这程序我在FREE PASCAL中运行,就无法运行,屏幕一直是黑屏,显示器下的灯闪烁不定,这是怎么回事?
另,我想在GAME OVER后再显示一次分数,但我写gotoxy(300,240);时,系统显示常数越界,这又是怎么回事?

10 楼

Free Pascal的Graph模式不同于TP,是800×600分辩率的,由于刷新率过低,一些显示器不能支持如此低的刷新率(尤其是液晶),我家的显示器也不能支持。所以显示器黑屏了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册