lightatdawn

Hosted By:
cprogramming.com



BASIC GAME COMMANDS INTRODUCTION by LIGHTATDAWN

I'v e decided to write this short introduction to basic game programing as a lead-in to the text game example that i wrote about a year ago. They're not really related in any way but this Introduction will help out people who are just getting into C++ programing and want to start on the gaming path. Its basically just going be be a compilation of a few of the more useful simple commands used in games (and other things of course but we're focusing on games here).

NOTES:

a) I use the C printf function over the C++ cout function. Its faster and i personally prefer the format-specification method of printf over the unstructured method of cout.
b) I also dont use cin or most other iostream. Its slow and i dont like it. Its that simple.
c) I'm not going to get technical. I'm going to keep the explanations more to HOW things work rather than WHY. Get a technical manual if you want to get... technical.
d) All my variables will be in the my_[variabletype]_[furtherinformation] format.
e) Oh ya, about headers; I only showed the ones you need for the particular command i'm explaining not all of the ones that you'll need for any particular code snippet.


SECTION 1 - ARRAYS

Ok, ok, i know arrays aren't functions, but its essential for any program, and there are people who dont properly understand them. I'm just going to cover the basics of this.

int my_integer_array[10]; //this declares your array of integers (10 of them in this case) .

Arrays can be used in exactly the same manner as a non-array of their data type. Like:

my_integer_array[0] = 100;

That line initialized the first element of the array to 100. The first element in an array is always 0. So an array with 10 elements would be 0-9. Arrays can also be initialized at the same time that they're declared like any other variable. The method is slightly different:

//Normal variable
int my_integer = 10;
//Array
int my_integer_array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Also; Arrays can be multi-dimensional. Like this:

int my_integer_multidimensionalarray[10][25];

used in pretty much the same manner:

my_integer_multidimensionalarray[0][24] = 55;


SECTION 2 - LOOPS

There are 2 main types of loops that i'll cover here. The "for" and the "do" loop.

THE FOR LOOP

The for loop is used mainly to increment variables, or count or sort a quantity of data (usually stored in an array).

int i;//we'll need an integer to count with

for(i = 0; i < 10; i ++)
{
printf("The data contained in my_integer_array[%i] is: %i\n", i, my_integer_array[i]);
}

Look to the printf section if you dont understand printf. Lets look at the for loop in pieces:

i = 0;//initializes your "i" variable to 0. It could be any number but 0 works here.
i < 10; //This goes through the loop while "i" is less than (<) 10. == < > != are also all legal operands in a for loop.
i ++;//This adds 1 to "i" on each pass through the loop.
//Other common arguments are: i --, i += anynumber, i -= anynumber, i += my_other_integer_variable, etc...

THE DO...WHILE LOOP

The do loop is usually the core loop of a game. Most of the actions that take place will happen inside a do loop. I'll show the same affect as above but using a do loop instead.

int i;

i = 0;
do
{
i ++;
}while(i < 10);

As you can see in this situation the do loop isn't as tidy as the for loop but it uses all the same elements to produce the same affect. do loops are usually used in other ways such as:

int my_integer_input;

do
{
my_integer_input = _getch();
}while( ch != 0 );

That code loops endlessly waiting for the user to press "0".


SECTION 3 - PRINTF

First lets look at a basic printf example.

#include <stdio.h>

printf("Hello World!\n");

The \n is like pressing ENTER. Without this the next printf would continue on the same line. Try removing it and watch what happens if you have other printf statements following it. \n\n would leave a blank line between each of your text lines. You get the picture...

Now thats great, but not incredibly useful. What happens when we want to show a number, like for instance Hit Points of your character in a game? printf accepts any number of arguments that you chose to give it. I'll start by using only one argument of the integer type and then show others.

int my_integer_variable = 111;

printf("Lets display our integer: %i\n" , my_integer_variable);

Output will be:

Lets display our integer: 111

So whats that '%i' thing? Thats the format specifier. %i tells us that the output should be in integer format. If we wanted to display a string, we would have wanted %s, like this:

char * my_string_variable = "Hello World!";

printf("lightatdawn says: %s\n" , my_string_variable);

Output will be:

lightatdawn says: Hello World!

Other specifiers include %f for floats, %c for a char, etc. Look up 'format specification fields' in your help files for more information.

