1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Sprite Groups in Rubygame


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)

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

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

©2014 About.com. All rights reserved.