发布
2008年1月14日

用 C++ 制作游戏

评分:3.6/5 (182 票)
*****
为了用 C++ 制作一个成功且用户友好的游戏,我们需要记住以下几点。
首先,简单是关键。当然,如果您熟悉 C++ 更高级的图形功能,您可以继续制作像 Liero 这样的复杂游戏,但现在,越简单越好。
此外,我们需要记住,游戏必须具有恰当的难度——不太容易,也不太难。当玩家获胜时,游戏还需要某种形式的奖励(例如,一条多彩的消息),这样用户才有玩下去的理由。
游戏也需要比纯文本多一点东西。例如,您可以使用井字棋棋盘,或者简单地使用彩色文本。

当您熟悉了这些概念之后,就可以开始实际制作游戏了。

如果您不熟悉输出彩色文本,我建议您在尝试制作游戏之前先学会如何做。这其实非常容易。首先,就在您开始主进程之前(在 int main () { 之前),您需要添加以下几行。
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);
}


然后,在输出文本的命令之前,您需要使用以下命令设置颜色:
setcolor (x); (将 (x) 替换为任何数字,例如 setcolor (7) 将输出默认颜色,白色。

现在,开始制作游戏。首先,您需要对您想制作的游戏类型有一个想法。在本教程中,让我们制作一个非常简单的游戏,叫做“猜高猜低?”。用户将看到一个数字,并被问及下一个数字是会比它高还是低。

首先,我们需要声明我们的变量。我们应该有三个无符号短整型。它们分别是第一个数字、第二个数字以及总得分。然后我们还需要一个字符,它将是用户输入的字母,“H”或“L”,高或低。我们可以这样声明它们:
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;


现在,为了完全随机化输出的数字,我们需要添加几行代码。如下所示,并附带注释解释每一行的作用。

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. 

完成这些之后,我们就可以开始着手界面了。
首先,在程序的顶部,我们应该一直显示得分。我们还想对游戏做一个简短的说明,然后开始游戏本身。
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;

然后我们可以开始输入序列。
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;}


如果我们把所有这些组合起来,这就是完成的项目。


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;}