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. ...

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.