Fire Bullets

To destroy some enemies, the player shoots them with bullets. Bullets are another kind of sprite, so you can make a class, Bullet, to describe them. When the player presses the spacebar, a bullet will be created. It travels in the direction the ship was pointing when it was fired until it hits an enemy or flies out of the window. When a bullet hits an enemy ship, both are destroyed in a ball of flame.

The Bullet Class

You’ve already made two sprite classes, so you’re probably getting the hang of it. When you make a bullet, you tell it where to start and what direction to go. Once you’ve made a bullet, it should keep moving in that direction until it hits an enemy or flies out the window. Here is a diagram for the Bullet class:

Get Learn Game Programming with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.