為了用 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;}
|