Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: Weird Output with Threads and NCurses

by Anonymous Monk
on Jul 19, 2016 at 18:12 UTC ( #1168066=note: print w/replies, xml ) Need Help??


in reply to Re^4: Weird Output with Threads and NCurses
in thread Weird Output with Threads and NCurses

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 :)

Replies are listed 'Best First'.
Re^6: Weird Output with Threads and NCurses
by Anonymous Monk on Jul 20, 2016 at 04:59 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1168066]
help
Chatterbox?
and all is quiet...

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



    Results (109 votes). Check out past polls.

    Notices?