O'Reilly logo

Learn Game Programming with Ruby by Mark Sobkowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required