Sunteți pe pagina 1din 36

COLEGIUL MILITAR LICEAL DIMITRIE CANTEMIR

LUCRARE PENTRU OBTINEREA ATESTATULUI PROFESIONAL LA INFORMATICA

PROFESOR COORDONATOR: DICHEI NICOLETA

ELEV: B DEANU IONU-VALENTIN

BREAZA, 2013

ANIMAIE DE TETRIS

INTRODUCERE

n ultimul timp este evident faptul c mrimea coeficientului de inteligen al unei naiuni depinde,din ce n ce mai mult,de modul n care aceasta se integreaz n efortul de asimilare i utilizare a tehnologiilor informaionale. Extrem de frmntata, lumea limbajelor de programare este indisolubil legat de evoluia spectaculoas,ntr-un interval de timp destul de redus, a sistemelor de calcul electronice(SCE). Dependente de iniiativa i capacitatea omului de a le structura capabilitaile fundamentale,SCE nu se pot exprima dect n prezena unor sisteme de comunicaie ca acesta. Potenialul de calcul al unui sistem SCE,privit ca main fizic, poate fi valorificat i chiar amplificat, cu ajutorul suportului ferit de mainile virtuale(sisteme de operare,sisteme de gestiune a bazelor de date,limbaje de programare,medii vizuale de programare,etc).

Limbajul prin care se transmit datele ctre un SCE la nivelul cel mai de jos se numete limbaj main. Alfabetul unui astfel de limbaj este format de simboluri apar innd multimii {0,1}. Urmtorul program realizat n limbajul de programare Turbo Pascal este un joc clasic de Tetris.S-au folosit urmtoarele unit-uri n crearea acestui joc: Crt.

Variabilele folosite n acest program sunt: type tip_nume= record nume:string; scor,cuburi,linii:longint; end; type mat=array[0..3,0..7] of integer; var f:text; inchidere:boolean; linii,cuburi,scor:longint; nume_jucator:string[20]; buton,x,y:integer; timp,_timp:longint; muta_piesa,piesa_next:integer; mutare:boolean; nr_linii:longint; joc:array[1..31,1..19] of integer; mx,my:integer; muta_piesa_ajutor,_muta_piesa_ajutor:mat; culoare_cuburi:integer;{culoare cuburilor este rosie} nume:array[1..20] of tip_nume; c:char; clock:longint absolute $0:$046c; endt,start:longint; Procedurile care sunt folosite n acest program sunt: procedure hidecursor;assembler; asm {procedurile urmtoare sunt proceduri standard copiate} mov ax,$0100{ascunde cursorul} mov cx,$2607 int $10; end; procedure showcursor;assembler; asm mov ax,$0100{arata cursorul} mov cx,$0507 int $10; end; procedure mousedata(var buton,x,y:integer); var bb,xx,yy:integer; begin{returneaz date referitoare la poziia cursorului mousului i a butoanelor}

asm mov ax,3 int $33 mov bb,bx mov xx,cx mov yy,dx end; buton:=bb; if buton=1 then delay(100); {buton=0 - nu s-a dat click buton=1 - s-a dat click cu primul buton buton=2 - s-a dat click cu al doilea buton buton=3 - s-a dat click cu ambele butoane } y:=yy div 8 +1; x:=xx div 8 +1; {x,y sunt coordonatele unde se afla cursorul mousului } end; procedure mouseinit; assembler; asm mov ax,0; int $33; {iniializeaz driverul mousului} end; procedure mouseshow; assembler; asm mov ax,1; int $33; {d comanda pentru afiarea cursorului mousului} end; procedure mousehide; assembler; asm mov ax,2; int $33; {d comand pentru ascunderea cursorului mousului} end; procedure modtext; begin asm mov ax,3 int 10h end; end; procedure deseneaza_buton(x,y:integer;s1,s2:string); begin {deseneaz un buton n spaiu} textbackground(GREEN); textcolor(WHITE); gotoxy(x,y); write(s1); textcolor(BLACK); write(s2); textbackground(BLUE); textcolor(BLACK); write(''); gotoxy(x+1,y+1); write(''); end; procedure creare; var f:text; i:integer; begin

{ajut la crearea automat a fiierului top dac acesta nu exist} assign(f,'top.txt'); rewrite(F); for i:=1 to 20 do writeln(f,'Nimeni'); for i:=1 to 20 do writeln(f,'0 0 0'); close(f); end; procedure citire_top; var j,i:integer; begin assign(f,'top.txt'); reset(F);{procedura care citete datele din fiierul top} if IOresult<>0 then begin creare; reset(f); end; for i:=1 to 20 do readln(f,nume[i].nume); for i:=1 to 20 do readln(f,nume[i].scor,nume[i].cuburi,nume[i].linii); close(f); end; procedure scriere_top; var i:integer; begin assign(f,'top.txt'); rewrite(f);{procedura care scrie datele n fiierul top} for i:=1 to 20 do writeln(f,nume[i].nume); for i:=1 to 20 do writeln(f,nume[i].scor,' ',nume[i].cuburi,' ',nume[i].linii); close(f); end; procedure scriere_top; var i:integer; begin assign(f,'top.txt'); rewrite(f);{procedura care scrie datele n fiierul top} for i:=1 to 20 do writeln(f,nume[i].nume); for i:=1 to 20 do writeln(f,nume[i].scor,' ',nume[i].cuburi,' ',nume[i].linii); close(f); end; procedure verificare_top; var pozitia,i:integer; in_top:boolean; begin {procedura care verific dac juctorul intra n top sau nu} in_top:=false; citire_top;{citim datele din top} for i:=1 to 20 do if (nume[i].scor<scor) then begin{vedem n ce poziie se afl juctorul nostru} pozitia:=i; in_top:=true; break; end; if not in_top then exit; {dac nu intr n top atunci ieim} {dac intr atunci mutm restul de la poziia lui cu o poziie mai n jos n top} for i:=19 downto pozitia do begin nume[i+1].nume:=nume[i].nume; nume[i+1].scor:=nume[i].scor; nume[i+1].cuburi:=nume[i].cuburi; nume[i+1].linii:=nume[i].linii;

