Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Pascal - Sto creando Snake ma...
Forum - Pascal - Sto creando Snake ma...

Avatar
The Lizard King (Member)
Rookie


Messaggi: 33
Iscritto: 10/06/2007

Segnala al moderatore
Postato alle 11:18
Domenica, 10/06/2007
Questo topic è stato chiuso dal moderatore

Allora raga, ho bisogno di un aiuto: sto creando il gioco di Snake in Pascal, e sono arrivato ad un buon punto! :D
Per ora il programma è suddiviso in 12 sottoprogrammi:
4 (spostamenti) dedicati alla direzione del serpente, e 12 (nodi) dedicati allo spostamento del serpente da destra a giù o sù, e da sinistra a giù o sù, e viceversa! :D
Attualmente il serpente si può spostare ovunque nello schermo, anche se per il momento se il serpente tocca i bordi della finestra, il programma va in titl, cosa che correggerò in futuro. La cosa più difficile dei movimenti del serpente, è quella di far cambiare direzione mentre sta già cambiando un'altra direzione (scusate il gioco di parole :asd: ).

Questo è il codice, spero che c'è qualcuno quì che sa darmi una mano:

Codice sorgente - presumibilmente Delphi

  1. program snake;
  2. uses crt;
  3. var v:array[1..80,2..20] of char;
  4. x,y,long,i,j:byte;
  5. s:char;
  6.  (*-----------------------------------------------------*)
  7. procedure destragiu;  (*Nodo da destra a gi—*)
  8.  begin
  9.   i:=0;
  10.    repeat
  11.     i:=i+1;
  12.     delay(200);
  13.     gotoxy(x-long+i,y);
  14.     v[x-long+i,y]:=' ';
  15.     write(v[x-long+i,y]);
  16.     gotoxy(x,y+i);
  17.     v[x,y+i]:='Û';
  18.     write(v[x,y+i])
  19.    until i=long;
  20.   y:=y+i
  21.  end;
  22.  (*-----------------------------------------------------*)
  23. procedure destrasu;   (*Nodo da destra a s—*)
  24.  begin
  25.   i:=0;
  26.    repeat
  27.     i:=i+1;
  28.     delay(200);
  29.     gotoxy(x-long+i,y);
  30.     v[x-long+i,y]:=' ';
  31.     write(v[x-long+i,y]);
  32.     gotoxy(x,y-i);
  33.     v[x,y-i]:='Û';
  34.     write(v[x,y-i])
  35.    until i=long;
  36.   y:=y-i
  37.  end;
  38.  (*-----------------------------------------------------*)
  39. procedure destra;    (*Spostamento a destra*)
  40.  begin
  41.   repeat
  42.    delay(100);
  43.    gotoxy(x-long+1,y);
  44.    v[x-long+1,y]:=' ';
  45.    write(v[x-long+1,y]);
  46.    x:=x+1;
  47.    gotoxy(x,y);
  48.    v[x,y]:='Û';
  49.    write(v[x,y]);
  50.    if keypressed then
  51.     repeat
  52.      s:=readkey
  53.     until (s=#80) or (s<>#80)
  54.   until (s=#80) or (s=#72) or (s='1')
  55.  end;
  56.  (*-----------------------------------------------------*)
  57. procedure sinistra;  (*Spostamento a sinistra*)
  58.  begin
  59.   repeat
  60.    delay(100);
  61.    gotoxy(x+long-1,y);
  62.    v[x+long-1,y]:=' ';
  63.    write(v[x+long-1,y]);
  64.    x:=x-1;
  65.    gotoxy(x,y);
  66.    v[x,y]:='Û';
  67.    write(v[x,y]);
  68.    if keypressed then
  69.    repeat
  70.     s:=readkey
  71.    until (s=#80) or (s<>#80)
  72.   until (s=#80) or (s=#72) or (s='1')
  73.  end;
  74.  (*-----------------------------------------------------*)
  75. procedure sinistragiu; (*Nodo da sinistra a gi—*)
  76.  begin
  77.   i:=0;
  78.    repeat
  79.     i:=i+1;
  80.     delay(200);
  81.     gotoxy(x+long-i,y);
  82.     v[x+long-i,y]:=' ';
  83.     write(v[x+long-1,y]);
  84.     gotoxy(x,y+i);
  85.     v[x,y+i]:='Û';
  86.     write(v[x,y+i])
  87.    until i=long;
  88.   y:=y+i
  89.  end;
  90.  (*-----------------------------------------------------*)
  91. procedure sinistrasu; (*Nodo da sinistra a s—*)
  92.  begin
  93.   i:=0;
  94.    repeat
  95.     i:=i+1;
  96.     delay(200);
  97.     gotoxy(x+long-i,y);
  98.     v[x+long-i,y]:=' ';
  99.     write(v[x+long-1,y]);
  100.     gotoxy(x,y-i);
  101.     v[x,y-i]:='Û';
  102.     write(v[x,y-i])
  103.    until i=long;
  104.   y:=y-i
  105.  end;
  106.  (*-----------------------------------------------------*)
  107. procedure su;         (*Spostamento verso s—*)
  108.  begin
  109.   repeat
  110.    delay(200);
  111.    gotoxy(x,y+long-1);
  112.    v[x,y+long-1]:=' ';
  113.    write(v[x,y+long-1]);
  114.    y:=y-1;
  115.    gotoxy(x,y);
  116.    v[x,y]:='Û';
  117.    write(v[x,y]);
  118.    if keypressed then
  119.    repeat
  120.     s:=readkey
  121.    until (s=#77) or (s<>#77)
  122.   until (s=#77) or (s=#75) or (s='1')
  123.  end;
  124.  (*-----------------------------------------------------*)
  125. procedure giu;        (*Spostamento verso gi—*)
  126.  begin
  127.   repeat
  128.    delay(200);
  129.    gotoxy(x,y-long+1);
  130.    v[x,y-long+1]:=' ';
  131.    write(v[x,y-long+1]);
  132.    y:=y+1;
  133.    gotoxy(x,y);
  134.    v[x,y]:='Û';
  135.    write(v[x,y]);
  136.    if keypressed then
  137.    repeat
  138.     s:=readkey
  139.    until (s=#77) or (s<>#77)
  140.   until (s=#77) or (s=#75) or (s='1')
  141.  end;
  142.  (*-----------------------------------------------------*)
  143. procedure sudestra;   (*Nodo da s— a destra*)
  144.  begin
  145.   i:=0;
  146.    repeat
  147.     i:=i+1;
  148.     delay(200);
  149.     gotoxy(x,y+long-i);
  150.     v[x,y+long-i]:=' ';
  151.     write(v[x,y+long-1]);
  152.     gotoxy(x+i,y);
  153.     v[x+i,y]:='Û';
  154.     write(v[x+i,y])
  155.    until i=long;
  156.   x:=x+i
  157.  end;
  158.  (*-----------------------------------------------------*)
  159. procedure susinistra;  (*Nodo da s— a sinistra*)
  160.  begin
  161.   i:=0;
  162.    repeat
  163.     i:=i+1;
  164.     delay(200);
  165.     gotoxy(x,y+long-i);
  166.     v[x,y+long-i]:=' ';
  167.     write(v[x,y+long-1]);
  168.     gotoxy(x-i,y);
  169.     v[x-i,y]:='Û';
  170.     write(v[x-i,y])
  171.    until i=long;
  172.   x:=x-i
  173.  end;
  174.  (*-----------------------------------------------------*)
  175. procedure giudestra;   (*Nodo da gi— a destra*)
  176.  begin
  177.   i:=0;
  178.    repeat
  179.     i:=i+1;
  180.     delay(200);
  181.     gotoxy(x,y-long+i);
  182.     v[x,y-long+i]:=' ';
  183.     write(v[x,y-long+1]);
  184.     gotoxy(x+i,y);
  185.     v[x+i,y]:='Û';
  186.     write(v[x+i,y])
  187.    until i=long;
  188.   x:=x+i
  189.  end;
  190.  (*-----------------------------------------------------*)
  191. procedure giusinistra;  (*Nodo da gi— a sinistra*)
  192.  begin
  193.   i:=0;
  194.    repeat
  195.     i:=i+1;
  196.     delay(200);
  197.     gotoxy(x,y-long+i);
  198.     v[x,y-long+i]:=' ';
  199.     write(v[x,y-long+1]);
  200.     gotoxy(x-i,y);
  201.     v[x-i,y]:='Û';
  202.     write(v[x-i,y])
  203.    until i=long;
  204.   x:=x-i
  205.  end;
  206.  (*-----------------------------------------------------*)
  207. begin (***************INIZIO DEL PROGRAMMA****************)
  208.  clrscr;
  209.  for i:=1 to 80 do
  210.   for j:=2 to 20 do      (*Inizializzazione del serpente*)
  211.    v[i,j]:=' ';          (*e attributi*)
  212.  long:=6;
  213.  x:=long+10;y:=15;
  214.  for i:=x-long+1 to long do v[i,y]:='Û';
  215.  for i:=x-long+1 to long do
  216.   begin
  217.    gotoxy(i,y);
  218.    write(v[i,y])
  219.   end;
  220.  (*-----------------------------------------------------*)
  221.  repeat
  222.   destra;
  223.   if s=#72 then       (*Movimenti del serpente*)
  224.      begin
  225.       destrasu;
  226.       su;
  227.       if s=#77 then sudestra
  228.       else
  229.       if s=#75 then
  230.          begin
  231.           susinistra;
  232.           repeat
  233.           sinistra;
  234.           if s=#72 then
  235.              begin
  236.               sinistrasu;
  237.               su;
  238.               if s=#75 then susinistra;
  239.               if s=#77 then sudestra
  240.              end
  241.           else
  242.           if s=#80 then
  243.              begin
  244.               sinistragiu;
  245.               giu;
  246.               if s=#75 then giusinistra;
  247.               if s=#77 then giudestra
  248.              end
  249.           until (s=#77) or (s='1')
  250.          end
  251.      end
  252.   else
  253.   if s=#80 then
  254.      begin
  255.       destragiu;
  256.       giu;
  257.       if s=#77 then giudestra
  258.       else
  259.       if s=#75 then
  260.          begin
  261.           giusinistra;
  262.           repeat
  263.           sinistra;
  264.           if s=#72 then
  265.              begin
  266.               sinistrasu;
  267.               su;
  268.               if s=#75 then susinistra;
  269.               if s=#77 then sudestra
  270.              end
  271.           else
  272.           if s=#80 then
  273.              begin
  274.               sinistragiu;
  275.               giu;
  276.               if s=#75 then giusinistra;
  277.               if s=#77 then giudestra
  278.              end
  279.           until (s=#77) or (s='1')
  280.          end
  281.      end
  282.  until s='1'
  283. end.


PM
Avatar
Karl (Member)
Pro


Messaggi: 70
Iscritto: 31/12/2006

Segnala al moderatore
Postato alle 16:47
Martedì, 03/07/2007
.

Ultima modifica effettuata da Karl il 03/07/2007 alle 16:49
PM
Avatar
Karl (Member)
Pro


Messaggi: 70
Iscritto: 31/12/2006

Segnala al moderatore
Postato alle 16:47
Martedì, 03/07/2007
Cerca Di Indentare Il Codice Sorgente Grazie Aiuterà di sicuro anche te hhe:k:

PM
Avatar
WARRIOR (Ex-Member)
Guru


Messaggi: 627
Iscritto: 30/03/2007

Segnala al moderatore
Postato alle 23:17
Mercoledì, 04/07/2007
Si, è buona norma creare una gerarchia nel codice, quindi renderlo piu chiaro ;) .
Cmq ognuno ha il suo stile :k:.
Ho visto che hai pubblicato il programma, quindi qui si puo chiudere.

Locked!
Luca :k:

PM