Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How do I get my ascii tile code to refresh without looking weird

by Anno (Deacon)
on Sep 12, 2007 at 18:53 UTC ( [id://638647]=note: print w/replies, xml ) Need Help??


in reply to How do I get my ascii tile code to refresh without looking weird

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://638647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found