program HaHaHa;
uses graph,crt;
type hrac=record
      meno:string[15];
      level,score:string;
          end;
var gd,gm,w,code,res,score,level,gulicky:integer;
    k,pom4,pom5:integer;
    tlac,klav:char;
    meno,pom3:string;
    qt,st,zmena:boolean;
    tab:array[1..10] of hrac;
    subor:text;
procedure efekt1(txt:string;col:integer;pocet:integer;y:integer);
var x1,y1,i:integer;
    key:string;
begin
settextstyle(1,0,1);
setcolor(col);
i:=0;
while not(keypressed) and (i<pocet) do begin
                          i:=i+1;
                          cleardevice;
                          setusercharsize(i,5,i,5);
                          outtextxy(40,y,txt);
                          delay(30-(i div 2));
                                         end;
settextstyle(1,0,3);
end;
procedure efekt2(txt:string;col:integer;pocet:integer);
var i:integer;
    key:string;
begin
settextstyle(1,0,1);
setcolor(col);
i:=0;
while not(keypressed) and (i<pocet) do begin
             i:=i+1;
             cleardevice;
             setusercharsize(i,5,i,5);
             outtextxy((getmaxx div 2)-(textwidth(txt) div 2),(getmaxy div 2)-(textheight(txt) div 2),txt);
             sound(60+10*i);
             delay(9);
             nosound;
             delay(40);
                  end;
key:=readkey;
settextstyle(1,0,3);
end;
procedure tabulka;
var i:integer;
    key:char;
begin
cleardevice;
rectangle(10,10,getmaxx-10,getmaxy-10);
rectangle(15,15,getmaxx-15,getmaxy-15);
setcolor(green);
outtextxy(44,15,'Meno');
outtextxy(340,15,'Level');
outtextxy(440,15,'Score');
for i:=1 to 10 do begin
                outtextxy(40,40*i+10,tab[i].meno);
                outtextxy(350,40*i+10,tab[i].level);
                outtextxy(450,40*i+10,tab[i].score);
                  end;
key:=readkey;
cleardevice;
end;
function HRA1(lev:integer):integer;
type tehla=record
          x,y,typ,special:integer;
          kde:pointer;
           end;
var i,r,x1,y1,x2,y2,dx,dy,dx2,e1,e2:integer;
    q1,q2,maxxx,maxyy,minyy,minxx,j,velkost:integer;
    riadky,stlpce,medz,score2,gulicky2:integer;
    size,size2,size3:word;
    po,p,podklad:pointer;
    tehly:array[1..15,1..10] of tehla;
    key,key2,key3:char;
    v,fi,x,y,xv,yv:real;
    start,koniec,glue:boolean;
procedure beep;
 begin
 sound(420);
 delay(10);
 nosound;
 end;
procedure ukaztehla(var t:tehla);
  var velkost,farba:integer;
  begin
  case t.typ of
   1:farba:=6;
   2:farba:=8;
   3:farba:=2;
   4:farba:=14;
   5:farba:=4;
   6:farba:=0;
  end;
  setfillstyle(1,farba);
  velkost:=imagesize(0,0,30,10);
  getmem(t.kde,velkost);
  getimage(t.x,t.y,t.x+30,t.y+10,t.kde^);
  bar(t.x,t.y,t.x+30,t.y+10);
  end;
