{$i img20.pas}
{$i clavier2.pas}
{$m 4000,0,40000}
type
position=record
         x,y:word;
end;

type
spr=record
   pos:position;
   larg,haut,
   pagex,pagey:word;
   page:byte;
   statu:boolean;
end;

type
anim=record
   count:word;
end;


var
sprite       :array [1..20] of spr;
count,
countb,
ani          :array [1..10] of anim;
targan       :position;
nbspr,lim1,
lim2         :word;
holdvecadres :pointer;
number,vites :byte;
direction    :boolean;

procedure movdani(x,y:word;numero,speed:byte);
var
contani:word;
begin
inc(ani[numero].count,speed);
if ani[numero].count>149
then ani[numero].count:=0;
contani:=ani[numero].count;
if contani>0 then if contani<50 then
 movspr(sprite[4].pagex,
          sprite[4].pagey,
          x,y,
          sprite[4].larg,
          sprite[4].haut,
          sprite[4].page,9);

if contani>49 then if contani<100 then
 movspr(sprite[5].pagex,
          sprite[5].pagey,
          x-2,y+3,
          sprite[5].larg,
          sprite[5].haut,
          sprite[5].page,9);

if contani>99 then if contani<150 then
 movspr(sprite[6].pagex,
          sprite[6].pagey,
          x-6,y+2,
          sprite[6].larg,
          sprite[6].haut,
          sprite[6].page,9);

inc(targan.x,1);
direction:=true;
end;

procedure tombdani(x,y:word;numero,speed:byte);
var
contani:word;
begin
inc(ani[numero].count,speed);
if ani[numero].count>183 then
begin
ani[numero].count:=0;
lim1:=lim2-1;
lim2:=280;
end;
contani:=ani[numero].count;
if contani>0 then if contani<50 then
 movspr(sprite[12].pagex,
          sprite[12].pagey,
          x,y,
          sprite[12].larg,
          sprite[12].haut,
          sprite[12].page,9);

if contani>49 then if contani<80 then
 movspr(sprite[13].pagex,
          sprite[13].pagey,
          x,y,
          sprite[13].larg,
          sprite[13].haut,
          sprite[13].page,9);

if contani>79 then if contani<130 then
 movspr(sprite[14].pagex,
          sprite[14].pagey,
          x,y,
          sprite[14].larg,
          sprite[14].haut,
          sprite[14].page,9);

if contani>129 then if contani<154 then
 movspr(sprite[15].pagex,
          sprite[15].pagey,
          x,y+30,
          sprite[15].larg,
          sprite[15].haut,
          sprite[15].page,9);


inc(targan.y,2);

end;
procedure movgani(x,y:word;numero,speed:byte);
var
contani:word;
begin
inc(ani[numero].count,speed);
if ani[numero].count>149 then ani[numero].count:=0;
contani:=ani[numero].count;
if contani>0 then if contani<50 then
 movspr(sprite[9].pagex,
          sprite[9].pagey,
          x,y,
          sprite[9].larg,
          sprite[9].haut,
          sprite[9].page,9);

if contani>49 then if contani<100 then
 movspr(sprite[8].pagex,
          sprite[8].pagey,
          x-3,y+2,
          sprite[8].larg,
          sprite[8].haut,
          sprite[8].page,9);

if contani>99 then if contani<150 then
 movspr(sprite[7].pagex,
          sprite[7].pagey,
          x,y+2,
          sprite[7].larg,
          sprite[7].haut,
          sprite[7].page,9);

dec(targan.x,1);
direction:=false;
end;


procedure lightani(x,y:word;numero,speed:byte);
var
contani:word;
begin
inc(ani[numero].count,speed);
if ani[numero].count>150 then ani[numero].count:=0;
contani:=ani[numero].count;
if contani>0 then if contani<50 then
 movspr(sprite[1].pagex,
          sprite[1].pagey,
          x,y+4,
          sprite[1].larg,
          sprite[1].haut,
          sprite[1].page,9);

if contani>50 then if contani<100 then
 movspr(sprite[2].pagex,
          sprite[2].pagey,
          x,y,
          sprite[2].larg,
          sprite[2].haut,
          sprite[2].page,9);

if contani>100 then if contani<150 then
 movspr(sprite[3].pagex,
          sprite[3].pagey,
          x,y+1,
          sprite[3].larg,
          sprite[3].haut,
          sprite[3].page,9);

end;

procedure premierplan1;
begin
 movspr(sprite[10].pagex,
          sprite[10].pagey,
          104,72,
          sprite[10].larg,
          sprite[10].haut,
          sprite[10].page,9);

 movspr(sprite[10].pagex,
          sprite[10].pagey,
          168,135,
          sprite[10].larg,
          sprite[10].haut,
          sprite[10].page,9);

movspr(sprite[11].pagex,
          sprite[11].pagey,
          160,73,
          sprite[11].larg,
          sprite[11].haut,
          sprite[11].page,9);

