Game Structure

Game Structure Guide

Game Structure in Game Programming

Every game follows a general programming structure, often known as the game structure. This structure makes sure helps you set up, run, and terminate things properly. Using this basic game structure will save you headaches later as your game project grows.

The Basic Game Structure

From a bird's eye view, the game code of all games is divided into three sections : Initialization, the game loop and termination.

Initialization

The first section is initialization and it is where you set up everything you need to get your game going. You'll want to set up two sets of variables and set up anything else you may need :

  • The game engine variables
  • The game object variables
  • And anything else

Anything else includes using RANDOMIZE to start the random number generator. However, since Basic4GL seems to automatically seeded, this appears to be unnecessary. If you are working on something more complex than a text game, you'll want to initialize any other thing you will need here. In more complex text games you may want to load a save file. Or, in a 2d game, this is where you would load your images. But, you'll learn about that in later tutorials.

Game Engine Variables

The game engine variables are the variables that the game engine itself will use. One simple example of a game engine variable commonly used is gameOver. This variable is used so the engine can remember if the game is over or should continue. This is primarily used in the main game loop. Another example is a variable to hold the score of the game.

Game Object Variables

Objects in your games will have various qualities that you will use variables to keep track of. For example, if you are going to allow your player to change the main character's name, you will need a variable to hold that name. If the character has health and can carry gold, you will need to create those variables as well and give them starting values.

The Code

Here is what the initialization code might look like :

' INITIALIZATION
' create game engine variables

dim gameOver
dim score

gameOver = 1 'if game over is 1, that means the game continues
score = 0

' create game object variables

dim characterName$
dim health
dim gold

characterName$ = "default"
health = 25
gold = 150

Notice that characterName$ is given a starting value, even though we expect the player to provide a value for it later. This is done just in case there is a bug somewhere. Giving it the value default will help you debug your game later, if needed.

The Game Loop

The game loop is where all the action happens. All your main code will go here. There's quite a bit to say about it, so it has its own tutorial : Game Loop. For now, just keep in mind that this is where all your main game code goes.

Termination

The final section is the Termination section. This is where you perform cleanup and any other tasks that you may want to do before shutting down the game. In text games, there's not much to do here. At most, you may want to use it to call an auto save routine for a large text adventure game.

This page has been recommended for cleanup, integration, etc.

This page was taken from an old tutorial.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.