Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; use Curses; srand( time() ^ ($$ + ($$ << 15)) ); my $score = 0; my @worm; for (my $i=0; $i < 8; $i++) { push @worm, { y => 12, x => 9-$i }; } my $w = Curses->new(); noecho(); my $maxy; my $maxx; $w->getmaxyx($maxy, $maxx); $w->clear; my $t = $w->subwin(1, $maxx, 0, 0); my $o = $w->subwin($maxy-1, $maxx, 1, 0); my $growing; my %prize; $t->addstr(0, 1, "Plibbles: A worm game in Perl"); $t->addstr(0, $maxx-8-length($score), "Score: $score"); $t->refresh; $o->box('|', '-'); draw_worm(@worm); $o->move($worm[0]{y}, $worm[0]{x}); prize(); $o->addstr($prize{y}, $prize{x}, $prize{prize}); $o->refresh(); while(1) { my @oldworm; for (my $wormlen = @worm - 1; $wormlen >= 0; $wormlen--) { $oldworm[$wormlen]{y} = $worm[$wormlen]{y}; $oldworm[$wormlen]{x} = $worm[$wormlen]{x}; } for (my $wormlen = @worm - 1; $wormlen > 0; $wormlen--) { $worm[$wormlen]{y} = $worm[$wormlen-1]{y}; $worm[$wormlen]{x} = $worm[$wormlen-1]{x}; } my $key = $o->getch(); if ($key eq 'h') { $worm[0]{x}--; } elsif ($key eq 'j') { $worm[0]{y}++; } elsif ($key eq 'k') { $worm[0]{y}--; } elsif ($key eq 'l') { $worm[0]{x}++; } else { @worm = @oldworm; $o->clear(); $o->box('|', '-'); draw_worm(@worm); $o->move($worm[0]{y}, $worm[0]{x}); next; } if ($growing != 0) { push @worm, $oldworm[scalar(@oldworm)]; $growing--; } $o->move($worm[0]{y}, $worm[0]{x}); my $char = $o->inch(); if ($char =~ /\d/) { $growing += $char; $score += $char; $t->addstr(0, $maxx-8-length($score), "Score: $score"); $t->refresh(); prize(); } elsif ($char ne ' ') { crash(); } $o->clear(); $o->box('|', '-'); $o->addstr($prize{y}, $prize{x}, $prize{prize}); draw_worm(@worm); $o->move($worm[0]{y}, $worm[0]{x}); $o->refresh(); } endwin(); sub draw_worm { my @worm = @_; my $head = shift @worm; $o->addstr($head->{y}, $head->{x}, '@'); foreach my $segment (@worm) { $o->addstr($segment->{y}, $segment->{x}, 'o'); } } sub crash { endwin; print "\nWell, you ran into something and the game is over.\n"; print "Your final score was $score\n\n"; exit; } sub prize { my ($a, $b); do { $a = rand($maxy-3) + 2; $b = rand($maxx-2) + 1; $o->move($a, $b); } while($o->inch ne ' '); my $v = int(rand(9) + 1); %prize = ( y => $a, x => $b, prize => $v ); }

Edit by tye to replace tabs with spaces


In reply to Plibbles by dooberwah

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2018-06-19 17:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (114 votes). Check out past polls.

      Notices?