end;
procedure notmove;
begin
if direction then begin
 movspr(sprite[4].pagex,
          sprite[4].pagey,
          targan.x,targan.y,
          sprite[4].larg,
          sprite[4].haut,
          sprite[4].page,9);

end
else
movspr(sprite[9].pagex,
          sprite[9].pagey,
          targan.x,targan.y,
          sprite[9].larg,
          sprite[9].haut,
          sprite[9].page,9);


end;

procedure ecran;
begin
if statu then
  begin
   copypage(adres[9],
            $a000,
            32000);
  end;
statu:=not statu;
end;

procedure decor(num:byte);
begin
if statu then
  begin
   copypage(adres[num],
            adres[9],
            32000);
  end;
end;

procedure initvar;
begin
{decor}
 lpal('ptab1.img');
 loadimg('ptab2.img',3);
 loadimg('ptab1.img',4);
 loadimg('ptab1.img',9);


{sprites}
  nbspr:=1;
 number:=0;
loadimg('pspr.img',1);
loadimg('ptarspr.img',2);


{sprites 1}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=1;
 sprite[number].pagex:=154;
 sprite[number].pagey:=5;
 sprite[number].larg:=9;
 sprite[number].haut:=30;
 sprite[number].statu:=true;

{sprites 2}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=1;
 sprite[number].pagex:=180;
 sprite[number].pagey:=2;
 sprite[number].larg:=9;
 sprite[number].haut:=34;
 sprite[number].statu:=true;

{sprites 3}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=1;
 sprite[number].pagex:=203;
 sprite[number].pagey:=3;
 sprite[number].larg:=9;
 sprite[number].haut:=33;
 sprite[number].statu:=true;

{sprites 4}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=16;
 sprite[number].pagey:=4;
 sprite[number].larg:=22;
 sprite[number].haut:=51;
 sprite[number].statu:=true;

{sprites 5}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=60;
 sprite[number].pagey:=7;
 sprite[number].larg:=30;
 sprite[number].haut:=48;
 sprite[number].statu:=true;

{sprites 6}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=103;
 sprite[number].pagey:=6;
 sprite[number].larg:=31;
 sprite[number].haut:=49;
 sprite[number].statu:=true;

{sprites 7}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=163;
 sprite[number].pagey:=81;
 sprite[number].larg:=32;
 sprite[number].haut:=49;
 sprite[number].statu:=true;

{sprites 8}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=206;
 sprite[number].pagey:=82;
 sprite[number].larg:=33;
 sprite[number].haut:=50;
 sprite[number].statu:=true;

{sprites 9}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=260;
 sprite[number].pagey:=79;
 sprite[number].larg:=25;
 sprite[number].haut:=52;
 sprite[number].statu:=true;

{sprites 10}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=1;
 sprite[number].pagex:=155;
 sprite[number].pagey:=47;
 sprite[number].larg:=21;
 sprite[number].haut:=63;
 sprite[number].statu:=true;

{sprites 11}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=1;
 sprite[number].pagex:=196;
 sprite[number].pagey:=45;
 sprite[number].larg:=24;
 sprite[number].haut:=64;
 sprite[number].statu:=true;

{sprites 12}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=193;
 sprite[number].pagey:=11;
 sprite[number].larg:=36;
 sprite[number].haut:=48;
 sprite[number].statu:=true;

{sprites 13}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=238;
 sprite[number].pagey:=3;
 sprite[number].larg:=32;
 sprite[number].haut:=66;
 sprite[number].statu:=true;

{sprites 14}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=281;
 sprite[number].pagey:=6;
 sprite[number].larg:=31;
 sprite[number].haut:=66;
 sprite[number].statu:=true;

 {sprites 15}
 inc(number);
 sprite[number].pos.y:=0;
 sprite[number].pos.x:=0;
 sprite[number].page:=2;
 sprite[number].pagex:=147;
 sprite[number].pagey:=15;
 sprite[number].larg:=38;
 sprite[number].haut:=34;
 sprite[number].statu:=true;

 targan.x:=50;
 targan.y:=76;
 lim1:=10;
 lim2:=140;
{affichage}
 statu:=true;

{animation}
for x:=1 to 6 do
ani[x].count:=0;
end;


begin
 mode(19);
 instalnewkeyb;
 clearkbd;
 initvar;

  repeat
   decor(4);
   lightani(50,50,1,4);
   lightani(80,50,2,4);
   lightani(220,140,3,4);
   if (targan.x<lim2) then
   begin
   if (keypress(75))
   and(targan.x>lim1)
   or ((keypress(77))
   and(targan.x<lim2))
   then begin
    if keypress(77) then
    movdani(targan.x,targan.y,4,4);
    if keypress(75) then
    movgani(targan.x,targan.y,5,4);
   end
   else notmove;
   end
   else
   tombdani(targan.x,targan.y,6,4);


   premierplan1;
   ecran;
  until keypress(1);

 delallems(0);
 holdkey;
 textmode(co80);
end.
