1. Computing

Sprite Groups in Rubygame

By

The Enemy class represents the aliens in our Space Invaders clone. It's very simple, and is more or less the same as the player class with the input mechanisms removed. On its own, an Enemy will move in the direction it's told to move forever. Turning around when it reaches the edge of the screen and moving down are handled by the EnemyFormation sprite group class.

class Enemy
  include Sprites::Sprite

  # Speed in pixels per second
  SPEED = 30

  def initialize(x,y)
    super()

    @image = Surface['enemy.png']
    @rect = @image.make_rect
    @rect.center = [x,y]
  end

  def update(delta, dir)
    @rect.centerx += SPEED * delta * dir
  end
end
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. The Alien

©2014 About.com. All rights reserved.