procedure odstrantehlu(var t:tehla);
  var i,j,q1,q2,suc:integer;
  begin
  suc:=10*(level div 3);
  i:=((t.x-minxx-3*medz) div (30+medz))+1;
  j:=((t.y-minyy-30) div 15)+1;
  if t.typ=1 then begin
                t.typ:=0;
                score2:=score2+10+suc;
                putimage(t.x,t.y,t.kde^,NormalPut);
                   end;
  if t.typ=2 then if t.special=1 then begin
                                  t.typ:=0;
                                  t.special:=0;
                                  score2:=score2+10+suc;
                                  putimage(t.x,t.y,t.kde^,NormalPut);
                                      end
                                 else begin
                                  t.special:=t.special-1;
                                  score2:=score2+10+suc;
                                      end;
  if t.typ=3 then begin
               score2:=score2+20+suc;
               odstrantehlu(tehly[i-1,j-1]);
               odstrantehlu(tehly[i+1,j-1]);
               odstrantehlu(tehly[i-1,j+1]);
               odstrantehlu(tehly[i+1,j+1]);
               odstrantehlu(tehly[i,j-1]);
               odstrantehlu(tehly[i+1,j]);
               odstrantehlu(tehly[i,j+1]);
               odstrantehlu(tehly[i-1,j]);
               t.typ:=0;
               putimage(t.x,t.y,t.kde^,NormalPut);
                  end;
  if t.typ=4 then begin
               glue:=true;
               t.typ:=0;
               score2:=score2+30+suc;
               putimage(t.x,t.y,t.kde^,NormalPut);
                  end;
  if t.typ=5 then begin
               t.typ:=0;
               gulicky2:=gulicky2+1;
               gulicky2:=gulicky2 mod 25;
               score2:=score2+30+suc;
               putimage(t.x,t.y,t.kde^,NormalPut);
                  end;
  if t.typ=6 then begin
               koniec:=true;
               for q1:=1 to stlpce do for q2:=1 to riadky do
                 if (q1<>i) and (q2<>j) then koniec:=(koniec and (tehly[q1,q2].typ=0));
               if koniec=true then begin
                 t.typ:=0;
                 score2:=score2+50+suc;
                 putimage(t.x,t.y,t.kde^,NormalPut);
                                   end;
                  end;

  end;
procedure vypisskore;
  var str1:string;
  begin
  setviewport(maxxx+20,10,getmaxx-1,80,true);
  clearviewport;
  setcolor(yellow);
  str(score,str1);
  outtextxy(2,2,'SCORE:');outtextxy(1,30,str1);
  setviewport(0,0,getmaxx,getmaxy,true);
  end;
procedure vypislevel;
  var str1:string;
begin
  setviewport(maxxx+20,90,getmaxx-1,160,true);
  clearviewport;
  setcolor(yellow);
  str(lev,str1);
  outtextxy(2,2,'LEVEL:');outtextxy(20,30,str1);
  setviewport(0,0,getmaxx,getmaxy,true);
  end;
procedure vypisgulicky;
  var j,i:integer;
begin
  setviewport(maxxx+14,170,getmaxx-1,280,true);
  clearviewport;
  setcolor(red);
  outtextxy(10,2,'BALLS:');
  setfillstyle(1,red);
  for j:=0 to (gulicky div 5)-1 do
  for i:=1 to 5 do fillellipse(3*r*i,3*r*j+30+r+2,r,r);
  for i:=1 to (gulicky mod 5) do fillellipse(3*r*i,3*r*((gulicky div 5))+30+r+2,r,r);
  setviewport(0,0,getmaxx,getmaxy,true);
  end;

begin
riadky:=6;stlpce:=10;
minxx:=10;
minyy:=10;
maxxx:=530;
maxyy:=getmaxy-10;
setfillstyle(1,blue);
bar(minxx,minyy,maxxx,maxyy);
setcolor(15);
rectangle(minxx-10,minyy-10,maxxx+10,maxyy+10);rectangle(minxx,minyy,maxxx,maxyy);
rectangle(maxxx+12,0,getmaxx,getmaxy);
setfillstyle(1,brown);
floodfill(minxx-2,minyy-2,15);
setviewport(maxxx+14,280,getmaxx-1,getmaxy-1,true);
settextstyle(2,0,4);
setfillstyle(1,6);setcolor(6);
bar(5,5,35,15);outtextxy(37,5,'Normal');
setfillstyle(1,8);setcolor(8);
bar(5,25,35,35);outtextxy(37,25,'3xNormal');
setfillstyle(1,2);setcolor(2);
bar(5,45,35,55);outtextxy(37,45,'Bomb');
setfillstyle(1,14);setcolor(14);
bar(5,65,35,75);outtextxy(37,65,'Glue');
setfillstyle(1,4);setcolor(4);
bar(5,85,35,95);outtextxy(37,85,'Extraball');
setfillstyle(1,0);setcolor(15);
rectangle(5,105,35,115);outtextxy(37,105,'End');
outtextxy(15,130,'z - left');
outtextxy(15,139,'x - right');
outtextxy(15,148,'p - fire');
outtextxy(15,157,'enter - stop');
setcolor(15);outtextxy(1,185,'Vyrobil:SAM-1999');
settextstyle(1,0,3);
vypislevel;
gulicky2:=gulicky;
start:=true;
for i:=1 to stlpce do for j:=1 to riadky do begin
                        medz:=round((maxxx-30*stlpce)/(stlpce+5));
                        tehly[i,j].typ:=1;
                        tehly[i,j].x:=minxx+3*medz+(30+medz)*(i-1);
                        tehly[i,j].y:=minyy+30+15*(j-1);
                        tehly[i,j].special:=0;
                                  end;
