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

Make More Enemies

Having a single enemy that falls from the top of the window to the bottom was a good way to write and test the Enemy class. Now let’s replace that single enemy with a stream of enemies. You can store all the enemies that are in the game using a single Ruby array called @enemies.

images/talking_enemies.png

The Array class has lots and lots of methods,[7] but you’ll use just three of them in Sector Five.

push

The push method adds a new object to the array. When it’s time to add a new enemy, you write:

 
@enemies.push Enemy.new(self)

…and one more enemy is added to the array. It’s up to you to make sure you don’t add anything to the array that’s not an enemy. ...

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