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

Loading and Displaying Your Sprite


3 of 5

About Rectangles in Rubygame

Many things in Rubygame require you to manipulate rectangles (or "rects"). For example, a rect is used to manage the position and dimensions of a sprite, used in collision detection and can be used to represent any rectangular area in your game. These rects are simple classes with X and Y coordinates, as well as width and height.

Rubygame provides the Rect class to manipulate rects. You can either manipulate the x, y, w and h attributes manually, or use one of the many helper methods Rubygame has thoughtfully provided for you. Since manipulating and positioning rects is such a common thing, Rubygame has provided quite a few helpers for you. Here are a few that will be most helpful.

  • bottom, left, right and top - Position the rect so that the bottom, left, right or top are at this X or Y position. This can be useful if the player has gone off the screen to the left, as you can assign the player's rect's left to the left side of the screen.

  • center - By default, rects are most of the time referred to by their top-left coodinates. This is not terribly useful in most cases, so Rubygame provides the center method. Using the center method, you can move the center of the rect to the X and Y coordinates you give it. This is a much handier method of moving things around than moving the top left corner.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. About Rectangles in Rubygame

©2014 About.com. All rights reserved.