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

Make Your Sprite Move


When talking about the main loop, we glossed over the event queue. However it hasn't been used except to detect the QuitEvent events. Remember that all types of events, including keyboard and mouse input, are reported through the event queue. Events are generated whenever keys are pressed, released, the mouse is moved or when any of the mouse buttons are pressed or released.

When keys are pressed, they generate a KeyDownEvent. This event object has an attribute called key that tells which key was pressed. This will be the way to get the player moving.

When keys are released, they generate a KeyUpEvent. This event object has an attribute called key as well, which tells us which key was released. This is how we'll stop the player moving when the player releases the key.

The keys you'll be using are the arrow keys. These arrow keys are referred to as the constants K_LEFT, K_RIGHT, K_UP and K_DOWN. If the down arrow is pressed, the KeyDownEvent's key attribute will equal K_DOWN.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. Revisiting the Event Queue

©2014 About.com. All rights reserved.