To calm crazyinsomniac's fears about holding true to the Space Invader's memory. Here's the kind of patches you can add to the code. This one limits the number of onscreen shots for the player to 4 and adds new functionality: a callback mechanism for when missles get destroyed. (It's also a nice demonstration of a closure.)

Inside of missles() add one line:

if ($t::x<$left or $t::y<$top or $t::x>$right or $t::y>$bot) { &{$_->{callback}} if ($_->{callback}); #NEW $_->{sprite}->remove;
There's the callback handler. And then change the anonymous subroutine that handles the ship controls to something like this:
$mw->bind('<Key>', [ sub { my $curshot=0 if 0; # NEW # Later in that code block... elsif ($_[1] == $Sprite::keycode::fire ) { return if $curshot>3; #NEW $curshot++; #NEW my $gun=new Sprite($c); $gun->image($img{"missle.gif"}); $gun->names("missle","weapon"); $gun->place($t::x, $t::y); push(@missles, { direction => -1, callback => sub {$curshot--}, #NEW sprite => $gun, }); }
And so in 5 lines we can control the number of shots fired by the hero. This can also be used to modify the number of shots fired by the aliens.

And that's why this is a demo. It's for modifying and playing with, and only a framework.


In reply to Re: Perl/Tk Space Invaders Game/Sprite Class by clintp
in thread Perl/Tk Space Invaders Game/Sprite Class by clintp

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.