1. Home
  2. Computing & Technology
  3. Ruby

The Main Loop

By , About.com Guide

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.

Explore Ruby
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Tasks & Scripts
  5. Game Programming
  6. The Main Loop

©2010 About.com, a part of The New York Times Company.

All rights reserved.