end; {acum l introducem i pe el n top} nume[pozitia].nume:=nume_jucator; nume[pozitia].scor:=scor; nume[pozitia].linii:=linii; nume[pozitia].cuburi:=cuburi; scriere_top; {acum scriem noul top} gotoxy(26,1); textcolor(RED+BLANK); textbackground(BLACK); write('Sunteti in Top pe pozitia ',pozitia); {dm i un mesaj c a intrat n top} if inchidere then readkey; end; procedure piesa_noua; forward; procedure initjoc; var i,j:integer; begin {procedura care deseneaz interfaa jocului} timp:=20; modtext; linii:=0;{iniializm toate variabilele de care avem nevoie} cuburi:=0; scor:=0; hidecursor; textbackground(BLUE); textcolor(WHITE); window(6,3,75,23); clrscr; textbackground(BLACK); gotoxy(1,21); write(' '); textbackground(BLUE); gotoxy(1,1); write(' Tetris '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(''); window(8,4,37,22); textbackground(BLACK);{desenm acele dreptunghiuri negre} write(' '); write(' '); write(' '); write(' '); write(' ');

write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); window(1,1,80,25); gotoxy(39,4); textbackground(BLUE); textcolor(WHITE); write('Nume : '); if Nume_jucator='' then write(' click ptr nume') else write(nume_jucator); {scriem tot ce ne mai trebuie} gotoxy(39,5); write('Linii :'); gotoxy(39,6); write('Cuburi :'); gotoxy(39,7); write('Scor :'); gotoxy(39,9); write(' Next '); window(39,10,58,20); textbackground(BLACK); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); window(1,1,80,25); {desenm i butoanele noastre} deseneaza_buton(62,10,' N','ew '); deseneaza_buton(62,12,' L','evel '); deseneaza_buton(62,14,' P','auza '); deseneaza_buton(62,16,' T','op '); deseneaza_buton(62,18,' C','lose '); textcolor(WHITE); textbackground(BLUE); gotoxy(48,5); write(linii); gotoxy(48,6); write(cuburi); gotoxy(48,7); write(scor); for j:=1 to 30 do for i:=1 to 18 do joc[j,i]:=0; {iniializm ecranul nostru nu 0 pentru c nu este absolut nimic pe el} piesa_noua;

end; procedure pauza; begin deseneaza_buton(62,14,' R','enire '); _timp:=timp;{ca s nu pierdem levelul la care eram} timp:=0;{punnd n timp 0 atunci oprim jocul} end; procedure resume; begin deseneaza_buton(62,14,' P','auza '); timp:=_timp;{revenim la timpul } end; procedure apasat(x,y:integer;s1,s2:string); begin {procedur asemntoare cu procedura deseneaz_buton numai c acum vom da impresia c butonul este apsat} textbackground(GREEN); textcolor(WHITE); gotoxy(x+1,y); write(s1); textcolor(BLACK); write(s2); textbackground(BLUE); textcolor(BLACK); gotoxy(x+1,y+1); write(' '); gotoxy(x,y); write(' '); delay(100); deseneaza_buton(x,y,s1,s2); end; procedure level; var c:char; citit:boolean; begin {procedura n care citim levelul dorit} citit:=false; textbackground(BLACK); textcolor(WHITE); gotoxy(24,24); write('Scrieti level-ul dorit (0-9) :'); {de fapt vom citi un caracter care trebuie s fie ESC sau o cifr } while (not citit) do begin showcursor; c:=readkey; if c=#27 then citit:=true; if ((ord(c)-48>=0)and(ord(c)-48<10)) then begin write(c); timp:=20-(ord(c)-48)*2;{cu ct levelul este mai mare cu att timpul este mai mic} citit:=true; end; end; delay(1000); hidecursor; gotoxy(24,24); write(' '); end; procedure piesa_noua; var i,j:integer;

gasit:boolean; cat_de_mult:integer; continuare:boolean; begin cat_de_mult:=0; textbackground(BLACK); {piesa care se afla n Next este tears} for i:=0 to 3 do for j:=0 to 7 do begin if (p[piesa_next][i][j]=1) then begin gotoxy(44+j,13+i); write(' '); end end; textbackground(culoare_cuburi); muta_piesa:=piesa_next; piesa_next:=random(7); {e deseneaz n Next noua pies} for i:=0 to 3 do for j:=0 to 7 do begin if (p[piesa_next][i][j]=1) then begin gotoxy(44+j,13+i); write(' '); end end; {aici se verific cte linii din pies sunt goale sau pline de 0 pentru c la ptrat de exemplu dac este desenat direct pe ecran va prea c este cu o linie mai jos dect trebuie} for i:=0 to 3 do begin for j:=0 to 7 do if (p[muta_piesa][i][j]=1) then begin muta_piesa_ajutor[i][j]:=1; gasit:=true; end else muta_piesa_ajutor[i][j]:=0; if (not gasit) then inc(cat_de_mult); end; {este desenat piesa pe ecran cu ct_de_mult mai sus (daca este nevoie)} for i:=cat_de_mult to 3 do for j:=0 to 7 do if (p[muta_piesa][i][j]=1) then begin gotoxy(20+j,4+i-cat_de_mult); write(' '); if (joc[13+j][1+i-cat_de_mult]=1) then continuare:=false; {n acest if se verific dac se mai poate continua jocul nu se va mai putea dac desenm piesa undeva unde exist loc ocupat => terminarea jocului} end; mx:=13; my:=1-cat_de_mult; start:=clock;{iniializm variabilele de care avem nevoie} endt:=start; textcolor(WHITE); textbackground(BLUE);

