1. Technology

Programming Games in C - Tutorial Three on Star Empires


1 of 5

Explaining the Game Loop
Programming Games in C - Tutorial Three on Star Empires

In tutorial one, I covered creating the game galaxy and adding the ships to the initial setup at each system. The main game mechanic is sending ships to other systems to attack and conquer them (or not). Anyway, it's easier to describe the game from top down so let's start at line 1001, DisplayDistances().

The first couple of Display functions are only needed to be called once each, so they are just before the game loop which is the while (!GameOver). GameOver is one of the few global variables; it's an int and is initialized to 0 (meaning false) in InitData(). As long as it is 0, the game will continue. All games, whether they are a simple text game like this or the latest Call of Duty rely on a Game Loop at the heart of the game.

What is a Game Loop?

All actions are sync'd to the game loop which in Star Empires runs at the speed of SECSPERTURN (5 unless changed) seconds per loop. Every time round the Loop the following are called:

DisplayMap() ;
DisplayFleets() ;
GetandProcessOrders() ;
EnemyOrders() ;
if (MoveFleets())
    DoFights() ;
BuildFleets() ;
ShowTurn() ;

DisplayMap is a simple double loop that displays the 5 x 5 Galaxy showing where the planets are and who owns them. There's a function owner(x,y) which takes the coordinates of each system and then calls getsystem(x,y) to return the index of the system and from there look up the owner member of that system and return it.

This is not the fastest way of implementing this. Arguablly using another 5 x 5 array of ints would be quicker with a value of -1 for all empty locations and unowned systems and the values 0 or 9 for those owned by the player or the enemy. This is a tradeoff common to all programming: space or speed. In this case I thought it would make the program more complex, storing the owner in two places (in the system and on a separate owner map) and making sure that both were updated.

DisplayFleets show all systems and a simple summary of how many ships are going to a system and how soon they will arrive, but first what is a Fleet?

What is a Fleet?

I've already described in tutorial one how fleets are implemented using a struct.

struct fleet {
int from;
int to;
int turns;
int numships;
int owner; /* (o or 1) */

The game starts with no fleets in play, just ships based at systems. When we want to move ships to another system, we move them into a fleet and store it in the fleets array. Each turn fleets are moved one space nearer their destination and when they arrive, they either fight any enemy fleets there, then the ships defending the system, or they reinforce the system with the ships from the fleet and the fleet is removed.

On the next page: Keyboard Input and Orders

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. How to program games in C. Tutorial Three - Star Empires

©2014 About.com. All rights reserved.