You can have multiple arguments in a printf statement. I'll use the for loop code as an example:

int my_integer_array[10] = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

int i;

for(i = 0; i < 10; i ++)
{
printf("The data contained in my_integer_array[%i] is: %i\n", i, my_integer_array[i]);
}

Output:

The data contained in my_integer_array[0] is: 2
The data contained in my_integer_array[1] is: 4
The data contained in my_integer_array[2] is: 8
The data contained in my_integer_array[3] is: 16
The data contained in my_integer_array[4] is: 32
The data contained in my_integer_array[5] is: 64
The data contained in my_integer_array[6] is: 128
The data contained in my_integer_array[7] is: 256
The data contained in my_integer_array[8] is: 512
The data contained in my_integer_array[9] is: 1024


SECTION 4 - USER INPUT

Alright, now comes the part that everyone always has questions about. User input. There are literally hundreds of methods to get input from the user but i'll just go into the tried-and-true methods here...

FGETS

#include <stdio.h>

char string[40];

printf("Type something:");
fgets(string, 40, stdin); //Syntax: char *fgets( char *string, int n, FILE *stream ); n is the maximum number of characters to read
printf("/nYou typed %s\n", string);

NOTE: To evaluate a string use stricmp() or strnicmp() detailed in the next section.

GETCH

#include <conio.h>

int c;

printf("Press a key...");
c = getch(); //getch() returns the integer value of the key that was pressed.
printf("/nThe integer value of the key you pressed is %i\n", c);

NOTE: The integer value of getch() can be evaluated like this:

if (c == 'Y') do_this_or_that_or_the_next_thing();

KBHIT

#include <conio.h>

printf("Press any key to continue..." );
//this just loops endlessly until a key is hit. Note the "!" operand before kbhit, because the loop continues while a key is not hit
while (!kbhit());
printf("\nYou pressed a key!");


SECTION 5 - STRING MANIPULATION

This is the last section for now. Maybe i'll do more later if the thought comes to me (and i have the time when it happens). I'm going to cover some of the most helpful string manipulation stuff. Pretty much vital for text games. Heck, what am i saying? They're vital for just about every application that you'll ever write.

STRICMP & STRNCMP

You can compare integer variables with the == operator, but you cant do the same thing with strings.

if (my_integer_numberone == my_integer_numbertwo) //is a perfectly legal piece of code
if (my_string_numberone == my_string_numbertwo) //is not.

In this situation we turn to the string-compare functions.

#include <string.h>

if ( stricmp(my_string, "what i want to check my string for") == 0 ) //then the my_string matches the text we supplied.

stricmp returns a value 0 if the strings match and non-zero if they do not. stricmp does not check case. If you want to check two strings with case sensitivity then use strcmp or strncmp respectivly. They work in the same manner.

strnicmp is also a string comparing function but it has an extra parameter allowing you to specify how many characters of the comparing string you want to check against the my_string variable.

if ( strnicmp(my_string, "this will only compare the first 4 characters of my_string against the first 4 of this", 4) == 0 )

STRLEN

strlen calculates the number of characters in the givin string.

#include <string.h>

char *my_string = "Lets see how many characters long this string is.";
printf("%i", strlen(my_string));

STRREV

strrev reverses all characters in the given string (except the terminating character) and returns a pointer to the reversed string.

#include <string.h>

char *my_string = "string";

printf("Before strrev(): %s\n", my_string);
strrev(my_string);
printf("After strrev(): %s\n" , my_string);

Oops i almost forgot STRCPY & STRCAT

There will be plenty of times when you need a string variable to, well, hold a string. So what you need is strcpy.

#include <string.h>

char my_string[80];

strcpy( string, "Here we have copied this to our string" );
printf( "%s", my_string );

Output:

Here we have copied this to our string

But what if we need to append some more stuff on the end of our string? strcat does that job.

char my_string[80];

strcpy( string, "Here we have copied this to our string" ); //we use strcpy first to get a string to add to...
strcat( string, " and added this" ); //then we tack something on the end
strcat( string, " and this." );
printf( "%s", my_string );

Output:

Here we have copied this to our string and added this and this.


THE END (FOR NOW AT LEAST)

lightatdawn@lycos.com

ÿ