cuburi:=cuburi+4; gotoxy(48,6); write(cuburi); scor:=linii*25+cuburi; gotoxy(48,7); write(scor); {dac s-a terminat jocul} if (not continuare) then begin textcolor(WHITE+BLINK); textbackground(BLACK); gotoxy(24,24); write(' End of the game !!! press any key '); verificare_top;{se verific dac dup scor se intr n top} delay(1000); if readkey=#0 then readkey; initjoc;{ reiniializm un alt joc} mouseshow; end end; procedure verificare_linie; var exista_linie:boolean; i,j,k:integer; begin i:=1; {dup ce o pies a ajuns jos atunci verificm dac nu s-a format vreo linie complet care va urma s fie tears} while(i<=18) do begin exista_linie:=true; for j:=1 to 30 do if (joc[j][i]=0) then exista_linie:=false; {se verific dac exist linie} if (exista_linie) then begin inc(linii); for k:=i-1 downto 1 do for j:=1 to 30 do joc[j][k+1]:=joc[j][k]; {dac a existat linie se mut totul mai jos iar linia de sus ia valoarea 0} for j:=1 to 30 do joc[j][1]:=0; for k:=i-1 downto 1 do begin for j:=1 to 30 do begin if (joc[j][k+1]=1) then textbackground(culoare_cuburi) else textbackground(BLACK); gotoxy(7+j,4+k); write(' '); end; {se redeseneaz tot ecranul} end end else inc(i);; end; gotoxy(48,5); textcolor(WHITE); textbackground(BLUE); write(linii); scor:=linii*25+cuburi; gotoxy(48,7);

10

write(scor); if ((linii mod 100=0)and(timp>2)and (linii>0)) then timp:= timp-2; {dac numrul de linii este 100, 200, 300 .. atunci mrim levelul} end; procedure lucru_cu_piesa(m:integer); {procedura n care mutm piesa la stnga la dreapta n jos sau o rotim} var i,j:integer; se_poate:boolean; begin se_poate:=true; if (m=1) then{mutare se stinga} begin for i:=0 to 3 do begin j:=0; while ((j<=7)and(muta_piesa_ajutor[i][j]=0)) do inc(j); {se parcurge matricea pentru a se afla unde se afl un cub(1) pe acea linie} if (j<=7) then {daca a existat un cub pe acea linie} if (mx+j-1<=0) then se_poate:=false {dac nu am ajuns la marginea din stng} else if (joc[mx+j-1][my+i]=1) then se_poate:=false; {i dac nu exit alt cub n stnga} end; if (se_poate) then {dac se poate muta} begin textbackground(BLACK); {se sterge piesa din locul vechi} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); dec(mx,2);{se schimb coordonatele piesei} {se redeseneaz piesa pe noile coordonate} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end; end; if (m=2) then{mutare dreapta} begin for i:=0 to 3 do begin j:=7; while ((j>=0)and(muta_piesa_ajutor[i][j]=0)) do dec(j); {totul este la fel ca la stnga numai c acum se verific dac exit un cub pe partea dreapt} if (j>=0) then {dac a existat un cub pe acea linie} if (mx+j+1>29) then se_poate:=false { dac nu am ajuns la marginea din dreapta} else if (joc[mx+j+1][my+i]=1) then se_poate:=false;

11

{i dac nu exit alt cub n dreapta} if (se_poate) then {daca se poate muta} begin textbackground(BLACK); {se terge piesa din locul vechi} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); inc(mx,2);{se schimba coordonatele piesei} {se redeseneaz piesa pe noile coordonate} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end end; if (m=3) then {nvrtirea piesei} begin for i:=0 to 3 do for j:=0 to 7 do _muta_piesa_ajutor[i][j]:=0; {n _muta_piesa_ajutor vom avea piesa rotit} for i:=0 to 3 do begin j:=6; while (j>=0) do begin _muta_piesa_ajutor[3-(j div 2)][i*2]:=muta_piesa_ajutor[i][j]; j:=j-2;; end;{aici este de fapt rotirea n adevaratul sens} for j:=0 to 3 do _muta_piesa_ajutor[j][i*2+1]:=_muta_piesa_ajutor[j][i*2]; end; for i:=0 to 3 do for j:=0 to 7 do if (_muta_piesa_ajutor[i][j]=1) then if ((mx+j<=0)or(mx+j>30)or(my+i<=0)or(my+i>18)) then se_poate:=false else{aici verificm dac piesa poate fi mutat dac nu se afl la margine sau exist o piesa care o mpiedic} if (joc[mx+j][my+i]=1) then se_poate:=false; if (se_poate) then begin{dac putem, atunci redesenm} textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do{tergem piesa} if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; for i:=0 to 3 do for j:=0 to 7 do{mutm piesa rotit n variabila cu care lucrm} muta_piesa_ajutor[i][j]:=_muta_piesa_ajutor[i][j]; end;

12

