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

The Main Loop


3 of 5

The Main Loop

The main loop is the heartbeat of any game. It's responsible for triggering everything that happens in each frame. It's responsible for pausing between frames to make sure your game doesn't run too fast, processing events from the Operating System, updating the game objects, for drawing everything and, finally, for displaying it on the screen.

The basic structure of a main loop in a game is an infinite loop. In Ruby, the do loop loop is most appropriate loop to use, since it loops forever until a break, return or throw statement. If the QuitEvent event is received, then the loop is broken out of. It's important to always have an exit path from your loops,--even if it's an infinite loop--that will drive the program from start to finish.

The loop itself is contained in a method called main_loop. This makes it easy to break from the main loop by calling return. It also contains the loop in a method so it can be called by the startup routine, and will ensure the Rubygame.quit method is called before the method exits.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. The Main Loop

©2014 About.com. All rights reserved.