#!/usr/bin/perl use strict; use Tk; use Tk::Photo; use MIME::Base64; use Tk::PNG; use Imager; my $xsize = 200; my $ysize = 200; my $cicle; my ($x, $y) = (int($xsize/2), int($ysize/2)); my $image = Imager->new(xsize=>$xsize, ysize=>$ysize); my $blue = Imager::Color->new( 255, 255, 255 ); my $image_data; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); my $main = MainWindow->new; my $tk_image = $main->Photo(-data => $image_data); my $screen = $main->Label(-image=>$tk_image)->pack; my $id = $main->repeat(100,\&move); MainLoop; sub move { my ($dx, $dy); my $i = int(rand(100)); if (($i/5) == int($i/5)) { $dx = 0; } elsif (($i/2) == int($i/2)) { $dx = 1; } else { $dx = -1; } my $j = int(rand(100)); if (($j/5) == int($j/5)) { $dy = 0; } elsif (($j/2) == int($j/2)) { $dy = 1; } else { $dy = -1; } if (($x + $dx) > $xsize) { $x = $xsize; } elsif (($x + $dx) < 0) { $x = 0; } else { $x += $dx; } if (($y + $dy) > $ysize) { $y = $ysize; } elsif (($y + $dy) < 0) { $y = 0; } else { $y += $dy; } $image->setpixel(x=>$x, y=>$y, color=>$blue); $cicle++; print "Cicle $cicle. Coords: $x, $y. Diference $dx, $dy.\n"; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); $tk_image->configure(-data => $image_data); $screen->configure(-image=>$tk_image); }