Making Games in C++

In order to make a successful and user- friendly game in C++, we need to remember the following things:
First of all, simplicity is the key. Of course, if you are comfortable with the more advanced graphics capabilities of C++, you can go on to make a complex game such as Liero, but for now, the simpler the better.
Also, we need to remember that a game has to be the right difficulty- not too easy, not too hard. It needs also to have some sort of reward (e.g a colorful message) when you win, so the user is playing for some reason.
A game also needs to have a little more than plain text. For example, you could use a noughts and crosses board, or simply colorful text.

When you are comfortable with these concepts, you can go on to actually making the game.

If you are not familiar with outputting colored text, I suggest you learn how to do this before trying to make a game. It is actually very easy. First of all, just before you start the main process (before the int main () {), you need to add these lines:
1
2
3
4
5
void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}


then, before the command to output the text, you set the color with the command:
setcolor (x);
(replace the (x) with any number e.g. setcolor (7) will come out with the default color, white.

Now, on to making the game. First of all, you need to have an idea of what kind of game you want to make. For the purpose of the tutorial, let us make a very simple game, called "Higher or Lower?". The user will be shown a number and asked if the next will be higher or lower.

First of all, we need to declare our variables. We should have three unsigned short integers. These should be the first number, the second number, and the overall score. We should then also have a character, which will be the letter the user enters, "H" or "L", higher or lower. We can declare these like this:
1
2
3
4
5
6
int main()
{
         short unsigned int score = 0;
short unsigned int num = 0;
short unsigned int num2 = 0;
char letter;


Now, in order to fully randomize the numbers which are outputted, we need to add in a few lines of code. These are as follows, with comments to explain which each line does.

1
2
3
4
loop:  //This labels the code for quick reference later on.
srand(time(NULL));  //Initialize random number generator
    num = 1 + rand() % (6 - 1 + 1);  //Shows that num is a random integer between 1 and 6.
    num2 = 1 + rand() % (6 - 1 + 1); //Shows that num2 is a random integer between 1 and 6. 

Once we have done this, we can start with the interface.
First of all, at the top of the program at all times, we should have the score being shown. We also want to have a quick explanation of the game, and then start the game itself:
1
2
3
4
5
6
7
8
9
cout <<"\nPoints: ";
    setcolor (10);
    cout << score << endl;
    setcolor (7);
    cout <<"Get to 5 points to win. The numbers range between 1 and 6.\n";
    setcolor (12);
    cout << num;
    setcolor (7);
    cout <<" is the first number. \nIs the next number going to be higher or lower? H or L?" << endl;

Then we can start the input sequence.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
cin >> letter;
    if (letter == 'h'||letter == 'H')
    {
               setcolor (12);
    cout << num2;
    setcolor (7);
    cout <<" is the second number.";
    if (num2>num) goto win;
                 else if (num2<num) goto lose;
                 else if (num2==num) goto same;
                 }
                      else
                      {
                          setcolor (12);
                           cout << num2;
                           setcolor (7);
                           cout <<" is the second number.";
                           if (num2<num) goto win;
                                        else if (num2>num) goto lose;
                                        else if (num2==num) goto same;
                                        win:
                                            {
                                                if (score==4)
                                                {
                                                             setcolor (12);
                                                             cout <<" You completed the game! Well done!!!\n";
                                                system ("pause");
                                                return 0;
                                                }
                                                else
                                                {cout <<"You win! Well done!\n";
                                             system ("pause");
                                             score++;
                                             goto loop;}
                                             }
                                             same:
                                                  {if (score==4)
                                                {
                                                             setcolor (10);
                                                             cout <<" You completed the game! Well done!!!\n";
                                                system ("pause");
                                                return 0;}
                                                        else
                                                        {cout <<"The numbers were the same! What a coincidence! I think\n we can give you a point for that...";
                                                        system ("pause");
                                                        score++;
                                                        goto loop;}}
                                             lose:
                                                  {cout <<"You lose...\n";
                                                    system ("pause");
}}                                                    return 0;}

If we put this all together, this is the finished project:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}
int main()
{
         short unsigned int score = 0;
short unsigned int num = 0;
short unsigned int num2 = 0;
char letter;
loop:
srand(time(NULL));
//Initialize random number generator
    num = 1 + rand() % (6 - 1 + 1);
    num2 = 1 + rand() % (6 - 1 + 1);
    cout <<"\nPoints: ";
    setcolor (10);
    cout << score << endl;
    setcolor (7);
    cout <<"Get to 5 points to win. The numbers range between 1 and 6.\n";
    setcolor (12);
    cout << num;
    setcolor (7);
    cout <<" is the first number. \nIs the next number going to be higher or lower? H or L?" << endl;
    cin >> letter;
    if (letter == 'h'||letter == 'H')
    {
               setcolor (12);
    cout << num2;
    setcolor (7);
    cout <<" is the second number.";
    if (num2>num) goto win;
                 else if (num2<num) goto lose;
                 else if (num2==num) goto same;
                 }
                      else
                      {
                          setcolor (12);
                           cout << num2;
                           setcolor (7);
                           cout <<" is the second number.";
                           if (num2<num) goto win;
                                        else if (num2>num) goto lose;
                                        else if (num2==num) goto same;
                                        win:
                                            {
                                                if (score==4)
                                                {
                                                             setcolor (12);
                                                             cout <<" You completed the game! Well done!!!\n";
                                                system ("pause");
                                                return 0;
                                                }
                                                else
                                                {cout <<"You win! Well done!\n";
                                             system ("pause");
                                             score++;
                                             goto loop;}
                                             }
                                             same:
                                                  {if (score==4)
                                                {
                                                             setcolor (10);
                                                             cout <<" You completed the game! Well done!!!\n";
                                                system ("pause");
                                                return 0;}
                                                        else
                                                        {cout <<"The numbers were the same! What a coincidence! I think\n we can give you a point for that...";
                                                        system ("pause");
                                                        score++;
                                                        goto loop;}}
                                             lose:
                                                  {cout <<"You lose...\n";
                                                    system ("pause");
}}                                                    return 0;}
good job!
Good game but I had trouble following what the code did until I ran it. I think it would be more clear if you got rid of all of your goto statements and lined up your brackets.

