C++ ile Oyun Hazırlamak

C başarılı ve kullanıcı dostu bir oyun yapmak için + +, aşağıdaki şeyleri hatırlamamız gerekiyor:
Her şeyden önce, basitlik anahtardır. Tabii ki, daha gelişmiş grafik özellikleri ile rahat C + + gibi Liero gibi karmaşık bir oyun yapmak için gidebilirsiniz, ama şimdi daha basit, daha iyi.
Ayrıca, doğru bir zorluk değil, çok sert, çok kolay değil bir oyun olması gerektiğini hatırlamamız gerekiyor. Bu ödül bir çeşit sahip olması da gerekir (örneğin renkli bir mesaj) siz kazanırsınız, bazı nedenlerden dolayı kullanıcı oynuyor böylece.
Bir oyun da düz metin biraz daha fazla olması gerekir. Örneğin, bir Noughts ve haçlar pansiyon, ya da sadece renkli yazı kullanabilirsiniz.

Bu kavramlar ile rahat olduğunuzda, aslında oyun yapmaya devam edebilirsiniz.

Renkli metin çıktısı ile aşina değilseniz, sana bu bir oyun yapmak için denemeden önce nasıl öğrenmek öneririz. Bu aslında çok kolaydır. Her şeyden önce, ana işlem (int main () {önce) başlamadan hemen önce, siz bu satırları eklemeniz gerekiyor:

void setcolor(unsigned short color)                 //The function that you'll use to
{                                                   //set the colour
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

sonra, metin çıktısı için komut önce komut ile renk:
setcolor (x) ((x) herhangi bir sayı ile örneğin setcolor varsayılan rengi, beyaz (7) ile ortaya çıkacak yerine.

Şimdi, oyun yapmak. Her şeyden önce, yapmak istediğiniz oyunun ne tür bir fikir olması gerekir.Öğreticinin amacı için, “Yükseköğretim Alt?” Adı verilen çok basit bir oyun yapalım.Kullanıcı sayısı gösterilir ve bir sonraki daha yüksek veya daha düşük olması durumunda istenecektir.

Her şeyden önce, biz değişkenleri bildirmek gerekir. Biz üç işaretsiz kısa tamsayı olmalıdır. Bu ilk sayı, ikinci sayı ve toplam skor olmalıdır. Bundan sonra da kullanıcı “H” ya da “L”, daha yüksek veya daha düşük girer mektup olacak bir karakteri olmalıdır. Bunun gibi, bu ilan edebilir:

int main()
{
         short unsigned int score = 0;
short unsigned int num = 0;
short unsigned int num2 = 0;
char letter;

Şimdi, tam outputted numaraları rastgele için, biz bir kaç satır kod eklemek gerekir. Yorum her satırın yaptığı açıklamaya Bunlar aşağıdaki gibidir.

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;

başlayabilirsiniz.

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 thinkn we can give you a point for that...";
                                                        system ("pause");
                                                        score++;
                                                        goto loop;}}
                                             lose:
                                                  {cout <<"You lose...n";
                                                    system ("pause");
}}                                                    return 0;}

bu bitmiş bir projedir:

#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 thinkn we can give you a point for that...";
                                                        system ("pause");
                                                        score++;
                                                        goto loop;}}
                                             lose:
                                                  {cout <<"You lose...n";
                                                    system ("pause");
}}                                                    return 0;}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir