c++小游戏

· · 休闲·娱乐

//                oldnet           2024/8/15-11:30:00
//                原创         引用请在洛谷@oldnet
//                       冒险棋
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
void printpt() {
    string s[1010] = {
        "/--------------------------------------------------------------------------------------------------------------------------------------------|",
        "|                                                                                                                                            |",
        "|                                                                                                                                            |",
        "|                                                                                                                       ---                  |",
        "|                                      --                                                              -                -----                |",
        "|                                ----------                                ----                        -----            ------               |",
        "|                --     --------------------                               -----                       ------            -----               |",
        "|                ----------------------------                    ----      -----                        -----    -       -----               |",
        "|                ----------------      ------              --   ------     -----                        ----    -----    -----               |",
        "|                 -------      -----   -----              -------------    ----                         ----     -----   ----                |",
        "|                 ----    ----------   ----               ------------    -----                         ----     -----   ----                |",
        "|                 ---- -------------   ----                -----------    ------                        ----      ---    ---- --             |",
        "|                 ----  -------       -----                ----  ----    --------                       ----      ---    ---------           |",
        "|                  ---                -----                ---- ----     ---- ----                      ----      ----------------           |",
        "|                  ----          ---  ----                 ---- ---     -----  -----                    -------------------------            |",
        "|                  ----      -------- ----                 ---- ---     ----    -----                ------------------------                |",
        "|                  ---- ------------- ----                 ---- ---    ----      -----           ----------------------- ----                |",
        "|                  ------------------ ---                  ----  ---  -----    ---------         -----------      ---  - ----                |",
        "|                  -----------     -  --                   ----  ---  ---- ---------------        ---------       -----------                |",
        "|                   --          -------                    --------- ------------------------          ----       -----------                |",
        "|                     --  --------------                   ----------------------   ----------        -----       ------ ----                |",
        "|                     ------------------                   -------------   --   ---  ---------        ---------   ---    ----                |",
        "|                     --------     ----                    ---- -------         ----    --           -----------  -----------                |",
        "|                      ---      -   ---                    ----   ---    ---    -----                -----------  -----------                |",
        "|                     ------------ ---                    ----  --- --   ---   -----               ------- ----  -----------                 |",
        "|                       ------------ ---                    ----      ---  ----  ----               -------       ---    ----    ---         |",
        "|                      ---------    ---                    ----      ---- ----  ----               ---- ---       ---    -------------       |",
        "|                      ----     --  ---                    ----      ----- --------               ----  ---       --------------------       |",
        "|                      ----------------                    ----       ---- --- ----              ----   --- --------------------------       |",
        "|                      ----------------                    ----       ----  -- ---               ---    ----------------                     |",
        "|                      ----------   ---                   -----        ---    ----              ---     ---------- ---    --                 |",
        "|                      ----         ----                  -----         -     ---               --      ----      -----  ------              |",
        "|                      ----   ----- ----                  ----               ----------                 ----      -----   ------             |",
        "|                      -----------------                  ----    ----------------------               -----     -----     -------           |",
        "|                    ------------------                  ----    -----------------------              -----     ----       ------            |",
        "|                      -------    -----                   ----    -----------------------              -----    ----         ------          |",
        "|                      ---        -----                    ---     -----                               ----    ----           -----          |",
        "|                       --         ----                    --                                          ----   ----             ----          |",
        "|                                  ---                                                                  ---  ----               --           |",
        "|                                   -                               按c开始游戏                              --                              |",
        "|--------------------------------------------------------------------------------------------------------------------------------------------/"
    };
    for (int i = 0; i < 41; i++) {
        cout << s[i] << endl;
    }
};
void COLOR_PRINT(string s, int color) {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
    cout << s;
    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
int rnd() {
    return rand() * rand();
}
void HideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void print1(string s, int x) {
    for (size_t i = 0; i < s.size(); i++) {
        cout << s[i];
        Sleep(x);
    }
}
void print2(string s, int x) {
    for (size_t i = 0; i < s.size(); i += 2) {
        cout << s[i] << s[i + 1];
        Sleep(x);
    }
}
void game() {
E1:
    HideCursor();
    {
        printpt();
        char c;
        while (1) {
            c = getch();
            if (c == 'c')break;
        }
        system("cls");
    }
    string wja, wjb;
    print1("请输入玩家名(2):", 100);
    cin >> wja >> wjb;
    print1("输入成功!", 100);
    cout << endl;
    system("pause");
    system("cls");


    int suma = 1, sumb = 1;
    bool bsja = 0, bsjb = 0;
    bool flag = rand() % 2 == 0 ? 1 : 0;
    for (int i = 1; 1; i++) {
        HideCursor();
        char c;
        c = getch();
        if (c == 'c') {
            COLOR_PRINT(wja, 12);
            COLOR_PRINT("在", 12);
            COLOR_PRINT(to_string(suma), 12);
            COLOR_PRINT("格", 12);
            cout << endl;
            COLOR_PRINT(wjb, 9);
            COLOR_PRINT("在", 9);
            COLOR_PRINT(to_string(sumb), 9);
            COLOR_PRINT("格", 9);
            cout << endl;
            i--;
            continue;
        }
        if (c == 't') continue;
        if (c == 'r') goto E1;
        if (bsja && i % 2 == flag) {
            bsja = 0;
            continue;
        }
        if (bsjb && i % 2 != flag) {
            bsjb = 0;
            continue;
        }
        if (i % 2 == flag) {
            COLOR_PRINT(wja, 12);
            COLOR_PRINT("请走", 12);
            int rd = rnd() % 10 + 1;
            COLOR_PRINT(to_string(rd), 12);
            COLOR_PRINT("步", 12);
            suma += rd;
            cout << endl;
            if (suma == sumb) {
                print2("呵呵呵,玩家", 500);
                print2(wjb, 500);
                print2("被打回起点:)\n", 500);
                sumb = 1;
            }
            if (suma >= 100) {
                if (suma == 100) {
                    COLOR_PRINT(wja, 12);
                    COLOR_PRINT("获胜!!!", 12);
                    cout << endl;
                    system("pause");
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    print2("游戏初始话成功!\n", 500);
                    system("pause");
                    system("cls");
                    goto E1;
                }
                suma -= (suma - 100) + 1;
            }
            if (suma == 7) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("爬梯子到了", 12);
                COLOR_PRINT("19", 12);
                cout << endl;
                suma = 19;
            }
            if (suma == 29) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("爬梯子到了", 12);
                COLOR_PRINT("42", 12);
                cout << endl;
                suma = 42;
            }
            if (suma == 57) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("爬梯子到了", 12);
                COLOR_PRINT("69", 12);
                cout << endl;
                suma = 69;
            }
            if (suma == 84) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("爬梯子到了", 12);
                COLOR_PRINT("94", 12);
                cout << endl;
                suma = 94;
            }
            if (suma == 11) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到长颈鹿,前进3格!", 12);
                suma += 3;
                cout << endl;
            }
            if (suma == 13 || suma == 26 || suma == 87 || suma == 95) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到捕兽夹暂停一会合", 12);
                bsja = 1;
                cout << endl;
                continue;
            }
            if (suma == 17) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到奶牛前进5格", 12);
                suma += 5;
                cout << endl;
            }
            if (suma == 47) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到海盗后退6格", 12);
                suma -= 6;
                cout << endl;
            }
            if (suma == 56) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到老虎后退8格", 12);
                suma -= 8;
                cout << endl;
            }
            if (suma == 91) {
                COLOR_PRINT(wja, 12);
                COLOR_PRINT("遇到海盗后退3格", 12);
                suma -= 3;
                cout << endl;
            }
            if (suma == 83) {
                COLOR_PRINT(wja, 9);
                COLOR_PRINT("遇到大象后退2格", 9);
                suma -= 2;
                cout << endl;
            }
        } else {
            COLOR_PRINT(wjb, 9);
            COLOR_PRINT("请走", 9);
            int rd = rnd() % 10 + 1;
            COLOR_PRINT(to_string(rd), 9);
            COLOR_PRINT("步", 9);
            sumb += rd;
            cout << endl;
            if (suma == sumb) {
                print2("呵呵呵,玩家", 500);
                print2(wja, 500);
                print2("被打回起点:)\n", 500);
                suma = 1;
            }
            if (sumb >= 100) {
                if (sumb == 100) {
                    COLOR_PRINT(wjb, 9);
                    COLOR_PRINT("获胜!!!", 9);
                    cout << endl;
                    system("pause");
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    cout << "游戏初始话中";
                    print2("。。。", 500);
                    system("cls");
                    print2("游戏初始话成功!\n", 500);
                    system("pause");
                    system("cls");
                    goto E1;
                }
                sumb -= (sumb - 100) + 1;
            }
            if (sumb == 7) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("爬梯子到了", 9);
                COLOR_PRINT("19", 9);
                cout << endl;
                sumb = 19;
            }
            if (sumb == 29) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("爬梯子到了", 9);
                COLOR_PRINT("42", 9);
                cout << endl;
                sumb = 42;
            }
            if (sumb == 57) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("爬梯子到了", 9);
                COLOR_PRINT("69", 9);
                cout << endl;
                sumb = 69;
            }
            if (sumb == 84) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("爬梯子到了", 9);
                COLOR_PRINT("94", 9);
                cout << endl;
                sumb = 94;
            }
            if (sumb == 11) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到长颈鹿,前进3格!", 9);
                suma += 3;
                cout << endl;
            }
            if (sumb == 13 || sumb == 26 || sumb == 87 || sumb == 95) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到捕兽夹暂停一会合", 9);
                bsjb = 1;
                cout << endl;
                continue;
            }
            if (sumb == 17) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到奶牛前进5格", 9);
                sumb += 5;
                cout << endl;
            }
            if (sumb == 47) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到海盗后退6格", 9);
                sumb -= 6;
                cout << endl;
            }
            if (sumb == 56) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到老虎后退8格", 9);
                sumb -= 8;
                cout << endl;
            }
            if (sumb == 91) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到海盗后退3格", 9);
                sumb -= 3;
                cout << endl;
            }
            if (sumb == 83) {
                COLOR_PRINT(wjb, 9);
                COLOR_PRINT("遇到大象后退2格", 9);
                sumb -= 2;
                cout << endl;
            }
        }
    }
}


int main() {
    HideCursor();
    srand(time(0));
    game();
    return 0;
}

如有扫到存在病毒,请忽略,因为使用了conio.h。我只用了getch()函数所以放心运行

编写不易,如有意见请私信,点个赞再走吧,关注也行qwq