for i:=1 to 10 do begin
     q1:=1+random(stlpce);
     q2:=1+random(riadky);
     tehly[q1,q2].typ:=2;
     tehly[q1,q2].special:=3;
                  end;
for i:=1 to 3 do tehly[1+random(stlpce),1+random(riadky)].typ:=3;
for i:=1 to 6 do tehly[1+random(stlpce),1+random(riadky)].typ:=4;
if random(2)=1 then tehly[1+random(stlpce),1+random(riadky)].typ:=5;
tehly[(stlpce div 2)+random(2),(riadky div 2)+random(2)].typ:=6;
for i:=1 to stlpce do for j:=1 to riadky do ukaztehla(tehly[i,j]);
r:=5;
randomize;
e1:=40;
e2:=3;
dx:=maxxx div 2;
dy:=maxyy-1-e2;
dx2:=dx;
x:=dx;
y:=dy-e2-r-1;
randomize;
fi:=-pi/2-pi*random/4;
xv:=2*cos(fi);
randomize;
yv:=2*sin(fi);
size:=imagesize(0,0,2*e1,2*e2);
getmem(p,size);
size2:=imagesize(0,0,2*r,2*r);
getmem(po,size2);
x1:=round(x);
y1:=round(y);
setcolor(15);
getimage(x1-r,y1-r,x1+r,y1+r,po^);
getimage(dx-e1,dy-e2,dx+e1,dy+e2,p^);
key:=' ';
score2:=score;
repeat
vypisskore;
vypisgulicky;
repeat
  if score2<>score then begin
                    score:=score2;
                    vypisskore;
                       end;
  if gulicky2<>gulicky then begin
                    gulicky:=gulicky2;
                    vypisgulicky;
                            end;
 if keypressed then key:=readkey;
 if (key='p') then begin
               v:=1+(lev/14);
               start:=false;
                 end;
 if start=true then begin
                   x:=dx;
                   y:=dy-e2-r-1;
                    end;
   if (round(x+xv)+r>maxxx) or (round(x+xv)-r<minxx) then begin
                                                      fi:=3*pi-fi;
                                                      beep;
                                                          end;
   if (round(y+yv)+r>maxyy) or (round(y+yv)-r<minyy) then begin
                                                      fi:=2*pi-fi;
                                                      beep;
                                                          end;
   xv:=0.5*xv;yv:=0.5*yv;
   if (x+xv<=dx+e1+r) and (x+xv>=dx-e1-r) and (y+yv>=dy-e2-r) then begin
                                                              fi:=2*pi-fi;
                                                              beep;
                                                              if glue=true then begin
                                                                                glue:=false;
                                                                                start:=true;
                                                                                end;
                                                              if key='z' then fi:=fi+0.5;
                                                              if key='x' then fi:=fi-0.5;
                                                                end;
   if (y+yv>dy-e2-1) and (x+xv<dx+e1+r+1) and (x+xv>dx-e1-r-1) then fi:=3*pi-fi;
   xv:=2.8*xv;yv:=2.8*yv;
   for q1:=1 to stlpce do for q2:=1 to riadky do if tehly[q1,q2].typ<>0 then begin
    if (y+yv>tehly[q1,q2].y) and (y+yv<tehly[q1,q2].y+10) then begin
            if (x+xv>tehly[q1,q2].x-r-3) and (x+xv<tehly[q1,q2].x+30+r+3) then begin
                                                  odstrantehlu(tehly[q1,q2]);
                                                  fi:=3*pi-fi;
                                                  beep;
                                                                end;
           { if (x+xv>tehly[q1,q2].x+30) and (x+xv<tehly[q1,q2].x+30+r) then begin
                                                  odstrantehlu(tehly[q1,q2]);
                                                  fi:=3*pi-fi;
                                                                end;}
                                                         end;
    if (x+xv>tehly[q1,q2].x-r) and (x+xv<tehly[q1,q2].x+30+r) then begin
            if (y+yv>tehly[q1,q2].y-r-1) and (y+yv<tehly[q1,q2].y) then begin
                                                  odstrantehlu(tehly[q1,q2]);
                                                  fi:=2*pi-fi;
                                                  beep;
                                                                end;
            if (y+yv>tehly[q1,q2].y+10) and (y+yv<tehly[q1,q2].y+10+r+1) then begin
                                                  odstrantehlu(tehly[q1,q2]);
                                                  fi:=2*pi-fi;
                                                  beep;
                                                                end;
                                                         end;
                                                                    end;
   xv:=v*cos(fi);
   yv:=v*sin(fi);
   x:=x+xv;
   y:=y+yv;
   x2:=round(x);
   y2:=round(y);
   setcolor(12);
   putimage(x1-r,y1-r,po^,0);
   getimage(x2-r,y2-r,x2+r,y2+r,po^);
   circle(x2,y2,r);
   x1:=x2;
   y1:=y2;
   delay(1);
   if (key='z') and (dx-e1>minxx) then dx2:=dx-1;
   if dx-e1<minxx then key:=' ';
   if (key='x') and (dx+e1<maxxx) then dx2:=dx+1;
   if dx+e1>maxxx then key:=' ';
   putimage(dx-e1,dy-e2,p^,0);
   getimage(dx2-e1,dy-e2,dx2+e1,dy+e2,p^);
   setcolor(3);
   for j:=dy-e2 to dy+e2 do line(dx2-e1,j,dx2+e1,j);
   dx:=dx2;
