搜索
熱搜: 活動 交友 discuz
查看: 1644|回復: 0
打印 上一主題 下一主題

[教學] C++寫小遊戲

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-7-27 20:47:43 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
為了使成功和易用遊戲在C++中,我們必須記住以下幾點 :
首先,簡單是關鍵 . 當然 ,如果你是舒適與更先進的圖形能力C++語言 你可以把一個複雜的遊戲如利羅 ,但就目前而言,愈簡單愈好. 同時 ,我們必須記住 ,一個遊戲已經是正確的難度不會太容易,不要太硬. 還需要有某種程度的報酬 (文理豐富多彩的信息)當你贏 所以用戶是玩一些理由. 遊戲還需要有一個多平原文本. 比如,你可以利用一個 noughts和十字架董事會 ,或者乾脆多彩文. 當你舒適 ,對這些概念,你可以在實際製作的遊戲 .當你舒適 ,對這些概念,你可以在實際製作的遊戲 . 如果你不熟悉輸出彩色文本 我建議你學習如何做到這一點 ,才去作遊戲 . 其實這是非常容易的. 首先,剛剛才開始你的主要過程前,你需要添加以下程式碼:
int main () {
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 “形, 更高或更低. 我們可以宣布這是這樣的:
int main()
{
         short unsigned int score = 0;
short unsigned int num = 0;
short unsigned int num2 = 0;
char letter;
現在 ,為了充分拖放的號碼是輸出 , 我們必須加入幾行代碼 . 這些措施如下,評論解釋其中每條生產線也.
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.
一旦我們做到了這一點 ,我們可以從網絡接口. 首先,在高層的程式在任何一個時代 ,我們應該有評分公映. 我們也希望能夠有一個快速解釋的遊戲 ,然後開始遊戲本身:
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;}
如果我們把這一切在一起,這是完成的項目:
#include
#include
#include
#include
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;}
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-29 08:10 , Processed in 0.276526 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表