- XNA - Intro :
- XNA Setup :
- The Game Loop :
- State Machines :
- 2D Starter - part 1 :
- 2D Starter - part 2 :
- 2D Starter - part 3 :
- 2D Starter - part 4 :
XNA Development - Basic Concepts: the Game Loop
The mighty Game Loop
At the heart of every game is the game loop.
The game loop can be thought of as a series of steps that happen while the game is running regardless of what the player is doing.
In it's simplest form (coincidentally what XNA gives you) it looks like this:
As the programmer, it's up to you to make sure everything gets updated in the update function and displayed in the draw function. Some of things that need to get done during the update:
- Get player input
- Move everthing on screen
- Check for collisions
- Make decisions based on all the above. (is the player dead? Is the game Over?)
Choices, choices
At this point, we need to decide how to keep track of everything in our game.
For example, in the game Asteroids, we need to track the player, the asteroids, any bullets the player or a UFO has fired and the UFO (if it's on screen).
We could have a player object, a big collection of Asteroid objects, a collection of Bullet objects and a UFO object.
As long as each object has a method to update itself, we can just call those in the main Update() function.
This might look like this:
List<Asteroid> asteroidList;
List<Bullet> bulletList;
UFO myUFO;
protected override void Update(GameTime gameTime)
{
myPlayer.Update(gameTime);
foreach (Asteroid a in asteroidList)
a.Update(gameTme);
foreach (bullet b in bulletList)
b.Update(gameTme);
myUFO.Update(gameTime);
}
Another option is to have a single manager type class that holds all the onscreen objects and does the updates. This might look like this:
protected override void Update(GameTime gameTime)
{
manager.Update(gameTime);
}
{
Player myPlayer;
List<Asteroid> asteroidList;
List<Bullet> bulletList;
UFO myUFO;
Public Update(GameTime gameTime)
{
myPlayer.Update(gameTime);
foreach (Asteroid a in asteroidList)
a.Update(gameTme);
foreach (bullet b in bulletList)
b.Update(gameTme);
myUFO.Update(gameTime);
}
}
The net result of either approach is to update the different objects based on what's happening in the game.
If the player moves the joystick, the ship rotates.
If the player overlaps an asteroid, he loses a ship and the asteroid breaks up.
Even before the game starts, when a player puts in a quarter, the game stops the attract mode and lets the player start the game.
We need a nice way to manage all this.
Enter the State Machine.