For Example:

Instead of:
1
2
3
4
if (num2>num) ...;
                 else if (num2<num) ...;
                 else if (num2==num) ...;
                 }

I think this makes it easier to read.
1
2
3
4
5
6
7
8
9
10
11
12
if ( num2 > num )
{
  ...;
}
else if ( num2 < num )
{
   ...;
}
else if ( num2 == num )
{
   ...;
}

Another thing I notice is that with multiple return statements its hard to follow where the program is being exited at. Try to have one return statement if possible. Sometimes using multiple return statements is more readable but that is another story.

Try to reduce redundent code.

Example - instead of this
1
2
3
4
5
6
7
8
9
    setcolor (10);
    cout << score << endl;
    setcolor (7);

    cout <<"Get to 5 points to win. The numbers range between 1 and 6.\n";

    setcolor (12);
    cout << num;
    setcolor (7);

maybe this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printText(unsigned short textColor, string text, unsigned short textColorAfter)
{
     setColor( textColor );
     cout << text;
     setColor( textColorAfter );     
}

....

	string num, score;

	printText( 10, score, 7);

        cout <<"Get to 5 points to win. The numbers range between 1 and 6.\n";

	printText( 10, num, 7);      

Now your only writing one line a of code every time you want to change your color.

I believe by doing these few things it will make your code more readable and writable. Which will make you an even a better programmer.

I did a quick refracting of your code and this is what I got.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <cctype>

using namespace std;

//Used to set Text Color
void setColor(unsigned short color);
//Print text in text color and sets the text color to textColotAfter when done
void printText(unsigned short textColor, string text, unsigned short textColorAfter = 7);

bool playHiLoGame( void );
void printGame( int firstNumber );
bool getResult( int firstNumber, int secondNumber, char yourGuess );

string intToString( int number);

int points = 0;

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    bool hasWon = false;
    
    int round = 1;
    for( round = 1; round <= 5; round++)
    {
         hasWon = playHiLoGame();

         if( hasWon == false)
         {
             cout <<"You lose...\n";  
             round = 6;
         }
         else if( round == 5)
         {
            printText( 12, " You completed the game! Well done!!!\n");  
         }
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



void setColor(unsigned short color)
{                                  
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

void printText(unsigned short textColor, string text, unsigned short textColorAfter)
{
     setColor( textColor );
     cout << text;
     setColor( textColorAfter );     
}


bool playHiLoGame( void )
{
    bool youGuessedRight = false;
    //generate random numbers
    int firstNumber = (rand() % 6) + 1;
    int secondNumber = (rand() % 6) + 1;
    
    printGame( firstNumber );
    
    //make sure input is either 'h' or 'l'
    char letter = ' ';
    while( letter != 'h' && letter != 'l')
    {
       cin >> letter;    
       letter = tolower(letter);
    }
    
    youGuessedRight = getResult( firstNumber, secondNumber, letter );
     
    return youGuessedRight;
}


void printGame( int firstNumber )
{ 
//   char pointsString[5] = points + "\n";  
   cout << "Points: ";
   printText(10,  intToString( points)+ "\n" ); //points
   cout << "Get to 5 points to win. The numbers range between 1 and 6." << endl;
   printText(12, intToString( firstNumber )); 
   cout << " is the first number." << endl;
   cout << "Is the next number going to be higher or lower? H or L?" << endl;
}

bool getResult( int firstNumber, int secondNumber, char yourGuess )
{
     bool hasWon = false;
     
     printText(12, intToString( secondNumber ) );
     cout << " is the second number." << endl;      
     
     if( yourGuess == 'h' && secondNumber >= firstNumber ||
         yourGuess == 'l' && secondNumber <= firstNumber )
     {
        cout << " You win! Well done!" << endl;
        points++;
        hasWon = true;
     }

     system ("pause");
     cout << "\n";
     return hasWon;
}


string intToString( int number)
{
       char buffer[20];
       sprintf(buffer, "%d", number);
       return buffer;
}
Last edited on
it is nice thanx but it is nearly a C code. Classes are important in programming games in C++.
Topic archived. No new replies allowed.