textbackground(culoare_cuburi); for i:=0 to 3 do{i acum o desenm pe ecran} for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end ; end; if (m=4) then {coborire} begin for j:=0 to 7 do begin {totul la fel ca la stinga si dreapta} i:=3; while ((i>=0)and(muta_piesa_ajutor[i][j]=0)) do dec(i); {se parcurge matricea pentru a se afla unde se afl un cub(1) pe acea coloana de jos n sus} if (i>=0) then {dac a existat un cub} if (my+i+1>18) then se_poate:=false {i nu am ajuns jos} else if (joc[mx+j][my+i+1]=1) then se_poate:=false; {i nu am ntlnit un alt cub} end; if (se_poate) then {dac se poate muta} begin {tergere pies} textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); inc(my);{schimbare pies} {redesenare pies} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end else begin {nu s-a putut muta, deci a ajuns piesa jos} for i:=0 to 3 do for j:=0 to 7 do begin if (muta_piesa_ajutor[i][j]=1) then joc[j+mx][i+my]:=1; end; {se verific dac s-a ocupat o linie ntreag } verificare_linie; {se aduce noua pies} piesa_noua;

13

end; end end; procedure schimbare_nume; var citire:boolean; caractere:set of char; c:char; begin citire:=true; caractere:=['A'..'Z','a'..'z',' ','_','-',',']; {caractere care sunt acceptate pentru un nume} textbackground(BLUE); textcolor(WHITE); gotoxy(48,4); write(' '); {dac a mai existat un nume, el este ters} gotoxy(48,4); nume_jucator:=''; showcursor; {este lsat cursoul s apar} {nu se folosete read ci se citesc multiple caractere cu ajutorul func iei readkey} while (citire) do begin c:=readkey; {citim un caracter} if (c=#13) then citire:=false; {dac caracterul citit este ENTER atunci citirea se opre te} if (length(nume_jucator)>=20) then gotoxy(68,4) {numele nu poate avea mai mult de 20 caractere} else if (c in caractere) then begin {dac caracterul nu a fost BACKSPACE el este tiprit} gotoxy(48,4); nume_jucator:=nume_jucator+c; write(nume_jucator); end; if (c=#8) then if (nume_jucator<>'') then begin {este tiprit numele din nou} nume_jucator[length(nume_jucator)]:=' '; {se d efect c ar fi sters respectivul char} gotoxy(48,4); write(nume_jucator); delete(nume_jucator,length(nume_jucator),1); gotoxy(48,4); write(nume_jucator); end; end; hidecursor; end; procedure top; type patretel=record c:char; cul:byte; end; type monitor=array [1..25,1..80] of patretel; var i,j:integer; ecran:monitor absolute $b800:0; {exact ca la timp declarm o variabil dar de date aceasta peste

14

memoria video unde se afl ceea ce vedem pe ecran} sav:monitor; begin sav:=ecran;{salvam ecranul actual} textcolor(WHITE); textbackground(BLUE); gotoxy(1,1); write(' Tetris Top 20 '); write(' Nr. Nume Scor Cuburi Linii '); write(''); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(''); textbackground(BLACK); textcolor(WHITE+BLINK); gotoxy(28,25); write('press any key to continue'); textbackground(BLUE); textcolor(WHITE); citire_top; {se citeste si mai apoi se scriu valorile} for i:=1 to 20 do begin gotoxy(2,3+i); for j:=length(nume[i].nume) to 21 do nume[i].nume:=nume[i].nume+' '; write(i:5,'. ',nume[i].nume,nume[i].scor:8, nume[i].cuburi:15,nume[i].linii:17); end; readkey; ecran:=sav;{refacem ecranul din salvare} end; begin culoare_cuburi:=red; piesa_next:=random(7); nume_jucator:='';{iniializm valorile de care avem nevoie} randomize; mouseinit; start:=clock; endt:=start; mutare:=false; initjoc;{desenm ecranul} mouseshow; c:=#0;inchidere:=false;

15

