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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.