another matrix thingy.

Discussion in 'Perl Misc' started by robin, Jun 1, 2005.

  1. robin

    robin Guest

    #!/usr/bin/perl

    srand;
    $c=0;
    while (1)
    {
    print " " x int rand (60);
    print "| | |\n";
    last if $c == 200;
    $c++;
    }


    -r
    robin, Jun 1, 2005
    #1
    1. Advertising

  2. robin wrote:
    > #!/usr/bin/perl
    >
    > srand;
    > $c=0;
    > while (1)
    > {
    > print " " x int rand (60);
    > print "| | |\n";
    > last if $c == 200;
    > $c++;
    > }


    I have no idea what purpose that code is supposed to fullfill nor what
    if anything it has to do with the subject line.

    There are, of couse, several things wrong with the code.
    Brian McCauley, Jun 1, 2005
    #2
    1. Advertising

  3. Brian McCauley <> wrote:

    > robin wrote:
    >> #!/usr/bin/perl
    >>
    >> srand;
    >> $c=0;
    >> while (1)
    >> {
    >> print " " x int rand (60);
    >> print "| | |\n";
    >> last if $c == 200;
    >> $c++;
    >> }

    >
    > I have no idea what purpose that code is supposed to fullfill nor
    > what if anything it has to do with the subject line.


    The output is apparently supposed to look like the computer screens
    from the movie "The Matrix".


    > There are, of course, several things wrong with the code.
    David K. Wall, Jun 1, 2005
    #3
  4. robin

    Guest

    David K. Wall wrote:
    > Brian McCauley <> wrote:
    >
    > > robin wrote:
    > >> #!/usr/bin/perl
    > >>
    > >> srand;
    > >> $c=0;
    > >> while (1)
    > >> {
    > >> print " " x int rand (60);
    > >> print "| | |\n";
    > >> last if $c == 200;
    > >> $c++;
    > >> }

    > >
    > > I have no idea what purpose that code is supposed to fullfill nor
    > > what if anything it has to do with the subject line.

    >
    > The output is apparently supposed to look like the computer screens
    > from the movie "The Matrix".
    >
    >
    > > There are, of course, several things wrong with the code.


    I think that this looks a bit better.

    #!/usr/local/bin/perl
    #matrix.pl
    #Wed Jun 1 15:56:43 MDT 2005
    #
    #
    use warnings;
    use strict;
    use Time::HiRes qw( usleep );

    srand;

    for (my $c; $c <= 200; $c++){
    print " " x int rand 60;
    print "\033[32m|||\n";
    usleep 500;
    }
    __END__

    But there is so much that could be done with this.
    , Jun 1, 2005
    #4
  5. robin

    thundergnat Guest

    wrote:
    >
    > David K. Wall wrote:
    >
    >>Brian McCauley <> wrote:
    >>
    >>
    >>>robin wrote:


    [snip code]

    >>>I have no idea what purpose that code is supposed to fullfill nor
    >>>what if anything it has to do with the subject line.

    >>
    >>The output is apparently supposed to look like the computer screens
    >>from the movie "The Matrix".
    >>
    >>
    >>
    >>>There are, of course, several things wrong with the code.

    >
    >
    > I think that this looks a bit better.
    >


    [snip code]

    >
    > But there is so much that could be done with this.
    >


    Hmm. I don't think *either* of those look terribly "Matrix-like".
    Niether does this particularly, but it was entertaining to write.

    #!/user/bin/perl

    # Set the randomness to zero and you have Rain.
    # Set the randomness low WRT Velocity and you have Swarm
    # Set the randomness high WRT Velocity and you have Brownian Motion

    use strict;
    use warnings;
    use Tk;

    my $top = MainWindow->new();

    my $maxobj = 50;
    my $maxvel = 15;
    my $run = 0;
    my ($height,$width,$maxx, $maxy, $minx, $miny);
    my @objects;
    my @printable;
    my $color = '';
    my $randomness = 4;

    $minx = $miny = 0;

    for (20 .. 255){
    push @printable, chr($_) if (chr($_) =~ /\p{Print}/);
    }

    my $canvas = $top->Canvas(
    -background => 'black'
    )->pack(-fill => 'both', -expand => 'y');

    $top->bind('<Configure>' => sub {
    $top->XEvent;
    $maxx = $canvas->width - $minx;
    $maxy = $canvas->height - $miny;
    }
    );

    my $frame = $top->Frame->pack;

    $frame->Label(
    -text => 'Number of objects',
    )->grid(-row => 1, -column => 0);

    my $maxobjentry = $frame->Entry(
    -textvariable => \$maxobj,
    -width => 5
    )->grid(-row => 1, -column => 1);


    $frame->Label(
    -text => 'Maximum Velocity'
    )->grid(-row => 1, -column => 2);

    my $maxventry = $frame->Entry(
    -textvariable => \$maxvel,
    -width => 5,
    -validate => 'all',
    -vcmd => sub{
    if ($_[0] =~ /[^\d]+/){
    return 0;
    }else{
    return 1;
    }
    }
    )->grid(-row => 1, -column => 3);

    $frame->Label(
    -text => 'Randomness'
    )->grid(-row => 1,-column => 4);

    my $maxrandentry = $frame->Entry(
    -textvariable => \$randomness,
    -width => 5,
    -validate => 'all',
    -vcmd => sub{
    if ($_[0] =~ /[^\d]+/){
    return 0;
    }else{
    return 1;
    }
    }
    )->grid(-row => 1,-column => 5);

    $frame->Button(
    -text => 'Start',
    -command => sub{ $run = 1; setup(); run_it();},
    -width => 6,
    )->grid(-row => 1, -column => 6);

    $frame->Button(
    -text => 'Pause',
    -command => sub{
    $run = 0;
    $maxobjentry->configure(-state => 'normal');
    },
    -width => 6,
    )->grid(-row => 1, -column => 7);

    $frame->Button(
    -text => 'Resume',
    -command => sub{ $run = 1; run_it(); },
    -width => 6,
    )->grid(-row => 1, -column => 8);

    my @letters = split //,'Not "The Matrix")';

    for (0..$#letters){
    $objects[$_]{object} = $canvas->createText((120+$_*18),100,
    -text => $letters[$_],
    -fill => 'light green',
    -font => '-*-Courier-Medium-R-Normal--*-280-*-*-*-*-*-*'
    );
    }

    $maxventry->bind($maxventry, '<Up>' => sub { $maxvel++ });
    $maxventry->bind($maxventry, '<Down>' => sub { $maxvel-- });

    $maxrandentry->bind($maxrandentry, '<Up>' => sub { $randomness++ });
    $maxrandentry->bind($maxrandentry, '<Down>' => sub { $randomness-- });

    $top->protocol('WM_DELETE_WINDOW' => sub{$run = 0, $top->update; $top->destroy});

    MainLoop;

    sub setup{
    return unless $run;
    $maxobjentry->configure(-state => 'disabled');
    if (@objects){
    for (@objects){
    $canvas->delete($$_{object});
    }
    @objects = ();
    }
    my @fnames = qw/Times Hevatica Courier/;
    for (0 .. $maxobj-1){
    my $x = int(rand($maxx));
    my $y = int(rand($maxy));
    for (0 .. 2){
    my $rgb = unpack('H*', pack('n', (int(rand(192)+64))));
    $rgb =~ s/.+(\w\w)$/$1/;
    $color .= $rgb;
    }
    my $font = '-*-'.$fnames[int(rand(2)+.5)].'-Medium-R-Normal--*-'.(int(rand(40)+20)*10).'-*-*-*-*-*-*';
    $objects[$_]{velx} = 0;
    $objects[$_]{vely} = int(rand($maxvel)+1);
    $objects[$_]{object} = $canvas->createText($x, $y,
    -text => $printable[int(rand(@printable))],
    -fill => '#'.$color,
    -font => $font
    );
    $color = '';
    }
    }
    sub run_it{
    while ($run) {
    $maxvel = 0 if ($maxvel !~ /\d/);
    $randomness = 0 if ($randomness !~ /\d/);
    for(0 .. $#objects){
    $objects[$_]{velx} += int(rand($randomness*2)+.5)-$randomness if $randomness;
    $objects[$_]{vely} += int(rand($randomness*2)+.5)-$randomness if $randomness;

    $objects[$_]{velx} = $maxvel if ($objects[$_]{velx} > $maxvel);
    $objects[$_]{velx} = -$maxvel if ($objects[$_]{velx} < -$maxvel);
    $objects[$_]{vely} = $maxvel if ($objects[$_]{vely} > $maxvel);
    $objects[$_]{vely} = -$maxvel if ($objects[$_]{vely} < -$maxvel);

    $canvas->move($objects[$_]{object}, $objects[$_]{velx}, $objects[$_]{vely});

    my ($x,$y) = $canvas->coords($objects[$_]{object});

    if ($x > $maxx){ $canvas->move($objects[$_]{object}, -$maxx,0) }
    if ($x < $minx){ $canvas->move($objects[$_]{object}, $maxx,0) }
    if ($y > $maxy){ $canvas->move($objects[$_]{object}, 0, -$maxy) }
    if ($y < $miny){ $canvas->move($objects[$_]{object}, 0, $maxy) }
    }
    $top->update;
    }
    }
    thundergnat, Jun 2, 2005
    #5
  6. robin

    robin Guest

    wow....this is great.
    I gotta learn more.
    -robin
    robin, Jun 2, 2005
    #6
  7. <fullquote>
    robin wrote:
    > wow....this is great.
    > I gotta learn more.
    > -robin

    </fullquote>

    What is great? It would be great if you would quote enough context such that
    people could know what you are talking about.
    Hopefully one of the first things you learn will be how to create Usenet
    postings that other people can understand.

    jue
    Jürgen Exner, Jun 3, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M.E.
    Replies:
    0
    Views:
    351
  2. David Graham

    bucies image selector thingy

    David Graham, Jul 3, 2003, in forum: HTML
    Replies:
    4
    Views:
    553
    brucie
    Jul 3, 2003
  3. Robbie

    loading message thingy

    Robbie, Aug 11, 2004, in forum: Python
    Replies:
    3
    Views:
    265
    John J. Lee
    Aug 12, 2004
  4. Mark Hobley

    Another doubleunderscore thingy

    Mark Hobley, Feb 28, 2010, in forum: C Programming
    Replies:
    11
    Views:
    890
    Ersek, Laszlo
    Mar 1, 2010
  5. Zed A. Shaw
    Replies:
    2
    Views:
    119
    Zed A. Shaw
    Jun 29, 2005
Loading...

Share This Page