Note: What the heck? Everything2 is at least two years old now, and there is NO node for Game Programming? Well, I will fix this post-haste!

Game programming is the art and craft of writing programs for computers (or microcontrollers or embedded devices or whatever) that play games. Whether you're playing Deus Ex on your Thunderbird 1.4 gHZ/GeForce 3/512 meg monster or Snakes on your Nokia cellphone, the basic concepts behind game programming are the same.

Game Programming 101

I've run into many young C programmers who specifically want to make games but didn't learn what they wanted to know from their introductory classes. That's because game programming is very different from the simple direct-procedure programs that most introductory classes use to teach.

So exactly how do game programs differ from most other programs? Well, if you've been going through a tutorial, all the programs you've written have almost certainly been linear affairs...they start, get a little input, do a little something, then exit. You may use a loop inside your program, but it's always something with a definite terminaion point, and its job is to calculate factorials or something.

Game programs are different in that they don't exit until the user explicitly tells them to. So a game program is one big loop.

The Main Game Loop

#include <stdio.h>

int flag;

void initialize_game()
{
    printf("initialize_game()\n");
    printf("Game variables are initialized here.\n");
    printf("This function can be called more than\n");
    printf("once if, for example, the player abandons\n");
    printf("his current game and starts a new one.\n\n");
    flag = 1;
}

void process_game_data()
{
    printf("process_game_data()\n");
    printf("This function updates the game data based on\n");
    printf("input receieved and the state of other game\n");
    printf("variables.  When it's done, a frame of animation\n");
    printf("can be drawn.\n\n");
}

void draw_screen()
{
    printf("draw_screen()\n");
    printf("This function draws one frame of animation,\n");
    printf("based on the input given in get_input()\n");
    printf("and the current state of the game\n");
    printf("variables.\n\n");
}

void get_input()
{
    printf("get_input()\n");
    printf("This function gets input from the user and\n");
    printf("changes game variables based on that input.\n\n");
    flag = 0;
}

void clean_up()
{
    printf("clean_up()\n");
    printf("The opposite of initialize_game(), this function frees\n");
    printf("all allocated resources.  Again, this would be called\n");
    printf("if the user decided to quit his current game and start\n");
    printf("a new one, so it's implemented as a function.\n");
}


int main()
{

   initialize_game();

   while(flag)
   {
      process_game_data();
      draw_screen();
      get_input();
   }

   clean_up();

   return 0;
}
(Note: I wrote this in C because it's what I know. If you know how to write a similar game structure in another language (Java, Visual Basic, etc) please add it to this node!)

This structure is sufficient to write any game, no matter how simple or complex. Notice that games are simple things. They put something on the screen, get input, process the game data, and put something new on the screen. And they do this until the user says to stop.

Real-Time Input and Output

So what else do you need to write a game? You need two more things: real-time input and real-time output. These are the two things that make computer gaming possible.

If you're using C, you may have discovered the various input-getting functions like getchar(), getch() and getche(). All of them are insufficient for game programming, because all of them wait for input.

However, there is a non-ANSI function that is implemented in most versions of C called kbhit() or _kbhit(). This function returns TRUE if there is a key waiting in the buffer and FALSE if there isn't. We can combine kbhit() with getch() to get near-real-time input.

Output is iffier. Depending on your implementation of C, there may be (non-ANSI) functions that allow you to clear your screen, and perhaps even move your cursor anywhere you wish. These functions, if you can find them, will give you near-real-time output, enough to write a game with.

Of course, such a game would be primitive, but would allow you to get started and learn a few things. Code libraries can help if you're familiar with them. The Simple DirectMedia Layer can provide you everything you need to write a game with. It's cross-platform and works on Linux, Windows, and MacOS (and is currently being ported to even more platforms).

If you're really interested in making your living as a game programmer, I have one piece of advice: start small. The first game you write on your own should be something you are absolutely certain you can do. Write Space Invaders or Asteroids, don't try to write a first-person shooter.

Why start small? Because you should finish every game you start. I can't stress this enough. If you're programming only for your own benefit, your game can be in production forever and no one is going to care. But if your goal is to do this for a living, you will need to show the ability to finish what you start. Don't let feature creep take over, and don't put off coding the opening menu because it's tedious and not very fun. When your game does what you originally intended it to do, and it starts up and exits cleanly, stop working on it, post it on the internet, and start a new game.

Well, I hope I answered a few questions about what game programming is, how it works, and how to get started doing it. We'll finish with some links:

Simple DirectMedia Layer, the best cross-platform game development layer you can get, and it's free:
http://www.libsdl.org/

Nehe's OpenGL tutorials, the best place to learn how to do lots of groovy things with OpenGL. Once again, free:
http://nehe.gamedev.net/

GameDev and FlipCode are both good repositories of game programming articles:
http://www.gamedev.net/
http://www.flipcode.com/

GamaSutra is the mother of all game creation sites, and has years worth of archived articles covering every facet of game creation:
http://www.gamasutra.com/

There. That should get you started.

Now for a morale boost. Here's a quote from TC Lee, programmer for the superb game Starflight.

"Maybe you'd like to build a game like 'Starflight' but are afraid that you don't have what it takes. Keep in mind that five motivated guys worked full time for three years to build 'Starflight' and that you experienced the result in a fraction of that time. The mental impact is magnified by the compression of fifteen man-years of production into a few weeks of consumption. On our time scale what we did was nothing superhuman, but from your perspective it might seem that way."

Don't be intimidated by good games you've played; remember, you are experiencing in mere hours a game that took many professionals years to build. It's like a magic trick - you see the trick, but you don't see all the practice the magician put into making the trick work. If you're willing to put in the time necessary to practice, you can be just as good as they are.


There are multiple ways of creating that sort of loop in Visual Basic.

One of the best methods is to create a Timer which, upon its first call, calls a Sub or Function which contains your main game loop, something like this:

Dim Running As Boolean

Sub Timer1_Timer()
Timer1.Enabled = False ' Make sure we don't start multiple loops.
Call GameLoop() ' Begin the loop!
End Sub

Sub GameLoop()
Do While Running
   ' Update your game data here.
   ' Redraw here.
   DoEvents ' Make sure that you don't hog the CPU.
Loop
End Sub

Sub Form_Unload()
Running=False
End Sub

This method allows you to create a sort of multi-threading in Visual Basic without the complexity of API calls.

Log in or register to write something here or to contact authors.