hello....again i need your help...this time is with a program of my own creation. it simulates a "snake" game. it is not 100% complete (the snake cannot yet walk by itself, but only when i push a directon key on the numpad). the trouble consists in it stopping after i press one direction key. it works just once, then it sops. i mean it does nothing. i can exit it the way i made it exit, but it does not add another block to its head/tail. (by the way, i did not made the procedure to delete the last block, i just wanted it to keep adding blocks...). pls help me, what can i do so it will continue to add a block in the direction i press more than once. thank you in advance.
program snake;
uses crt,graph;
type lista =^nod;
nod =record
infx,infy:integer;
ant,urm:lista;
end;
var prim,ultim,p:lista;
s,g,h,gmode,gdriver:integer;
ok:boolean;
procedure creare;
begin
prim^.infx:=200;
prim^.infy:=200;
prim^.ant:=nil;
prim^.urm:=nil;
p^.infx:=202;
p^.infy:=200;
p^.ant:=prim;
p^.urm:=nil;
prim^.urm:=p;
ultim^.infx:=204;
ultim^.infy:=200;
ultim^.urm:=nil;
ultim^.ant:=p;
p^.urm:=ultim;
end;
procedure verificare(var ok:boolean);
begin
p:=prim^.urm;
ok:=true;
while (p<>nil) and (ok=true) do begin
if (p^.infx=prim^.infx)and (p^.infy=prim^.infy) then ok:=false;
p:=p^.urm;
end;
end;
procedure desenare;
begin
p:=prim;
while p<>nil do begin
delay(10);
rectangle(p^.infx,p^.infy,p^.infx+2,p^.infy+2);
p:=p^.urm;
end;
end;
procedure adaugare;
begin
ultim^.ant^.urm:=nil;
ultim^.ant :=nil;
p:=ultim;
ultim:=ultim^.ant;
p^.urm:=prim;
prim^.ant:=p;
prim:=p;
end;
procedure sk;
var c:char;
begin
randomize;
g:=2*random(201);
h:=2*random(201);
rectangle(h,g,h+2,g+2);
s:=0;
repeat
c:=readkey;
case c of
'2': ultim^.infy:=ultim^.infy+2;
'8': ultim^.infy:=ultim^.infy-2;
'4': ultim^.infx:=ultim^.infx-2;
'6': ultim^.infx:=ultim^.infx+2;
end;
if ultim^.infx<0 then ultim^.infx:=400
else if ultim^.infx>400 then ultim^.infx:=0;
if ultim^.infy<0 then ultim^.infy:=400
else if ultim^.infy>400 then ultim^.infy:=0;
verificare(ok);
adaugare;
if (prim^.infx=g) and (prim^.infy=h) then begin
inc(s);
ultim^.urm:=p;
p^.ant:=ultim;
p^.urm:=nil;
p^.infx:=g;
P^.infy:=h;
ultim:=p;
randomize;
h:=2*random(201);
g:=2*random(201);
rectangle(h,g,h+2,g+2);
end;
desenare;
until (c='5') or (ok=false);
end;
begin
new(p);new(prim);new(ultim);
gdriver:=detect;
initgraph(gdriver,gmode,'');
creare;
rectangle(0,0,400,400);
desenare;
sk;readln;
writeln('Scorul dumneavoastra este ',s*100);
closegraph;
readln;
end.