use Tk;
use strict;

my $SLOUPCU=7;
my $RADKU=7;
my $TLOUSTKA=50;
my $KROK=0.1;
my $STARTX=2;
my $STARTY=2;
my $NEPRITELX=4;
my $NEPRITELY=5;
my $SP=2;
my $CIL_HRY=($SLOUPCU-2)*($RADKU-2);

my $smer="";
my $x = $STARTX;
my $y = $STARTY;
my $nepritelx=$NEPRITELX;
my $nepritely=$NEPRITELY;
my $refresh=60;
my $sezrano=0;
my @sezrano=();
my($t1,$t2,$l,$canvas,$nepritel,$cdman,$timer);
my @m;



my $m = MainWindow->new();
$m->title("CDman");

&vykresli_obrazovku();
&aktivuj_klavesy();

sub vykresli_obrazovku {
  $canvas = $m->Canvas(-width=>$SLOUPCU*$TLOUSTKA, -height=>$RADKU*$TLOUSTKA, -relief=>"ridge", -background=>"green", -border=>0)->pack();
  $t1 = $m->Button(-text => "Start", -command => \&start)->pack(-side=>"left");
  $l = $m->Label(-text => "Hotovo: 1/$CIL_HRY")->pack(-side=>"left");
  $t2 = $m->Button(-text => "Konec", -command => sub{exit})->pack(-side=>"right");

  #mrizka
  foreach my $i (0..$RADKU){
        $canvas->createLine(0, $i*$TLOUSTKA, $SLOUPCU*$TLOUSTKA, $i*$TLOUSTKA,-fill => "lightgreen");
  }
  foreach my $i (0..$SLOUPCU){
        $canvas->createLine($i*$TLOUSTKA, 0, $i*$TLOUSTKA, $RADKU*$TLOUSTKA, -fill => "lightgreen");
  }

  #mince
  for(my $i=2;$i<=$SLOUPCU-1;$i++){
    for(my $j=2;$j<=$RADKU-1;$j++){
      $m[$i][$j]=$canvas->createArc(($i-0.6)*$TLOUSTKA, ($j-0.6)*$TLOUSTKA, ($i-0.4)*$TLOUSTKA, ($j-0.4)*$TLOUSTKA, -fill=>"red", -outline=>"red");
    }
  }

  #cdman
  $cdman=$canvas->createArc($TLOUSTKA*($x-1)+$SP, $TLOUSTKA*($y-1)+$SP, $x*$TLOUSTKA-$SP, $y*$TLOUSTKA-$SP, -start=>30, -extent=>300, -fill=>"yellow");

  #nepritel
  my $obrazek = $m->Photo(-file=>"Ghost.gif");
  $nepritel=$canvas->createImage($TLOUSTKA*($nepritelx-.5)+$SP, $TLOUSTKA*($nepritely-.5)+$SP, -image=>$obrazek);

  #okoli
  $canvas->createRectangle($TLOUSTKA/2, $TLOUSTKA/2, ($SLOUPCU-.5)*$TLOUSTKA, $TLOUSTKA*($RADKU-.5), -outline=>"darkgreen", -width=>$TLOUSTKA);
}

sub aktivuj_klavesy{
  $m->bind("<Left>", \&doleva);
  $m->bind("<Right>", \&doprava);
  $m->bind("<Up>", \&nahoru);
  $m->bind("<Down>", \&dolu);
  $m->bind("<p>", \&pauza);
  $m->bind("<s>", \&start);
}

sub doprava{
  $canvas->itemconfigure($cdman, -start=>30, -extent=>300);
  $smer="p";
}

sub doleva{
  $canvas->itemconfigure($cdman, -start=>210, -extent=>300);
  $smer="l";
}

sub nahoru{
  $canvas->itemconfigure($cdman, -start=>120, -extent=>300);
  $smer="n";
}

sub dolu{
  $canvas->itemconfigure($cdman, -start=>300, -extent=>300);
  $smer="d";
}

sub start {
  $t1->configure(-text=>"Pauza", -command=>\&pauza);
  $timer=$canvas->repeat($refresh, \&refresh);
}

sub pauza {
  $t1->configure(-text=>"Start", -command=>\&start);
  $timer->cancel;
}

sub refresh{
  for(my $i=2;$i<=$SLOUPCU-1;$i++){
    for(my $j=2;$j<=$RADKU-1;$j++){
       if(sqrt(($i-$x)**2+($j-$y)**2)<1/3 and not (grep $_ eq "$i-$j", @sezrano)){
        push(@sezrano, "$i-$j");
        $canvas->delete($m[$i][$j]);
        $sezrano++;
        $l->configure(-text => "Hotovo: $sezrano/$CIL_HRY");
        &uspech if($sezrano==$CIL_HRY);
      }
    }
  }
  if(sqrt(($x-$nepritelx-.5)**2+($y-$nepritely-.5)**2)<1/2){
    &neuspech;
  }

  if($smer eq "p" and $x+$KROK<$SLOUPCU-1){
      $x+=$KROK;
      $canvas->move($cdman, $KROK*$TLOUSTKA, 0);
  }elsif($smer eq "l" and $x>2){
      $x-=$KROK;
      $canvas->move($cdman, -$KROK*$TLOUSTKA, 0);
  }elsif($smer eq "n" and $y>2){
      $y-=$KROK;
      $canvas->move($cdman, 0, -$KROK*$TLOUSTKA);
  }elsif($smer eq "d" and $y+$KROK<$RADKU-1){
      $y+=$KROK;
      $canvas->move($cdman, 0, $KROK*$TLOUSTKA);
  }

  if(int(rand(2))==1){
    if($x>$nepritelx+.5){
      $nepritelx+=$KROK;
      $canvas->move($nepritel, $KROK*$TLOUSTKA*0.7, 0);
    }else{
      $nepritelx-=$KROK;
      $canvas->move($nepritel, -$KROK*$TLOUSTKA*0.7, 0);
    }
  }else{
    if($y>$nepritely+.5){
      $nepritely+=$KROK;
      $canvas->move($nepritel, 0, $KROK*$TLOUSTKA*0.7);
    }else{
      $nepritely-=$KROK;
      $canvas->move($nepritel, 0, -$KROK*$TLOUSTKA*0.7);
    }
  }

}

sub uspech {
  $timer->cancel;
  $t1->configure(-state=>"disabled");
  $m->messageBox(-message => "Hra dokoncena!", -type => "ok");
}

sub neuspech {
  $timer->cancel;
  $t1->configure(-state=>"disabled");
  $m->messageBox(-message => "Byl jsi sezran!", -type => "ok");
  $canvas->itemconfigure($cdman, -fill=>"black");
}

MainLoop();
