Galactic Arena - Article II The Game Loop

Glen Martin is the CEO of Dynamic Adventures and the Lead Programmer on Zenfar, a single-player, alien science-fantasy CRPG. This article is the second in a monthly series on creating simple computer RPGs.

Welcome back! Before we get to this months code, I want to thank everybody for their feedback on the first article. I will make sure and try put comments in the code showing what is compiler (Microsoft Visual C++) or operating system (Windows) dependant. Comments show up as green text when using Visual C++, they are notes to yourself or to other programmers about the code, they have no other effect. Also don't fear great features like combat and C++ code will appear in upcoming articles.

Today we are going to explore loops and if statements. These powerful programming constructs will allow us to do cool things like move from place to place and re-roll our characters. Loops allow computer to do a task over and over again (like counting voter ballots). In our case we will use a do-while loop to allow the player to re-roll his character until he gets the best scores he can or he is tired of re-rolling. The other do-while loop in the program is a very simple game loop. The game loop does many things:

If statements or conditional statements allow the program to choose between different branches throughout the code based on user input, program properties, and many other factors. In Galactic Arena the if statements are used to output different text based on which room or area the character is in.

Time to fire up the old compiler. You are going to want to open your Galactic Arena Workspace.



Now if the GalacticArena.cpp file is not open, open it now. You do this by going to the file view and selecting source files and then double clicking on the GalacticArena.cpp file.




// GalacticArena.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"  // Remove this include if you are not using MS Visual C++

#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int Dice(int number,int sides)
{     
	int i,total;

	total=0;

	for (i=0; i<number; i++)
	{  
		total += rand()%sides + 1;  
	}

	return(total);
}

int main(int argc, char* argv[])
{
	int muscle, brains, toughness;
	char action;
	int room;

	// Initialize
	room = 0;

	// Make sure the random number are different each time the program is run
	// We do this by looking at the current time
	srand( (unsigned)time( NULL ) );

	// Introduction
	printf("Welcome to Galactic Arena!\n\n");

	// This is a simple loop that allows you to reroll your character
	do
	{
		muscle = Dice(2,5);
		brains = Dice(2,5);
		toughness = Dice(2,5) + muscle;

		/* printf - \n is for a newline  \t is for a tab */
		printf("Your Alter Ego\n\n");
		printf("Muscle:\t\t%d\n", muscle);
		printf("Brains:\t\t%d\n", brains);
		printf("Toughness:\t%d\n\n", toughness);
		printf("<A>ccept or <R>eroll\n");

		action = getch();
	} while (action == 'r' || action == 'R');  // Check to see if the r key is pressed


	// Game Loop
	do
	{
		if (room == 0)
		{
			printf("You find yourself in the traing pit outside the GALACTIC ARENA!\n");
			printf("<1> Go inside\n");
			printf("<2> Train\n");			
			printf("<Q>uit\n");
			action = getch();

			if (action == '1')
			{
				printf("You enter the GALACTIC ARENA.\n");
				room = 1;
			}
			else
			{
				muscle++;
				printf("Your train for several hours and feel tired but much stronger.\n");
			}
		}
		else if (room == 1)
		{
			printf("The Arena is filled with cheering Citizens from across the Multiverse.\n");
			printf("You prepare for battle...\n");
			printf("A sliding door begins to open slowly...\n");
			printf("<1> Go back to train some more\n");
			printf("<2> Wait\n");
			printf("<3> Prepare for battle\n");
			printf("<Q>uit\n");
			action = getch();

			if (action == '1')
			{
				room = 0;
			}
			else if (action == '2' || action == '3')
			{
				room = 2;
			}
		}		
		else if (room == 2)
		{
			printf("The door to the arena opens!\n");
			printf("Two large humanoids with green skin and glowing red eyes enter the arena!\n");
			printf("The crowd begins to cheer and your pulse quickens...\n");
			printf("<Q>uit - Combat next article\n");
			action = getch();			
		}

		printf("\n");

	} while (action != 'q' && action != 'Q');

	// End of game
	printf("\nThanks for playing...");
	getch();

	return 0;
}


Next type in the C code above or download the file here the finished program is also included.. After you are done it is time to compile the code you can do this by either press F7 or selecting Build GalacticArena.exe from the Build menu. If you see any errors or warnings in the bottom window, check what you have typed in to see if it matches the code above, have patience. If you double click on an error it will take you to the line where the error occured. Now run the program by selecting Execute GalacticArena.exe from the Build menu. Your program should look something like this...



Next article, it will be time for Galactic Arena combat. In the meantime, Loren Davis sent in some information on free compilers and I have added some links to inexpensive versions of Microsoft Visual C++. Learning to program on any compiler under any operating system is well worth the time you will spend, it allows you to be unleash your creative potential in new ways (you could even try to create Galactic Arena for the web). See you next time.