Simple game? (note: I've never played the original or any variation thereof).

#!/usr/bin/perl # # aliens.pl - game using Async::Tiny my @ship = split /\n/, <<END; # ### ##### # # END my @explosion = split /\n/, <<END; ! % ^ ^ < # > @ . END use Curses; use Term::ReadKey; use Async::Tiny; use Time::HiRes 'time'; use strict; my ($width, $height) = GetTerminalSize; my @aliens; # {row, col, state} my @missles; # {row, col} my $alienwidth = 5; my $shooterx = int $width / 2; my $base = $height - 5; my $shoottime = 0; my $aliencount = 0; sub drawalien { my ($row, $col) = @_; addstr $row++, $col, $_ for @ship; } sub drawexplosion { my ($row, $col) = @_; addstr $row++, $col, $_ for @explosion; } my $t = Async::Tiny->new; $t->addWaitCallback( sub # do full draw { clear; # draw aliens for my $alien (@aliens) { $alien->{state} eq 'dead' and next; $alien->{state} eq 'exp' and drawexplosion( $alien->{row}, $alien->{col} ), $alien->{state} = 'dead', next; drawalien( $alien->{row}, $alien->{col} ); } for my $m (@missles) { addstr $m->{row}, $m->{col}, '|'; } addstr $base, 0, '=' x $width; addstr $base + 4, 2, scalar localtime; addstr $base + 2, 2, 'active missles: ' . @missles; move $base - 1, $shooterx; refresh; }); sub left { $shooterx > 0 and --$shooterx } sub right { $shooterx < $width - 1 and ++$shooterx } sub shoot # fire missle { $shoottime + 1 > time and return; push @missles, {col => $shooterx, row => $base - 2}; $shoottime = time; } sub hit # test for missle hit on ship { my ($row, $col) = ($_[0]{row}, $_[0]{col}); for my $alien (@aliens) { $alien->{state} eq 'dead' and next; my ($ar, $ac) = ($alien->{row}, $alien->{col}); $row < $ar || $row > $ar + 2 || $col < $ac || $col > $ac + 4 and next; $alien->{state} = 'exp'; --$aliencount or die "you win\n"; return 1; } return 0; } $t->addRepeatCallback( .15, sub # move missles { @missles = map { if( $_->{row} > 0 ) { --$_->{row}; hit($_) ? () : $_; } else { () } } @missles; } ); $t->addReadCallback( *STDIN, sub # process arrow keys + 'q' { my ($string) = @_; for ($string =~ /./gs ) { /A/ ? shoot() : /D/ ? left() : /C/ ? right() : /q/ ? die "quit\n" : 0 } }); $t->changeReadMode( *STDIN, 'character' ); # create ships across top for (my $col = 3; $col < $width - $alienwidth - 3; $col += $alienwidth + + 3) { push @aliens, { row => 1, col => $col, state => 'live' }; $aliencount++; } for my $alien (@aliens) # move aliens { $t->addRepeatCallback( 1 + rand 1, sub # move an alien { $alien->{state} eq 'dead' and return; ++$alien->{row} + 3 >= $base and die "you lose\n"; } ); } initscr; clear; ReadMode 'cbreak'; eval { $t->eventloop }; my $errormsg = $@; ReadMode 'restore'; endwin; print $errormsg;

It's just a proof of concept :)


In reply to Re^5: Weird Output with Threads and NCurses by Anonymous Monk
in thread Weird Output with Threads and NCurses by var121

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.