So essentially you want to show an animated image of your game map that
shows a "@" moving around. The following code shows a simple solution,
based on the assumption that your system has a
clear command that prints the character sequence to clear the screen.
The main difference to your approach is that it sets up the entire image as an array of lines instead of printing every character as it comes along. This is done by the sub image(). It uses the core module Time::HiRes to set the frame rate with sub-second resolution. The "@" is moved through a sample of six positions and back again.
use Time::HiRes qw( sleep);
use constant SIZE => 15;
my @map = do {
my @ones = ( 1) x SIZE;
my @mixed = ( 1, ( 0) x (SIZE - 2), 1);
(
[ @ones],
map( [ @mixed], 1 .. SIZE - 2),
[ @ones],
);
};
my @positions = ( [ 8, 4], [ 9, 5], [ 10, 6], [10, 7], [10, 8], [ 10,
+9]);
my $clear_string = `clear`;
for my $pos ( @positions, reverse @positions ) {
print $clear_string;
print image( \ @map, @$pos);
sleep 0.2;
}
exit;
sub image {
my ( $map, $x, $y) = @_;
my @image = map join( '', map $_ ? '* ' : ' ', @$_), @$map;
$_ .= "\n" for @image;
substr( $image[ $y], 2*$x, 1) = '@';
@image;
}
Anno