while ((c<>#27)and(not inchidere)) do begin c:=#0; if (((endt - start) >=timp )and(timp<>0)) then begin {dac a trecut timpul atunci cobor m piesa} start:=clock; endt:=start; mutare:=true; end else endt:=clock; if (mutare) then begin lucru_cu_piesa(4); {aici se coboar piesa} mutare:=false; end; mousedata(buton,x,y); if (keypressed) then begin c:=readkey;{daca s-a apsat vreo tasta atunci ea este citit} if ((c=#0)and(timp<>0)) then begin c:=readkey; {acestea sunt caracterele transmise de tastele direc ionale} if (c=#72) then lucru_cu_piesa(3); if (c=#77) then lucru_cu_piesa(2); if (c=#80) then lucru_cu_piesa(4); if (c=#75) then lucru_cu_piesa(1); c:=#0; end; {i acum dac s-a apsat una dintre tastele care apar cu alb pe butoane} if ((c='n')or(c='N')) then begin mousehide; apasat(62,10,' N','ew '); initjoc; mouseshow; end; if ((c='l')or(c='L')) then begin mousehide; apasat(62,12,' L','evel '); level; mouseshow; end; if (((c='p')or(c='P'))and(timp>0)) then begin mousehide; apasat(62,14,' P','auza '); pauza; mouseshow; end; if (((c='r')or(c='R'))and(timp=0)) then begin mousehide; apasat(62,14,' R','enire '); resume; mouseshow; end; if ((c='t')or(c='T')) then begin

16

mousehide; apasat(62,16,' T','op '); top; mouseshow; end; if ((c='c')or(c='C')) then begin mousehide; apasat(62,18,' C','lose '); inchidere:=true; mouseshow; end; end; if (buton<>0) then begin {acum dac s-a dat click cumva} mousehide; {se verific pentru fiecare buton} if ((y=10)and(x>=62)and(x<=71)) then begin apasat(62,10,' N','ew '); initjoc; end; if ((y=12)and(x>=62)and(x<=71)) then begin apasat(62,12,' L','evel '); level; end; if ((y=14)and(x>=62)and(x<=71)) then begin if (timp<>0) then begin apasat(62,14,' P','auza '); pauza; delay(100); end else begin apasat(62,14,' R','enire '); resume; delay(100); end; end; if ((y=16)and(x>=62)and(x<=71)) then begin apasat(62,16,' T','op '); top; end; if ((y=18)and(x>=62)and(x<=71)) then begin apasat(62,18,' C','lose '); inchidere:=true; end; if ((y=4)and(x>=47)and(x<=71)) then begin schimbare_nume; end; mouseshow; end; end; inchidere:=true; {verificm i topul la sfrit} verificare_top; end. Apoi urmeaz corpul programului:

17

begin culoare_cuburi:=red; piesa_next:=random(7); nume_jucator:='';{iniializm valorile de care avem nevoie} randomize; mouseinit; start:=clock; endt:=start; mutare:=false; initjoc;{desenm ecranul} mouseshow; c:=#0;inchidere:=false; while ((c<>#27)and(not inchidere)) do begin c:=#0; if (((endt - start) >=timp )and(timp<>0)) then begin {dac a trecut timpul atunci cobor m piesa} start:=clock; endt:=start; mutare:=true; end else endt:=clock; if (mutare) then begin lucru_cu_piesa(4); {aici se coboarea piesa} mutare:=false; end; mousedata(buton,x,y); if (keypressed) then begin c:=readkey;{dac s-a apasat vreo tasta atunci ea este citit} if ((c=#0)and(timp<>0)) then begin c:=readkey; {acestea sunt caracterele transmise de tastele direc ionale} if (c=#72) then lucru_cu_piesa(3); if (c=#77) then lucru_cu_piesa(2); if (c=#80) then lucru_cu_piesa(4); if (c=#75) then lucru_cu_piesa(1); c:=#0; end; {i acum dac s-a apsat una dintre tastele care apar cu alb pe butoane} if ((c='n')or(c='N')) then begin mousehide; apasat(62,10,' N','ew '); initjoc; mouseshow; end; if ((c='l')or(c='L')) then begin mousehide; apasat(62,12,' L','evel '); level; mouseshow; end; if (((c='p')or(c='P'))and(timp>0)) then begin mousehide; apasat(62,14,' P','auza ');

18

pauza; mouseshow; end; if (((c='r')or(c='R'))and(timp=0)) then begin mousehide; apasat(62,14,' R','enire '); resume; mouseshow; end; if ((c='t')or(c='T')) then begin mousehide; apasat(62,16,' T','op '); top; mouseshow; end; if ((c='c')or(c='C')) then begin mousehide; apasat(62,18,' C','lose '); inchidere:=true; mouseshow; end; end; if (buton<>0) then begin {acum dac s-a dat click cumva} mousehide; {se verific pentru fiecare buton} if ((y=10)and(x>=62)and(x<=71)) then begin apasat(62,10,' N','ew '); initjoc; end; if ((y=12)and(x>=62)and(x<=71)) then begin apasat(62,12,' L','evel '); level; end; if ((y=14)and(x>=62)and(x<=71)) then begin if (timp<>0) then begin apasat(62,14,' P','auza '); pauza; delay(100); end else begin apasat(62,14,' R','enire '); resume; delay(100); end; end; if ((y=16)and(x>=62)and(x<=71)) then begin apasat(62,16,' T','op '); top; end; if ((y=18)and(x>=62)and(x<=71)) then begin apasat(62,18,' C','lose '); inchidere:=true; end;

19

if ((y=4)and(x>=47)and(x<=71)) then begin schimbare_nume; end; mouseshow; end; end; inchidere:=true; {verificm i topul la sfrit} verificare_top; end. Codul Surs: uses crt; type tip_nume= record nume:string; scor,cuburi,linii:longint; end; type mat=array[0..3,0..7] of integer; const p:array[0..6] of mat =( ( (0,0,0,0,0,0,0,0), (0,0,1,1,1,1,0,0), (0,0,1,1,1,1,0,0), (0,0,0,0,0,0,0,0) ), ( (0,0,0,0,1,1,0,0), (0,0,0,0,1,1,0,0), (0,0,0,0,1,1,0,0), (0,0,0,0,1,1,0,0) ), ( (0,0,0,0,0,0,0,0), (0,0,1,1,1,1,1,1), (0,0,0,0,1,1,0,0), (0,0,0,0,0,0,0,0) ), ( (0,0,1,1,1,1,0,0), (0,0,1,1,0,0,0,0), (0,0,1,1,0,0,0,0), (0,0,0,0,0,0,0,0) ), ( (0,0,1,1,1,1,0,0), (0,0,0,0,1,1,0,0), (0,0,0,0,1,1,0,0), (0,0,0,0,0,0,0,0) ), ( (0,0,1,1,0,0,0,0), (0,0,1,1,1,1,0,0), (0,0,0,0,1,1,0,0), (0,0,0,0,0,0,0,0) ), ( (0,0,0,0,1,1,0,0), (0,0,1,1,1,1,0,0), (0,0,1,1,0,0,0,0), (0,0,0,0,0,0,0,0) )

20

); var f:text; inchidere:boolean; linii,cuburi,scor:longint; nume_jucator:string[20]; buton,x,y:integer; timp,_timp:longint; muta_piesa,piesa_next:integer; mutare:boolean; nr_linii:longint; joc:array[1..31,1..19] of integer; mx,my:integer; muta_piesa_ajutor,_muta_piesa_ajutor:mat; culoare_cuburi:integer; nume:array[1..20] of tip_nume; c:char; clock:longint absolute $0:$046c; endt,start:longint; {--------------------------------------------------------------} procedure hidecursor;assembler; asm mov ax,$0100 mov cx,$2607 int $10; end; {--------------------------------------------------------------} procedure showcursor;assembler; asm mov ax,$0100 mov cx,$0507 int $10; end; {--------------------------------------------------------------} procedure mousedata(var buton,x,y:integer); var bb,xx,yy:integer; begin asm mov ax,3 int $33 mov bb,bx mov xx,cx mov yy,dx end; buton:=bb; if buton=1 then delay(100); y:=yy div 8 +1; x:=xx div 8 +1; end; {--------------------------------------------------------------} procedure mouseinit; assembler; asm mov ax,0; int $33; end; {--------------------------------------------------------------} procedure mouseshow; assembler; asm mov ax,1; int $33;

21

end; {--------------------------------------------------------------} procedure mousehide; assembler; asm mov ax,2; int $33; end; {--------------------------------------------------------------} procedure modtext; begin asm mov ax,3 int 10h end; end; {--------------------------------------------------------------} procedure deseneaza_buton(x,y:integer;s1,s2:string); begin textbackground(GREEN); textcolor(WHITE); gotoxy(x,y); write(s1); textcolor(BLACK); write(s2); textbackground(BLUE); textcolor(BLACK); write(''); gotoxy(x+1,y+1); write(''); end; {--------------------------------------------------------------} procedure creare; var f:text; i:integer; begin assign(f,'top.txt'); rewrite(F); for i:=1 to 20 do writeln(f,'Nimeni'); for i:=1 to 20 do writeln(f,'0 0 0'); close(f); end; {--------------------------------------------------------------} procedure citire_top; var j,i:integer; begin assign(f,'top.txt'); reset(F); if IOresult<>0 then begin creare; reset(f); end; for i:=1 to 20 do readln(f,nume[i].nume); for i:=1 to 20 do readln(f,nume[i].scor,nume[i].cuburi,nume[i].linii); close(f); end; {--------------------------------------------------------------} procedure scriere_top; var i:integer; begin assign(f,'top.txt'); rewrite(f); for i:=1 to 20 do writeln(f,nume[i].nume);

22

for i:=1 to 20 do writeln(f,nume[i].scor,' ',nume[i].cuburi,' ',nume[i].linii); close(f); end; {--------------------------------------------------------------} procedure verificare_top; var pozitia,i:integer; in_top:boolean; begin in_top:=false; citire_top;{citim datele din top} for i:=1 to 20 do if (nume[i].scor<scor) then begin pozitia:=i; in_top:=true; break; end; if not in_top then exit; for i:=19 downto pozitia do begin nume[i+1].nume:=nume[i].nume; nume[i+1].scor:=nume[i].scor; nume[i+1].cuburi:=nume[i].cuburi; nume[i+1].linii:=nume[i].linii; end; {acum ii introducem si pe el in top} nume[pozitia].nume:=nume_jucator; nume[pozitia].scor:=scor; nume[pozitia].linii:=linii; nume[pozitia].cuburi:=cuburi; scriere_top; gotoxy(26,1); textcolor(RED+BLINK); textbackground(BLACK); write('Sunteti in Top pe pozitia ',pozitia); if inchidere then readkey; end; {--------------------------------------------------------------} procedure piesa_noua; forward; {--------------------------------------------------------------} procedure initjoc; var i,j:integer; begin timp:=20; modtext; linii:=0; cuburi:=0; scor:=0; hidecursor; textbackground(BLUE); textcolor(WHITE); window(6,3,75,23); clrscr; textbackground(BLACK); gotoxy(1,21); write(' '); textbackground(BLUE); gotoxy(1,1); write(' Tetris '); write(' '); write(' '); write(' ');

23

write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(''); window(8,4,37,22); textbackground(BLACK); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); window(1,1,80,25); gotoxy(39,4); textbackground(BLUE); textcolor(WHITE); write('Nume : '); if Nume_jucator='' then write(' click ptr nume') else write(nume_jucator); gotoxy(39,5); write('Linii :'); gotoxy(39,6); write('Cuburi :'); gotoxy(39,7); write('Scor :'); gotoxy(39,9); write(' Next '); window(39,10,58,20); textbackground(BLACK); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' ');

