Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

again i need help from the ones who know theyre thing:)

tipul01tipul01 Posts: 3Member
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.

Comments

  • terupterup Posts: 18Member
    : 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.

    Bine, vad ca esti roman. Pai in primul rand in procedura "sk" dupa comanda "adaugare" ai pus conditia aia ca atunci cand primul patrat al ramei ajunge la punctul (g,h) sa iti adauge inca un patratel. Primul ti-l adauga dar dupa aceea g si h sunt modificate iar conditia nu mai este indeplinita. Deci nu o sa-ti mai adauge inca un patratel la rama.
    Altceva este ca tu i-ai spus sa iti deseneze patratelul dupa ce ai dat randomize si sa-l deseneze in punctul (g,h). Incearca sa il desenezi in punctul (ultim^.infx,ultim^.infy).
    This is my opinion. Sper sa ii dai de cap si sa il reusesti ca e interesant. Salut.
  • zibadianzibadian Posts: 6,349Member
    : : 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.
    :
    : Bine, vad ca esti roman. Pai in primul rand in procedura "sk" dupa comanda "adaugare" ai pus conditia aia ca atunci cand primul patrat al ramei ajunge la punctul (g,h) sa iti adauge inca un patratel. Primul ti-l adauga dar dupa aceea g si h sunt modificate iar conditia nu mai este indeplinita. Deci nu o sa-ti mai adauge inca un patratel la rama.
    : Altceva este ca tu i-ai spus sa iti deseneze patratelul dupa ce ai dat randomize si sa-l deseneze in punctul (g,h). Incearca sa il desenezi in punctul (ultim^.infx,ultim^.infy).
    : This is my opinion. Sper sa ii dai de cap si sa il reusesti ca e interesant. Salut.
    :
    This explanation is useless to others, who speak only english but have the same problem. Remember that there are many people non-italian or non-spanish (I'm not sure which) readers here, which could benefit from your answer.
  • terupterup Posts: 18Member
    : : 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.
    :
    : Bine, vad ca esti roman. Pai in primul rand in procedura "sk" dupa comanda "adaugare" ai pus conditia aia ca atunci cand primul patrat al ramei ajunge la punctul (g,h) sa iti adauge inca un patratel. Primul ti-l adauga dar dupa aceea g si h sunt modificate iar conditia nu mai este indeplinita. Deci nu o sa-ti mai adauge inca un patratel la rama.
    : Altceva este ca tu i-ai spus sa iti deseneze patratelul dupa ce ai dat randomize si sa-l deseneze in punctul (g,h). Incearca sa il desenezi in punctul (ultim^.infx,ultim^.infy).
    : This is my opinion. Sper sa ii dai de cap si sa il reusesti ca e interesant. Salut.
    :

    Well, I guess that this might bee the problem:
    In the procedure "sk" you have an if statement. You told to ad a new sqare to your snake if the first sqare (the head) of the snake gets to the point with the (g,h) coordonates. At the first it works but after the condition is not satisfied.
    Another thing is that if the condition is satisfied, it calculates corectly the last sqare but it draws to a random point.
    I apologize to all.
Sign In or Register to comment.