in reply to Perl/Tk Space Invaders Game/Sprite Class
Inside of missles() add one line:
There's the callback handler. And then change the anonymous subroutine that handles the ship controls to something like this:if ($t::x<$left or $t::y<$top or $t::x>$right or $t::y>$bot) { &{$_->{callback}} if ($_->{callback}); #NEW $_->{sprite}->remove;
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.$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 that's why this is a demo. It's for modifying and playing with, and only a framework.
|
|---|