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

In reply to Re: How do I get my ascii tile code to refresh without looking weird by Anno
in thread How do I get my ascii tile code to refresh without looking weird by JonPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.