Live, Collide, Die

Fine, fine, there are a couple more things happening in the fourth file.

First, on line 261, we have this line:

player_laser_sprites.add( PlayerLaserSprite( player_sprite.rect.midtop ) )

Wuh? All that does is ask that a new laser sprite be added to the board every time we hit the spacebar. And we ask that it appear in the area of the player_sprite... at the middle top of it, to be exact. Once we've added the sprite, it just goes, passed on its own rules established in "update." Bang!

If you look in the update function for the EnemyClass, you'll see that is does the exact same thing for enemy laser blast, except that it does it randomly instead of at every press of the spacebar.

The other feature worth noticing is at line 292. This is where we figure out if sprites in the player's laser group and sprite in the enemy group overlap. This is called collision detection. This function tests to see if there was a collision, removed sprites from both groups that have collided, and then returns a list of those collisions.

Right now, we don't want to do much of anything beyond removing the sprites, but we could use this list of collision in a loop to play sounds or have the game display a message or whatever at each collision. In fact, at line 297, we ask that, when the player sprite collides with the enemies' laser sprites, the main loop should stop. That's another way of saying...


Copyright Mike Edwards 2006-2009. All content available under the Creative Commons Attribution ShareAlike license, unless otherwise noted.