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

Sprite Groups in Rubygame


The update method of the EnemyFormation class handles the movement pattern of the classic Space Invaders game. It will check if any of the aliens have reached the edge of the screen, and if so change direction and drop down. This is done by setting up a special SCREEN_BOUNDS hash of rectangles.

Imagine the screen bounds rectangles as invisible rectangles just off the edge of the screen. If any of the enemies touch these rectangles, they've gone off the screen. If any of them have gone off the screen, the direction flag is flipped and the entire formation is moved down. This collision detection with the edge of the screen is done in the update method.

def update(delta)
  each do|e|
    # Gone off the rigth side?
    if @dir == 1 and e.rect.collide_rect?( SCREEN_BOUNDS[:right] )
      @dir = -1

    # Gone off the left side?
    elsif @dir == -1 and e.rect.collide_rect?( SCREEN_BOUNDS[:left] )
      @dir = 1

  call :update, delta, @dir
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. Move Left, Drop Down, Change Direction

©2014 About.com. All rights reserved.