until (key=#27) or (y+yv>maxyy-r-e2) or (koniec=true);
if (key=#27) then begin
                   size3:=imagesize(140,200,400,250);
                   getmem(podklad,size3);
                   getimage(140,200,400,250,podklad^);
                   setfillstyle(1,0);
                   bar(140,200,400,250);
                   setcolor(white);
                   rectangle(140,200,400,250);
                   outtextxy(148,207,'Chces skoncit ? (a/n)');
                   repeat
                    key:=readkey;
                   until (key='a') or (key='n');
                   putimage(140,200,podklad^,normalput);
                   freemem(podklad,size3);
                   HRA1:=2;
                  end;
if (y+yv>maxyy-r-2) then begin
                   gulicky2:=gulicky2-1;
                   v:=0;
                   x:=dx;
                   y:=dy-e2-r-1;
                   start:=true;
                   putimage(x1-r,y1-r,po^,normalput);
                   size3:=imagesize(140,200,400,250);
                   getmem(podklad,size3);
                   getimage(140,200,400,250,podklad^);
                   setfillstyle(1,0);
                   bar(140,200,400,250);
                   setcolor(white);
                   rectangle(140,200,400,250);
                   outtextxy(148,207,' Gulicka je von!!!');
                   key3:=readkey;
                   putimage(140,200,podklad^,normalput);
                   freemem(podklad,size3);
                   key:=' ';
                   fi:=-pi/2-pi*random/4;
                         end;
until (key='a') or (key='A') or (gulicky=0) or (koniec=true);
freemem(p,size);
freemem(po,size2);
velkost:=imagesize(0,0,30,10);
setcolor(15);
if (gulicky=1) then begin
                   size3:=imagesize(140,200,400,250);
                   getmem(podklad,size3);
                   getimage(140,200,400,250,podklad^);
                   setfillstyle(1,0);
                   bar(140,200,400,250);
                   setcolor(white);
                   rectangle(140,200,400,250);
                   outtextxy(148,207,'    THE END!!!');
                   key:=readkey;
                   putimage(140,200,podklad^,normalput);
                   freemem(podklad,size3);
                   HRA1:=1;
                          end;
if koniec=true then begin
                   size3:=imagesize(140,200,400,250);
                   getmem(podklad,size3);
                   getimage(140,200,400,250,podklad^);
                   setfillstyle(1,0);
                   bar(140,200,400,250);
                   setcolor(white);
                   rectangle(140,200,400,250);
                   outtextxy(148,207,'    Perfektne!!!');
                   key:=readkey;
                   putimage(140,200,podklad^,normalput);
                   freemem(podklad,size3);
                   HRA1:=0;
                   koniec:=false;
                    end;
while keypressed do
key:=readkey;
end;{koniec funkcie HRA1}
begin
clrscr;
randomize;
gd:=detect;
initgraph(gd,gm,'c:\bp\bgi\');
assign(subor,'c:\bp\tab.sam');
reset(subor);
for w:=1 to 10 do begin
               readln(subor,tab[w].meno);
               readln(subor,tab[w].level);
               readln(subor,tab[w].score);
                  end;
close(subor);
settextstyle(1,0,3);
res:=0;
level:=0;
score:=0;
gulicky:=4;
klav:='x';
zmena:=true;qt:=false;st:=false;
efekt1('SAM',yellow,47,100);
if not(keypressed) then delay(500);
efekt1('uvadza',lightblue,26,120);
if not(keypressed) then delay(500);
efekt1('multimedialny',green,12,100);
if not(keypressed) then delay(500);
efekt1('cool',white,45,80);
if not(keypressed) then delay(500);
efekt1('narez',magenta,32,100);
if not(keypressed) then delay(500);
efekt2('MRAZIK',white,29);
setviewport(0,0,getmaxx,getmaxy,true);
cleardevice;
repeat
repeat
gulicky:=gulicky mod 5;
level:=level mod 10;
if zmena then begin
 cleardevice;
 settextstyle(4,0,9);
 setcolor(15);
 outtextxy(115,20,'MRAZIK');
 settextstyle(2,0,6);
 setcolor(15);outtextxy(460,455,'Vyrobil:SAM-1999');
 setcolor(blue);
 settextstyle(1,0,3);
 setviewport(200,150,440,360,true);
 clearviewport;
 rectangle(0,0,240,210);
 rectangle(3,3,237,207);
 outtextxy(30,10,'  1.Start game');
 str(level+1,meno);
 outtextxy(30,50,'  2.Level: '+meno);
 str(gulicky+1,meno);
 outtextxy(30,90,'  3.Balls: '+meno);
 outtextxy(30,130,'  4.Hall of fame');
 outtextxy(30,170,'  5.Quit');
 setviewport(0,0,getmaxx,getmaxy,true);
           end;
if keypressed then klav:=readkey
      else klav:=' ';
zmena:=true;
case klav of
 '1':st:=true;
 '2':level:=level+1;
 '3':gulicky:=gulicky+1;
 '4':tabulka;
 '5':qt:=true;
 else zmena:=false;
 end;
until st or qt;
level:=level+1;
gulicky:=gulicky+1;
setviewport(0,0,getmaxx,getmaxy,true);
res:=0;w:=10;
if st then begin
         score:=0;
         w:=0;
         while (res=0) do begin
               cleardevice;
               res:=HRA1(level);
               if res=0 then level:=level+1;
                  end;
           end;
pom4:=0;
while (w<10) or (score>pom4) do begin
               w:=w+1;
               val(tab[w].score,pom4,code);
               if score>=pom4 then begin
                                   for k:=9 downto w do begin
                                                tab[k+1].meno:=tab[k].meno;
                                                tab[k+1].level:=tab[k].level;
                                                tab[k+1].score:=tab[k].score;
                                                       end;
                                   cleardevice;
                                   rectangle(0,0,getmaxx,getmaxy);
                                   rectangle(5,5,getmaxx-5,getmaxy-5);
                                   setcolor(14);
                                   outtextxy(130,20,'  Dostal si sa do siene slavy');
                                   outtextxy(140,100,'Zadaj meno:');
                                   meno:='';tlac:=' ';pom5:=0;
                                   while (tlac<>#13) and (pom5<10*textwidth('m')) do begin
                                                   pom5:=pom5+textwidth(tlac)+2;
                                                   tlac:=readkey;
                                                   if tlac<>#13 then meno:=meno+tlac;
                                                   outtextxy(300+pom5,100,tlac);
                                                       end;
                                   while keypressed do tlac:=readkey;
                                   meno:=meno;
                                   tab[w].meno:=meno;
                                   str(level,meno);
                                   tab[w].level:=meno;
                                   str(score,meno);
                                   tab[w].score:=meno;
                                   score:=-1;
                                   rewrite(subor);
                                   for k:=1 to 10 do begin
                                              writeln(subor,tab[k].meno);
                                              writeln(subor,tab[k].level);
                                              writeln(subor,tab[k].score);
                                                     end;
                                   close(subor);
                                   end;
               gulicky:=4;
               end;
if st then level:=0;
if qt<>true then begin
               tabulka;
               st:=false;
                 end;
until qt;
while keypressed do meno:=readkey;
end.
