Forum of Horatiu Roman - Enjoy
 
AcasaAcasa  CalendarCalendar  FAQFAQ  CautareCautare  InregistrareInregistrare  MembriMembri  GrupuriGrupuri  Harta vizitatorilorHarta vizitatorilor  Conectare  
Conectare
Utilizator:
Parola:
Conectare automata: 
:: Mi-am uitat parola
Cautare
 
 

Rezultate pe:
 
Rechercher Cautare avansata
Ultimele subiecte
» pacat ca acest forum nu mai este activ
Scris de crr_ro 12th Septembrie 2016, 3:42 pm

» CULOAREA VIETII
Scris de EUGEN 19th Iunie 2012, 3:43 pm

» Povestea lui Iov
Scris de Genesis 7th Aprilie 2011, 8:50 am

» transpun.txt
Scris de Horace 24th Ianuarie 2011, 7:32 am

» MAZEGAME
Scris de Horace 2nd Iunie 2010, 1:31 pm

» nebunie
Scris de Vizitator 24th Februarie 2010, 8:50 am

Cine este conectat?
In total este 1 utilizator conectat: 0 Inregistrati, 0 Invizibil si 1 Vizitator

Nici unul

Recordul de utilizatori conectati a fost de 13, 5th Iunie 2009, 12:33 am
Statistici
Avem 57 membri inregistrati
Cel mai nou utilizator inregistrat este: alinatim

Membrii nostri au postat un numar de 1064 mesaje în 108 subiecte

Distribuiți | 
 

 SNAKE codul sursa

Vezi subiectul anterior Vezi subiectul urmator In jos 
AutorMesaj
Horace
Admin
Admin
avatar

masculin Numarul mesajelor : 298
Varsta : 24
Muzica : psihedelica
Reputatie : 10
Data de inscriere : 12/05/2007

MesajSubiect: SNAKE codul sursa   26th Octombrie 2009, 4:37 pm

Cod:

program joc_snake;
{
autor: Horatiu Roman.
versiune: 2.2
}
uses crt;
type
    head=record
      x,y:byte;
      length: word;
      speed: word;
      dir: 1..4;
      end;
    body=record
      x,y:byte;
      time:word;
      end;
    foods=record
      x,y:byte;
      power:byte;
      end;

const
initx=20;
inity=20;
initlength=3;
initspeed=1;
initdir=1;
foodchar=char(ord(3));
snakechar=char(ord(219));
screen_vsize=50;
screen_hsize=80;
menu_hsize=20;


var
snake: head;
k:char;
keystep,exit,exit3:boolean;
food:foods;
i:word;
b:array[1..600] of body;
score:longint;
scores:array [1..10] of integer;
scoresn:array [1..10] of string;



procedure writescores;
var f:text;
i:byte;
begin
assign(f,'snake.res');
rewrite(f);
for i:=1 to 10 do begin
  writeln(f,scores[i]);
  writeln(f,scoresn[i]);
  end;
close(f);
end;





procedure readscores(var rst:boolean);
var f:text;
i:byte;
err:integer;
begin
assign(f,'snake.res');
{$I-}
reset(f);
{$I+}
err:=IOResult;
if err<>0 then
  begin
  clrscr;
  writeln('Error ',err,' occured. Ask adult for help.');
  delay(2500);
  end;
if err=2 then
  begin
  rewrite(f);
  close(f);
  reset(f);
  writescores;
  rst:=true;
  end
  else
  rst:=false;
for i:=1 to 10 do begin
  readln(f,scores[i]);
  readln(f,scoresn[i]);
  end;
close(f);
end;




procedure init;
var rst:boolean;
begin
rst:=false;
repeat
textmode(C80+font8x8);
textcolor(white);
exit:=false;
exit3:=false;
score:=0;
snake.x:=initx;
snake.y:=inity;
snake.length:=initlength;
snake.speed:=initspeed;
snake.dir:=initdir;
food.x:=random(screen_hsize-menu_hsize-3)+2;
food.y:=random(screen_vsize-3)+2;
food.power:=0;
readscores(rst);
until rst=false;
end;


procedure clrscr2(x1,y1,x2,y2:byte);
var i,j:byte;
begin
for i:=x1 to x2 do
  for j:=y1 to y2 do begin
    gotoxy(i,j); write(' ');
    end;
end;




procedure showscores;
var i:byte;
begin
clrscr2(2,2,screen_hsize-menu_hsize-2,screen_vsize-2);
gotoxy((screen_hsize-menu_hsize) div 2 - 7,5); write('Highscores list');
for i:=1 to 10 do begin
  gotoxy(trunc((screen_hsize-menu_hsize)/7),10+i); write(scoresn[i]);
  gotoxy(trunc((screen_hsize-menu_hsize)*6/7),10+i); write(scores[i]);
  end;
