program matrix_code_emulator;
uses crt;
type pole=array[1..80,1..25,1..2] of byte;
var x,y,p:byte;
    matica:pole;
    znak,i:char;
    cas:integer;
    h:word;
procedure naplnm;
          var I,M:shortint;
          begin
          for I:=1 to 80 do begin
              for M:=1 to 25 do begin
          matica[I,M,2]:=16;
                            end;end;
          end;

procedure napln(x,y,farba:byte;i:integer);
          begin

          if i>0 then begin;
          p:=trunc(random*236)+20;
          matica[x,y,1]:=p;
                       end;
          if i=0 then begin
                      matica[x,y,1]:=32;
                      end;
          matica[x,y,2]:=farba;
          {matica[x,y,3]:=1;}
          end;
procedure pis;
          var I,M:shortint;
          label hop;
          begin
          for I:=1 to 80 do begin
              for M:=1 to 25 do begin
            {if (matica[I,M,3])=1 then begin
                matica[I,M,3]:=0;
             }   znak:=chr(matica[I,M,1]);
                if I=80 then if M=25 then begin delay(cas);goto hop; end;
                gotoxy(I,M);
                TextColor(matica[I,M,2]);
                write(znak);
                hop:;
                          {  end;}end;end;
          end;

procedure zapis;
          var i:byte;
          M,N:shortint;
           W,G,G2,B,B2:integer;
          label skok,skok2;
         begin
         W:=15; G:=10; G2:=2; B:=8; B2:=16;

          for N:=1 to 80 do begin
          i:=trunc(random*100);
          case i of
             0..2:goto skok2;
             end;
          for M:=1 to 25 do begin
              if matica[N,M,2]=B2 then  begin
                                           i:=trunc(random*100);
                                           case i of
                                           0..35:napln(N,M,W,1);
                                            end;
                                            goto skok2;
                                           end;
                if matica[N,M,2]=W then  begin
                                           i:=trunc(random*100);
                                           case i of
                                           0..80:napln(N,M,G,-1);
                                            end;
                                           goto skok;
                                           end;
                if matica[N,M,2]=G then  begin
                                           i:=trunc(random*100);
                                           case i of
                                           0..20:napln(N,M,G2,-1);
                                            end;
                                            goto skok;
                                           end;
                if matica[N,M,2]=G2 then  begin
                                           i:=trunc(random*100);
                                           case i of
                                           0..6:napln(N,M,B,-1);
                                            end;
                                            goto skok;
                                           end;
                if matica[N,M,2]=B then  begin
                                           i:=trunc(random*100);
                                           case i of
                                           0..50:napln(N,M,B2,0);
                                            end;
                                            goto skok;
                                           end;
                             skok:;
                             end;
                             skok2:;
                             end;
         end;

procedure odpis;
          var i:integer;
          M,N:shortint;

         begin
          for N:=1 to 80 do begin
                          i:=trunc(random*100);
                         case i of
                         0..5: for M:=1 to trunc(random*26) do begin
                               napln(N,M,16,0)
                               end;
                          end;
                         end;
         end;
begin
randomize;
clrscr;
cas:=10;
h:=5;
TextBackground(0);
TextColor(10);
{}
naplnm;
repeat
zapis;
odpis;
pis;
until keypressed;
end.