24

write(' '); window(1,1,80,25); deseneaza_buton(62,10,' deseneaza_buton(62,12,' deseneaza_buton(62,14,' deseneaza_buton(62,16,' deseneaza_buton(62,18,' textcolor(WHITE); textbackground(BLUE); gotoxy(48,5); write(linii); gotoxy(48,6); write(cuburi); gotoxy(48,7); write(scor);

N','ew '); L','evel '); P','auza '); T','op '); C','lose ');

for j:=1 to 30 do for i:=1 to 18 do joc[j,i]:=0; piesa_noua; end; {--------------------------------------------------------------} procedure pauza; begin deseneaza_buton(62,14,' R','enire '); _timp:=timp; timp:=0; {--------------------------------------------------------------} procedure resume; begin deseneaza_buton(62,14,' P','auza '); timp:=_timp; end; {--------------------------------------------------------------} procedure apasat(x,y:integer;s1,s2:string); begin textbackground(GREEN); textcolor(WHITE); gotoxy(x+1,y); write(s1); textcolor(BLACK); write(s2); textbackground(BLUE); textcolor(BLACK); gotoxy(x+1,y+1); write(' '); gotoxy(x,y); write(' '); delay(100); deseneaza_buton(x,y,s1,s2); end; {--------------------------------------------------------------} procedure level; var c:char; citit:boolean; begin citit:=false; textbackground(BLACK); textcolor(WHITE); gotoxy(24,24);

25

write('Scrieti level-ul dorit (0-9) :'); while (not citit) do begin showcursor; c:=readkey; if c=#27 then citit:=true; if ((ord(c)-48>=0)and(ord(c)-48<10)) then begin write(c); timp:=20-(ord(c)-48)*2; citit:=true; end; end; delay(1000); hidecursor; gotoxy(24,24); write(' '); end; {--------------------------------------------------------------} procedure piesa_noua; var i,j:integer; gasit:boolean; cat_de_mult:integer; continuare:boolean; begin cat_de_mult:=0; textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do begin if (p[piesa_next][i][j]=1) then begin gotoxy(44+j,13+i); write(' '); end end; textbackground(culoare_cuburi); muta_piesa:=piesa_next; piesa_next:=random(7); for i:=0 to 3 do for j:=0 to 7 do begin if (p[piesa_next][i][j]=1) then begin gotoxy(44+j,13+i); write(' '); end end; for i:=0 to 3 do begin for j:=0 to 7 do if (p[muta_piesa][i][j]=1) then begin muta_piesa_ajutor[i][j]:=1; gasit:=true; end else muta_piesa_ajutor[i][j]:=0; if (not gasit) then inc(cat_de_mult); end; for i:=cat_de_mult to 3 do for j:=0 to 7 do

26

if (p[muta_piesa][i][j]=1) then begin gotoxy(20+j,4+i-cat_de_mult); write(' '); if (joc[13+j][1+i-cat_de_mult]=1) then continuare:=false; end; mx:=13; my:=1-cat_de_mult; start:=clock; endt:=start; textcolor(WHITE); textbackground(BLUE); cuburi:=cuburi+4; gotoxy(48,6); write(cuburi); scor:=linii*25+cuburi; gotoxy(48,7); write(scor); if (not continuare) then begin textcolor(WHITE+BLINK); textbackground(BLACK); gotoxy(24,24); write(' End of the game !!! press any key '); verificare_top; delay(1000); if readkey=#0 then readkey; initjoc; mouseshow; end end; {--------------------------------------------------------------} procedure verificare_linie; var exista_linie:boolean; i,j,k:integer; begin i:=1; while(i<=18) do begin exista_linie:=true; for j:=1 to 30 do if (joc[j][i]=0) then exista_linie:=false; {se verific dac exist linie} if (exista_linie) then begin inc(linii); for k:=i-1 downto 1 do for j:=1 to 30 do joc[j][k+1]:=joc[j][k]; for j:=1 to 30 do joc[j][1]:=0; for k:=i-1 downto 1 do begin for j:=1 to 30 do begin if (joc[j][k+1]=1) then textbackground(culoare_cuburi) else textbackground(BLACK); gotoxy(7+j,4+k); write(' '); end; end end else inc(i);;

27

end; gotoxy(48,5); textcolor(WHITE); textbackground(BLUE); write(linii); scor:=linii*25+cuburi; gotoxy(48,7); write(scor); if ((linii mod 100=0)and(timp>2)and (linii>0)) then timp:= timp-2; {daca numar de linii este 100, 200, 300 .. atunci marim level'ul} end; {--------------------------------------------------------------} procedure lucru_cu_piesa(m:integer); {procedura n care mutm piesa la stnga ,la dreapta ,n jos sau o rotim} var i,j:integer; se_poate:boolean; begin se_poate:=true; if (m=1) then{mutare se stinga} begin for i:=0 to 3 do begin j:=0; while ((j<=7)and(muta_piesa_ajutor[i][j]=0)) do inc(j); if (j<=7) then {dac a existat un cub pe acea linie} if (mx+j-1<=0) then se_poate:=false else if (joc[mx+j-1][my+i]=1) then se_poate:=false; end; if (se_poate) then {dac se poate muta} begin textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); dec(mx,2); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end; end; if (m=2) then begin for i:=0 to 3 do begin j:=7; while ((j>=0)and(muta_piesa_ajutor[i][j]=0)) do dec(j); if (j>=0) then if (mx+j+1>29) then se_poate:=false else if (joc[mx+j+1][my+i]=1) then se_poate:=false; end; if (se_poate) then

28

begin textbackground(BLACK); {se terge piesa din locul vechi} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); inc(mx,2); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end end; if (m=3) then begin for i:=0 to 3 do for j:=0 to 7 do _muta_piesa_ajutor[i][j]:=0; for i:=0 to 3 do begin j:=6; while (j>=0) do begin _muta_piesa_ajutor[3-(j div 2)][i*2]:=muta_piesa_ajutor[i][j]; j:=j-2;; end; for j:=0 to 3 do _muta_piesa_ajutor[j][i*2+1]:=_muta_piesa_ajutor[j][i*2]; end; for i:=0 to 3 do for j:=0 to 7 do if (_muta_piesa_ajutor[i][j]=1) then if ((mx+j<=0)or(mx+j>30)or(my+i<=0)or(my+i>18)) then se_poate:=false else if (joc[mx+j][my+i]=1) then se_poate:=false; if (se_poate) then begin textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; for i:=0 to 3 do for j:=0 to 7 do muta_piesa_ajutor[i][j]:=_muta_piesa_ajutor[i][j]; textbackground(culoare_cuburi); for i:=0 to 3 do{si acum o desenam pe ecran} for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my);

