Collision Detection
Finally, all the pieces are in place for your game. You've learned how to create a game window in Rubygame. You can load and display images on the screen. You can move these images around. You can even arrange these images on the screeninto meaningful groups. The last thing you need to make this a "game" is collision detection.
Collision detection tells us what sprites are touching each other. It tells us which aliens are being hit by our lasers, which of the alien plasma beams are hitting our player and gives us a chance to react to this.
Collision detection is where all the interaction comes in.
Before getting started, be sure to download the part 3 example files. This is a different file than those used in the previous gaming articles, so you'll need to download the file using the above link. Note that all pixel art (such as the player, aliens and bullets) were created by Pixel_Outlaw.
Both the player and the alien enemies shoot bullets. These are not, however, represented by separate classes; they're represented by a single class. The type of the bullet is determined by which direction it's traveling. In this case (our Space Invaders clone) this is particularly convenient because the player's bullets are always traveling up and the alien's bullets are always traveling down.
The bullet class isn't anything new to you.
It's a lot like the alien and player classes. It moves on its own and, if it goes off the screen, it deletes itself from the game (or "kills" itself). What the bullet class does not do is collision detection against the player and aliens. A bullet doesn't know anything about players and aliens. It just knows where it's going.
The bullets themselves are created either randomly by a timer from the aliens or when the player presses the space bar. Two new sprite groups are created to handle these bullets: a sprite group as a member of the EnemyFormation sprite group and a sprite group as a member of the Player class. This keeps everything organized and, again, keeps complexity out of the main loop.
Here's a snippet from the EnemyFormation class' update method. Every so often, this will let a bullet be fired from a random alien.
Great, now we have bullets in groups all ready for collisions, so let's do it! Here, I've decided to put the collision detection calls inside the main loop. It could have been abstracted a bit, but it's pretty short anyway. If this game got much bigger, you'd probably want to clean that up but as it is right now, this is fine.
Here's a section of the main loop showing the collision detection. As you can see, it's pretty straightforward. The player's bullets are checked for collisions against the entire enemy formation. For every collision, the block which kills both the bullet and the enemy is run. The same thing is repeated for collisions against the alien bullets and the player (only with slightly different conditional syntax).
Finally, all the pieces are in place for your game. You've learned how to create a game window in Rubygame. You can load and display images on the screen. You can move these images around. You can even arrange these images on the screeninto meaningful groups. The last thing you need to make this a "game" is collision detection.
Collision detection tells us what sprites are touching each other. It tells us which aliens are being hit by our lasers, which of the alien plasma beams are hitting our player and gives us a chance to react to this.
Collision detection is where all the interaction comes in.
Before getting started, be sure to download the part 3 example files. This is a different file than those used in the previous gaming articles, so you'll need to download the file using the above link. Note that all pixel art (such as the player, aliens and bullets) were created by Pixel_Outlaw.
Both the player and the alien enemies shoot bullets. These are not, however, represented by separate classes; they're represented by a single class. The type of the bullet is determined by which direction it's traveling. In this case (our Space Invaders clone) this is particularly convenient because the player's bullets are always traveling up and the alien's bullets are always traveling down.
The bullet class isn't anything new to you.
It's a lot like the alien and player classes. It moves on its own and, if it goes off the screen, it deletes itself from the game (or "kills" itself). What the bullet class does not do is collision detection against the player and aliens. A bullet doesn't know anything about players and aliens. It just knows where it's going.
The bullets themselves are created either randomly by a timer from the aliens or when the player presses the space bar. Two new sprite groups are created to handle these bullets: a sprite group as a member of the EnemyFormation sprite group and a sprite group as a member of the Player class. This keeps everything organized and, again, keeps complexity out of the main loop.
Here's a snippet from the EnemyFormation class' update method. Every so often, this will let a bullet be fired from a random alien.
if (Clock.runtime - @last_fired) * 0.001 > FIRE_RATEa = self[ rand(length) ]@bullets.push Bullet.new( [ a.rect.centerx, a.rect.centery + 16 ], :enemy )@last_fired = Clock.runtimeend
Great, now we have bullets in groups all ready for collisions, so let's do it! Here, I've decided to put the collision detection calls inside the main loop. It could have been abstracted a bit, but it's pretty short anyway. If this game got much bigger, you'd probably want to clean that up but as it is right now, this is fine.
player.update(delta)enemies.update(delta) player.bullets.collide_group(enemies) do|bul,enem|enem.killbul.killendunless enemies.bullets.collide_sprite(player).empty?player.dieend background.blit( screen, [0,0] )player.draw( screen )enemies.draw( screen )
Here's a section of the main loop showing the collision detection. As you can see, it's pretty straightforward. The player's bullets are checked for collisions against the entire enemy formation. For every collision, the block which kills both the bullet and the enemy is run. The same thing is repeated for collisions against the alien bullets and the player (only with slightly different conditional syntax).
SHARE