program igdc99;

uses graph, crt;
const h=600; W=800;

      herox=400;

var grDriver, grMode:integer;
    esc:boolean;
    topborder, botborder:array [0..W] of integer;
    topchange, botchange:integer;
    lasttopborder,lastbotborder:integer;
    score:real;
    heroy:integer;
    lastheroy:integer;
    stopgame:boolean;
    kol:integer;
    velocity:integer;
    changer:integer;
    topmax,botmax:integer;
    lastkey, key:char;
{*************************************************-Initialize}
{---------------------------------------------------StartDraw}
procedure startdraw;
begin
 setcolor(15);
 line(0,0,w,0);
 line(w,0,w,h);
 line(w,h,0,h);
 line(0,h,0,0);
end;
{--------------------------------------------------Initialize}
procedure Initialize;
var i:integer;
begin
 esc:=false;
 velocity:=1;
 stopgame:=false;
 score:=0;
 kol:=0;
 changer:=72;
 topmax:=1;
 botmax:=1;
 for i:=0 to w do
 begin
  topborder[i]:=1;
  botborder[i]:=1;
 end;
 lasttopborder:=1;
 lastbotborder:=1;
 heroy:=h div 2;
 grDriver:=0;
 InitGraph(grdriver, grmode, '');
 startdraw;
end;
{*****************************************************-Update}
{----------------------------------------------------UpBorder}
procedure UpBorder;
var i,z:integer;
begin
  lasttopborder:=topborder[0];
  lastbotborder:=topborder[0];
  for i:=0 to W-1 do
  begin
   topborder[i]:=topborder[i+1];
   botborder[i]:=botborder[i+1];
  end;
  randomize;
  randseed:=random(1000);
  z:=random(changer);
  if random(2) = 0 then z:=-z;
  topborder[W]:=Topborder[W-1]+(topchange*2 div 3)+z;


  Randseed:=Random(1000);
  z:=random(changer);
  if random(2) = 0 then z:=-z;
  botborder[W]:=botborder[W-1]+(botchange div 2)+z;

  if (abs(topborder[w]-topborder[w-1])>10) then
  begin
   if topborder[w]>topborder[w-1]
    then topborder[w]:=topborder[w-1]+10
    else topborder[w]:=topborder[w-1]-10;
  end;
  if (abs(botborder[w]-botborder[w-1])>10) then
  begin
   if botborder[w]>botborder[w-1]
    then botborder[w]:=botborder[w-1]+10
    else botborder[w]:=botborder[w-1]-10;
  end;
  if (topborder[w]>h-botmax-200) then topborder[w]:=topborder[w-1]+random(2)-random(4);
  if (botborder[w]>h-topmax-200)  then botborder[w]:=botborder[w-1]+random(2)-random(4);
  if (topborder[w]<0) then TopBorder[w]:=-Topborder[w];
  if (botborder[w]<0) then botborder[w]:=-botborder[w];
  botchange:=botBorder[W]-botBorder[w-1];
  topchange:=topBorder[W]-topBorder[w-1];


end;
{-------------------------------------------------------Upkey}
procedure UpKey;
begin
 if keypressed then key:=readkey else key:=#0;

 if (key=#27) then esc:=true;
 if ((key='w') or (key='æ')) and (lastkey=#0) then heroy:=heroy-velocity;
 if ((key='w') or (key='æ')) then heroy:=heroy-velocity*2;
 if ((key='s') or (key='ë')) then heroy:=heroy+velocity;


end;
{-----------------------------------------------------UpScore}
procedure Upscore;
begin
 score:=score+0.4;
end;
{------------------------------------------------------uphero}
procedure uphero;
begin
 heroy:=heroy+velocity;
 if ((heroy<topborder[herox]) or (heroy>h-botborder[herox])) then stopgame :=true;
end;
{------------------------------------------------------uplast}
procedure Uplast;
begin
 lastheroy:=heroy;
 lastkey:=key;
end;
{------------------------------------------------------Update}
procedure Update;
var i:integer;
begin
 if not stopgame then
 begin
  uplast;
  for i:=1 to kol do
  begin
  UpBorder;

  upkey
  end;
  uphero;



  UpScore;
  if Round(score) mod 250 = 0 then
  begin
   kol:=kol+1;
   velocity:=velocity+1;

  end;
  if Round(score) mod 10 =0 then
  begin
   topmax:=topborder[w-velocity*(kol+1)];
   botmax:=botborder[w-velocity*(kol+1)];
   for i:=w-velocity*kol*2 to w do
   begin
    if topmax<topborder[i] then topmax:=topborder[i];
    if botmax<botborder[i] then botmax:=botborder[i];
   end;
  end;
 end;
 upkey;
end;

{*******************************************************-Draw}
{-------------------------------------------------clearscreen}
procedure clearscreen;
var i:integer;
begin
 {setcolor(0);
 for i:=0 to w-2 do
 begin
  if (topborder[i]<>0) then line(i+1,0,i+1,topborder[i]);
  if (botborder[i]<>0) then line(i+1,0,i+1,h-botborder[i]);
 end;}
 if (round(score) mod 250 <> 0) then
 begin
  for i:=0 to w-(kol+1) do
  begin
   if (topborder[i]<>0) then
   putpixel(i+kol,topborder[i],0);
    if (botborder[i]<>0) then
   putpixel(i+kol,h-botborder[i],0);
  end;
  bar(10,10,200,40);
  putpixel(herox,lastheroy,0);
 end else
 begin
  setcolor(0);
  bar(1,1,w-1,h-1);
 end;

end;
{----------------------------------------------------DrawHero}
procedure DrawHero;
begin
 putpixel(herox,heroy,12);
end;
{--------------------------------------------------DrawBorder}
procedure DrawBorder;
var i:integer;
begin
 {setcolor(15);
 for i:=0 to w do
 begin
  line(i,0,i,topborder[i]);
  line(i,h,i,h-botborder[i]);
 end;}
 for i:=w downto 0 do
 begin
  putpixel(i,topborder[i],15);
  putpixel(i,h-botborder[i],15);
 end;
end;
{---------------------------------------------------DrawScore}
procedure DrawScore;
var s:string;
begin
 setColor(14);
 str(Round(score),s);
 outtextXy(10,10,s);

end;
{--------------------------------------------------------Draw}
procedure Draw;
var s:string;
begin
 clearscreen;
 DrawHero;
 DrawBorder;
 DrawScore;
 str(Round(score),s);
 s:='Your score is: '+s;
 if stopgame then
 begin
  setcolor(12);
  outtextxy(w div 2, h div 2, s);
 end;
end;
{*******************************************************-Main}
BEGIN
initialize;
While not esc do
begin
 Update;

 Draw;
 Delay(12);
end;


closegraph;

END.

