program rpg;

uses crush13h,crt;

var image:array [1..13] of pointer;
    a,b,x,y:integer;
    flag:boolean;
    c:char;

procedure initstuff;
begin
     video_mode ( $13);
     initvirt;
end;

procedure closestuff;
begin
     closevirt;
     video_mode ( 03);
     for a:=1 to 13 do
         begin
              killimage(image[a]);
         end;
end;

procedure loadstuff;
begin
     loadpcx('jogo_b.pcx',vp[1]);
     getimage(0,41,20,61,image[1],vp[1]);
     getimage(20,41,40,61,image[2],vp[1]);
     getimage(40,41,60,61,image[3],vp[1]);
     getimage(0,61,20,81,image[4],vp[1]);
     getimage(20,61,40,81,image[5],vp[1]);
     getimage(40,61,60,81,image[6],vp[1]);
     getimage(0,81,20,101,image[7],vp[1]);
     getimage(20,81,40,101,image[8],vp[1]);
     getimage(39,81,59,101,image[9],vp[1]);
     getimage(0,101,20,121,image[10],vp[1]);
     getimage(20,101,40,121,image[11],vp[1]);
     getimage(39,101,59,121,image[12],vp[1]);
     getimage(59,20,78,40,image[13],vp[1]);
end;

procedure movedown;
begin
     copypage (vp[2],vp[1],0);
     if a=1 then b:=1;
     if a=2 then b:=2;
     if a=3 then b:=3;
     if a=4 then b:=2;
     a:=a+1;
     if a>4 then a:=1;
     y:=y +3;
     if y >180 then y:=180;
     putimage(x,y,255,image[b+6],vp[1]);
     copypage (vp[1],vga,0);
end;

procedure moveup;
begin
     copypage (vp[2],vp[1],0);
     if a=1 then b:=1;
     if a=2 then b:=2;
     if a=3 then b:=3;
     if a=4 then b:=2;
     a:=a+1;
     if a>4 then a:=1;
     y:=y-3;
     if y < 0 then y:=0;
     putimage(x,y,255,image[b+9],vp[1]);
     copypage (vp[1],vga,0);
end;

procedure moveleft;
begin
     copypage (vp[2],vp[1],0);
     if a=1 then b:=1;
     if a=2 then b:=2;
     if a=3 then b:=3;
     if a=4 then b:=2;
     a:=a+1;
     if a>4 then a:=1;
     x:=x-3;
     if x < 0 then x:=0;
     putimage(x,y,255,image[b+3],vp[1]);
     copypage (vp[1],vga,0);
end;

procedure moveright;
begin
     copypage(vp[2],vp[1],0);
     if a=1 then b:=1;
     if a=2 then b:=2;
     if a=3 then b:=3;
     if a=4 then b:=2;
     a:=a+1;
     if a>4 then a:=1;
     x:=x+3;
     if x > 299 then x:=299;
     putimage(x,y,255,image[b],vp[1]);
     copypage (vp[1],vga,0);
end;

procedure layout;
var a,b:integer;
begin
     cls (0,vp[2]);
     for a:=0 to 5 do
         begin
              for b:=0 to 5 do
                  begin
                       putimage(a*20,b*20,0,image[13],vp[2]);
                  end;
         end;
     copypage (vp[2],vga,0)
end;

begin
     initstuff;
     loadstuff;
     layout;
     flag:=true;
     a:=1;
     x:=0;
     y:=0;
     repeat
           begin
                c:=readkey;
                if c ='l' then moveright;
                if c ='k' then moveleft;
                if c ='w' then moveup;
                if c ='s' then movedown;
                if c =' ' then flag:=false;
           end;
     until flag=false;
     closestuff;
end.