29

write(' '); end; end ; end; if (m=4) then {coborire} begin for j:=0 to 7 do begin i:=3; while ((i>=0)and(muta_piesa_ajutor[i][j]=0)) do dec(i); if (i>=0) then if (my+i+1>18) then se_poate:=false else if (joc[mx+j][my+i+1]=1) then se_poate:=false; end; if (se_poate) then begin {tergere pies} textbackground(BLACK); for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; textbackground(culoare_cuburi); inc(my);{schimbare pies} {redesenare pies} for i:=0 to 3 do for j:=0 to 7 do if (muta_piesa_ajutor[i][j]=1) then begin gotoxy(7+j+mx,3+i+my); write(' '); end; end else begin for i:=0 to 3 do for j:=0 to 7 do begin if (muta_piesa_ajutor[i][j]=1) then joc[j+mx][i+my]:=1; end; verificare_linie; {se aduce noua pies} piesa_noua; end; end end; {--------------------------------------------------------------} procedure schimbare_nume; var citire:boolean; caractere:set of char; c:char; begin citire:=true; caractere:=['A'..'Z','a'..'z',' ','_','-',',']; textbackground(BLUE);

30

textcolor(WHITE); gotoxy(48,4); write(' '); gotoxy(48,4); nume_jucator:=''; showcursor; while (citire) do begin c:=readkey; {citim un caracter} if (c=#13) then citire:=false; if (length(nume_jucator)>=20) then gotoxy(68,4) else if (c in caractere) then begin gotoxy(48,4); nume_jucator:=nume_jucator+c; write(nume_jucator); end; if (c=#8) then if (nume_jucator<>'') then begin {este tiparit numele din nou} nume_jucator[length(nume_jucator)]:=' '; gotoxy(48,4); write(nume_jucator); delete(nume_jucator,length(nume_jucator),1); gotoxy(48,4); write(nume_jucator); end; end; hidecursor; end; {--------------------------------------------------------------} procedure top; type patretel=record c:char; cul:byte; end; type monitor=array [1..25,1..80] of patretel; var i,j:integer; ecran:monitor absolute $b800:0; sav:monitor; begin sav:=ecran; textcolor(WHITE); textbackground(BLUE); gotoxy(1,1); write(' Tetris Top 20 '); write(' Nr. Nume Scor Cuburi Linii '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' ');

31

write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(' '); write(''); textbackground(BLACK); textcolor(WHITE+BLINK); gotoxy(28,25); write('press any key to continue'); textbackground(BLUE); textcolor(WHITE); citire_top; for i:=1 to 20 do begin gotoxy(2,3+i); for j:=length(nume[i].nume) to 21 do nume[i].nume:=nume[i].nume+' '; write(i:5,'. ',nume[i].nume,nume[i].scor:8, nume[i].cuburi:15,nume[i].linii:17); end; readkey; ecran:=sav; end; {--------------------------------------------------------------} begin culoare_cuburi:=red; piesa_next:=random(7); nume_jucator:=''; randomize; mouseinit; start:=clock; endt:=start; mutare:=false; initjoc; mouseshow; c:=#0;inchidere:=false; while ((c<>#27)and(not inchidere)) do begin c:=#0; if (((endt - start) >=timp )and(timp<>0)) then begin start:=clock; endt:=start; mutare:=true; end else endt:=clock; if (mutare) then begin lucru_cu_piesa(4); mutare:=false; end; mousedata(buton,x,y); if (keypressed) then begin c:=readkey;

32

if ((c=#0)and(timp<>0)) then begin c:=readkey; if (c=#72) then lucru_cu_piesa(3); if (c=#77) then lucru_cu_piesa(2); if (c=#80) then lucru_cu_piesa(4); if (c=#75) then lucru_cu_piesa(1); c:=#0; end; if ((c='n')or(c='N')) then begin mousehide; apasat(62,10,' N','ew '); initjoc; mouseshow; end; if ((c='l')or(c='L')) then begin mousehide; apasat(62,12,' L','evel '); level; mouseshow; end; if (((c='p')or(c='P'))and(timp>0)) then begin mousehide; apasat(62,14,' P','auza '); pauza; mouseshow; end; if (((c='r')or(c='R'))and(timp=0)) then begin mousehide; apasat(62,14,' R','enire '); resume; mouseshow; end; if ((c='t')or(c='T')) then begin mousehide; apasat(62,16,' T','op '); top; mouseshow; end; if ((c='c')or(c='C')) then begin mousehide; apasat(62,18,' C','lose '); inchidere:=true; mouseshow; end; end; if (buton<>0) then begin mousehide; if ((y=10)and(x>=62)and(x<=71)) then begin apasat(62,10,' N','ew '); initjoc; end;

33

if ((y=12)and(x>=62)and(x<=71)) then begin apasat(62,12,' L','evel '); level; end; if ((y=14)and(x>=62)and(x<=71)) then begin if (timp<>0) then begin apasat(62,14,' P','auza '); pauza; delay(100); end else begin apasat(62,14,' R','enire '); resume; delay(100); end; end; if ((y=16)and(x>=62)and(x<=71)) then begin apasat(62,16,' T','op '); top; end; if ((y=18)and(x>=62)and(x<=71)) then begin apasat(62,18,' C','lose '); inchidere:=true; end; if ((y=4)and(x>=47)and(x<=71)) then begin schimbare_nume; end; mouseshow; end; end; inchidere:=true; verificare_top; end.

34

Bibliografie

1.

L. oca, A. R. Demco, C. Opincaru, A. Sindile Informatic Manual pentru clasa a X-a Editura Niculescu - Bucureti, 2000

2.

Fl. Munteanu, T. Ionescu, Gh. Musc, D. Saru, S. M. Dasclu Programarea calculatoarelor Manual pentru liceele de informatic, Editura Didactic i Pedagogic Bucureti, 1995

3.

S. Niculescu i colaboratori Bacalaureat i atestat

Editura L&S, 1998

35

CUPRINS

INTRODUCERE..2 CORPUL PROGRAMULUI3 BIBLIOGRAFIE..35

36