end;




procedure gameover;
var str: string[10];
i,j:byte;
weak:boolean;
begin
clrscr2(2,2,screen_hsize-menu_hsize-1,screen_vsize-1);
snake.speed:=0;
gotoxy(trunc((screen_hsize-menu_hsize)/7),screen_vsize div 2);
textcolor(red);
write('Game is over, you are dead');
gotoxy(trunc((screen_hsize-menu_hsize)/7),screen_vsize div 2+1);
write('Please enter name: ');textcolor(white);
readln(str);
gotoxy(trunc((screen_hsize-menu_hsize)/7),screen_vsize div 2+2); textcolor(red);
weak:=true;
for i:=1 to 10 do begin
  if score>scores[i] then begin
                          for j:=10 downto i+1 do begin
                              scores[j]:=scores[j-1];
                              scoresn[j]:=scoresn[j-1];
                              end;
                          scores[i]:=score;
                          scoresn[i]:=str;
                          weak:=false;
                          break;
                          end;
  end;
if weak then i:=11;
case i of
1: write(str,' is the new champion!!!');
2..3: write(str,' is in the top 3!!');
4..9: write(str,' is in the highscores!');
10: write(str,' was quite lucky.');
11: if screen_hsize-menu_hsize>30 then write('My grandma plays better than ',str) else write(str,' sucks');
end;
    sound(340);delay(100);nosound;
    sound(380);delay(100);nosound;
    sound(420);delay(100);nosound;
    sound(445);delay(160);nosound;
    delay(700);

writescores;
exit:=true;
showscores;
gotoxy(trunc((screen_hsize-menu_hsize)*2/7),screen_vsize-2);write('Exit? (esc)');
if readkey=#27 then exit3:=true;
end;




procedure keys;
begin
if keypressed then
  k:=readkey;
  if keystep then begin
  case k of
  #27: begin
      exit:=true; exit3:=true; gotoxy(20,20); textcolor(red); writeln('Exiting');
      end;
  {wasd}
  'd': if snake.dir<>3 then snake.dir:=1;
  'w': if snake.dir<>4 then snake.dir:=2;
  'a': if snake.dir<>1 then snake.dir:=3;
  's': if snake.dir<>2 then snake.dir:=4;
  {numpad}
  '6': if snake.dir<>3 then snake.dir:=1;
  '8': if snake.dir<>4 then snake.dir:=2;
  '4': if snake.dir<>1 then snake.dir:=3;
  '5': if snake.dir<>2 then snake.dir:=4;
  '2': if snake.dir<>2 then snake.dir:=4;
  '9': begin if snake.speed < 10 then begin inc(snake.speed); k:=char(ord(186)) end; end;
  'e': begin if snake.speed < 10 then begin inc(snake.speed); k:=char(ord(186)) end; end;
{  'q': begin if snake.speed > 1 then begin dec(snake.speed); k:=char(ord(186)) end; end; }
  end;
  keystep:=false;
  end;
end;



procedure drawmenu;
var i:byte;
begin
clrscr;
for i:=2 to screen_vsize do begin
  gotoxy(screen_hsize-menu_hsize,i);write(char(ord(186))); { mijloc vertical }
  end;
for i:=2 to screen_hsize-1 do begin
  gotoxy(i,1);write(char(ord(205)));          { linia sus }
  gotoxy(i,screen_vsize);write(char(ord(205)));        { linia jos }
  end;
gotoxy(screen_hsize-menu_hsize,1);write(char(ord(203)));    {mijloc sus}
gotoxy(1,1);write(char(ord(201)));    {  sus stg }
for i:=2 to screen_vsize-1 do begin
  gotoxy(1,i);write(char(ord(186)));        {linia stg }
  gotoxy(screen_hsize-1,i);write(char(ord(186)));        { linia dr }
  end;
gotoxy(1,screen_vsize);write(char(ord(200)));          { stg  jos }
gotoxy(screen_hsize-1,screen_vsize);write(char(ord(188)));          {  dr jos  }
gotoxy(screen_hsize-1,1);write(char(ord(187)));          {  dr sus  }
gotoxy(screen_hsize-menu_hsize,screen_vsize);write(char(ord(202)));          {mijloc jos}

gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-3,7);write('SNAKE'); {titlu}

gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-4,9);write('Speed:');    {69,9 - viteza    }
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-4,10);write('Length:');  {70,10 - lungimea  }
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-4,11);write('Score:');  {69,11 - scor      }
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-5,13);write('Alt+Enter');
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2-5,14);write('Fullscreen');
textcolor(darkgray);
if screen_vsize>=40 then begin
gotoxy(screen_hsize-menu_hsize+1,30);write('    controls:    ');
gotoxy(screen_hsize-menu_hsize+1,31);write('  (numlock on)  ');
gotoxy(screen_hsize-menu_hsize+1,32);write('  8, w  =  sus  ');
gotoxy(screen_hsize-menu_hsize+1,33);write('  4, a  =  stg  ');
gotoxy(screen_hsize-menu_hsize+1,34);write('  6, d  =  dr.  ');
gotoxy(screen_hsize-menu_hsize+1,35);write('  2,5,s  =  jos  ');
gotoxy(screen_hsize-menu_hsize+1,36);write(' 9,e=creste viteza');
gotoxy(screen_hsize-menu_hsize+1,37);write('                  ');
gotoxy(screen_hsize-menu_hsize+1,38);write('  Viteza creste  ');
gotoxy(screen_hsize-menu_hsize+1,39);write('      singura    ');
end;
gotoxy(20,20);
end;


procedure draw;
var i:word;
begin
for i:=1 to snake.length do begin
  gotoxy(b[i].x,b[i].y);textcolor(lightgray);write(snakechar);
  end;
gotoxy(snake.x,snake.y);textcolor(white);write(snakechar);
gotoxy(food.x,food.y);textcolor(green);write(foodchar);
{speed}
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2+3,9);textcolor(white);write(snake.speed,' ');
{length}
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2+4,10);write(snake.length,' ');
{score}
gotoxy(screen_hsize-menu_hsize+menu_hsize div 2+3,11);write(score,'  ');

gotoxy(screen_hsize-3,screen_vsize-2);
end;


procedure increasespeed;
begin
if  (snake.length div 7 >= snake.speed) and
    (snake.speed<10)
  then inc(snake.speed);

end;


procedure step;
var i:word;
begin
gotoxy(b[1].x,b[1].y);write(' ');
for i:=1 to snake.length-1 do begin
  b[i].x:=b[i+1].x;
  b[i].y:=b[i+1].y;
  b[i].time:=b[i+1].time;
  dec(b[i].time);
  end;
case snake.dir of
1: if snake.x<screen_hsize-menu_hsize-1 then inc(snake.x) else if not exit then gameover;
2: if snake.y>2 then dec(snake.y) else if not exit then gameover;
3: if snake.x>2 then dec(snake.x) else if not exit then gameover;
4: if snake.y<screen_vsize-1 then inc(snake.y) else if not exit then gameover;
end;
if (snake.x=food.x) and (snake.y=food.y) then begin
  inc(snake.length);
  increasespeed;
  food.x:=random(screen_hsize-menu_hsize-3)+2;
  food.y:=random(screen_vsize-3)+2;
  score:=score+snake.speed;
  end;
for i:=1 to snake.length do
  if (b[i].x=snake.x) and (b[i].y=snake.y) then if not exit then gameover;
  b[snake.length].x:=snake.x;
  b[snake.length].y:=snake.y;
  b[snake.length].time:=snake.length+1;
draw;
end;


procedure speedhelper;
begin
if not exit then
        begin
        keys;
        delay(5);
        end;
end;


procedure speed;
var i:byte;
begin
case snake.speed of
    0: ;
    1: for i:=1 to 40 do speedhelper;
    2: for i:=1 to 32 do speedhelper;
    3: for i:=1 to 26 do speedhelper;
    4: for i:=1 to 20 do speedhelper;
    5: for i:=1 to 16 do speedhelper;
    6: for i:=1 to 12 do speedhelper;
    7: for i:=1 to 9 do speedhelper;
    8: for i:=1 to 7 do speedhelper;
    9: for i:=1 to 6 do speedhelper;
    10: for i:=1 to 5 do speedhelper;
    else;
    end;
end;



begin
randomize;
repeat
init;
drawmenu;
repeat
keystep:=true;
speed;
step;
until exit;

until exit3;
end.


Comentarii, sugestii, etc.

Daca intamplator va vine ideea de a fura codul si a il folosi in programele voastre, ... sunteti bineveniti sa il folositi dar sa ma treceti totusi ca partial autor/ajutor. Nu e frumos sa plagiezi. codul asta l-am facut singur, din cunostintele mele si fara coduri copiate si lipite in program.

Unde credeti ca s-ar putea imbunatati, schimba, scurta, etc, va rog sa dati idei. am sa va tin minte si am sa scriu in acest subiect la sfarsit ca m-ati ajutat. Smile

_________________
de obicei nu am dreptate. dovedeste!
Sus In jos
Vezi profilul utilizatorului http://horacehomepage.great-forum.com
 
SNAKE codul sursa
Vezi subiectul anterior Vezi subiectul urmator Sus 
Pagina 1 din 1

Permisiunile acestui forum:Nu puteti raspunde la subiectele acestui forum
Forum || Horace Homepage :: Discutii :: Informatica si calculatoare-
Mergi direct la: