The Basic Game Loop
by Geoff Howland

There is a basic formula that almost every game, that is what is called a "game loop". The game loop is just a series of procedures for getting input and displaying output to the player and updating the game. There are 3 basic processes that go on inside a game loop and 2 outside of it.

Starting the Game

The beginning of every game usually consists of an animation sequence to show off some aspect of the game's story or background and an option menu for starting the game or changing various parameters that affect the game in some way. Those parameters often include sound volume, graphic options, multiplayer options, difficulty and starting level.

Player Input

These routines will take the player's input from whatever device they are using and store it in a way that the game can process to make changes to the game internals.

Updating Game Internals

These routines are the real guts of the game. Everything from moving the player's character using their input, to the actions of the enemies and determining whether the player has won or lost the game is determined here.

Displaying the Screen

This actually can be done in two ways. You can either draw everything to the screen at one time or what is more commonly done, set everything up to be drawn and then draw the screen afterwards. Drawing the screen can take longer than most processes in a game so you want to do it all at one time. Determining what is necessary to be drawn can take a relatively long time with all the checks your game could have, so it's best to do this before you actually try to commit your graphics to the screen or video hardware.

Ending the Game

Once the game is over there is normally an ending sequence, or at least something that says goodbye to the player.

Conclusion

Understanding what elements go into creating a game loop can definitely make starting your first game easier. Before you start programming anything, sit down and draw out a small flowchart or list of things you want to have happen and for the player to see. You'll find it makes the actual programming and end result much better.

Discuss this article in the forums


Date this article was posted to GameDev.net: 9/17/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
General

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!