c加加编程小游戏代码是什么

fiy 其他 112

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    C++编程语言是一种强大的语言,可以用来开发各种类型的应用程序,包括小游戏。下面是一个简单的C++小游戏代码示例,供参考:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() {
        srand(time(0)); // 设置随机数种子
    
        int targetNumber = rand() % 100 + 1; // 生成1到100之间的随机数
        int guess;
        int attempts = 0;
        bool correct = false;
    
        cout << "欢迎来到猜数字游戏!" << endl;
    
        while (!correct) {
            cout << "请输入一个1到100之间的整数: ";
            cin >> guess;
            attempts++;
    
            if (guess == targetNumber) {
                correct = true;
            } else if (guess < targetNumber) {
                cout << "猜小了!" << endl;
            } else {
                cout << "猜大了!" << endl;
            }
        }
    
        cout << "恭喜你猜对了!" << endl;
        cout << "你猜了" << attempts << "次。" << endl;
    
        return 0;
    }
    

    这个小游戏是一个猜数字的游戏。程序会生成一个1到100之间的随机数,然后玩家需要通过输入猜测的数字来猜对这个随机数。程序会根据玩家的猜测给出相应的提示,直到玩家猜对为止。最后,程序会显示玩家猜对的次数。

    这只是一个简单的示例,你可以根据自己的需求进行修改和扩展,添加更多的游戏规则和功能。希望对你有所帮助!

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    C++编程语言是一种通用的、高级的编程语言,非常适合用于开发各种类型的小游戏。以下是一个简单的C++编程小游戏的示例代码,用于帮助你入门:

    1. 猜数字游戏
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main() {
        srand(time(0)); // 用当前时间初始化随机种子
        int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
        int guess;
        int tries = 0;
    
        std::cout << "欢迎来到猜数字游戏!\n\n";
    
        do {
            std::cout << "猜一个1到100之间的数字:";
            std::cin >> guess;
            tries++;
    
            if (guess > secretNumber) {
                std::cout << "猜大了!\n\n";
            } else if (guess < secretNumber) {
                std::cout << "猜小了!\n\n";
            } else {
                std::cout << "恭喜你,猜对了!\n";
                std::cout << "你用了" << tries << "次猜中。\n";
            }
        } while (guess != secretNumber);
    
        return 0;
    }
    
    1. 简单的文字冒险游戏
    #include <iostream>
    #include <string>
    
    int main() {
        std::string playerName;
        int playerHealth = 100;
        int playerGold = 0;
    
        std::cout << "欢迎来到文字冒险游戏!\n\n";
        std::cout << "请输入你的角色名:";
        std::cin >> playerName;
    
        std::cout << "\n欢迎," << playerName << "!\n";
        std::cout << "你的初始生命值为" << playerHealth << ",金币数量为" << playerGold << "。\n\n";
    
        std::cout << "你来到了一个神秘的森林,你决定:\n";
        std::cout << "1. 探索森林\n";
        std::cout << "2. 离开森林\n";
    
        int choice;
        std::cin >> choice;
    
        if (choice == 1) {
            std::cout << "\n你遇到了一只恶龙!你被吃掉了,游戏结束。\n";
        } else if (choice == 2) {
            std::cout << "\n你成功离开了森林,游戏结束。\n";
        } else {
            std::cout << "\n无效的选择,游戏结束。\n";
        }
    
        return 0;
    }
    
    1. 简单的井字棋游戏
    #include <iostream>
    #include <vector>
    #include <string>
    
    std::vector<std::vector<char>> board(3, std::vector<char>(3, ' '));
    char currentPlayer = 'X';
    
    void drawBoard() {
        std::cout << "  1 2 3\n";
        for (int i = 0; i < 3; i++) {
            std::cout << i + 1 << " ";
            for (int j = 0; j < 3; j++) {
                std::cout << board[i][j] << " ";
            }
            std::cout << "\n";
        }
    }
    
    bool makeMove(int row, int col) {
        if (row < 1 || row > 3 || col < 1 || col > 3 || board[row - 1][col - 1] != ' ') {
            return false;
        }
    
        board[row - 1][col - 1] = currentPlayer;
        return true;
    }
    
    bool checkWin() {
        // 检查行
        for (int i = 0; i < 3; i++) {
            if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][0] == board[i][2]) {
                return true;
            }
        }
    
        // 检查列
        for (int j = 0; j < 3; j++) {
            if (board[0][j] != ' ' && board[0][j] == board[1][j] && board[0][j] == board[2][j]) {
                return true;
            }
        }
    
        // 检查对角线
        if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
            return true;
        }
    
        if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
            return true;
        }
    
        return false;
    }
    
    int main() {
        std::cout << "欢迎来到井字棋游戏!\n\n";
        std::cout << "玩家1使用X,玩家2使用O。\n\n";
    
        int row, col;
        bool validMove;
        bool gameover = false;
    
        while (!gameover) {
            drawBoard();
            validMove = false;
    
            while (!validMove) {
                std::cout << "玩家" << currentPlayer << ",请下棋(行 列):";
                std::cin >> row >> col;
    
                validMove = makeMove(row, col);
    
                if (!validMove) {
                    std::cout << "无效的移动,请重试。\n";
                }
            }
    
            if (checkWin()) {
                drawBoard();
                std::cout << "玩家" << currentPlayer << "获胜!\n";
                gameover = true;
            } else if (currentPlayer == 'X') {
                currentPlayer = 'O';
            } else {
                currentPlayer = 'X';
            }
        }
    
        return 0;
    }
    
    1. 简单的飞机大战游戏
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    bool gameover;
    const int width = 20;
    const int height = 20;
    int x, y;
    int fruitX, fruitY;
    int score;
    int tailX[100], tailY[100];
    int nTail;
    enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
    eDirection dir;
    
    void Setup() {
        gameover = false;
        dir = STOP;
        x = width / 2;
        y = height / 2;
        fruitX = rand() % width;
        fruitY = rand() % height;
        score = 0;
    }
    
    void Draw() {
        system("cls");
        for (int i = 0; i < width + 2; i++)
            std::cout << "#";
        std::cout << std::endl;
    
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (j == 0)
                    std::cout << "#";
                if (i == y && j == x)
                    std::cout << "O";
                else if (i == fruitY && j == fruitX)
                    std::cout << "F";
                else {
                    bool printTail = false;
                    for (int k = 0; k < nTail; k++) {
                        if (tailX[k] == j && tailY[k] == i) {
                            std::cout << "o";
                            printTail = true;
                        }
                    }
                    if (!printTail)
                        std::cout << " ";
                }
                if (j == width - 1)
                    std::cout << "#";
            }
            std::cout << std::endl;
        }
    
        for (int i = 0; i < width + 2; i++)
            std::cout << "#";
        std::cout << std::endl;
        std::cout << "Score:" << score << std::endl;
    }
    
    void Input() {
        if (_kbhit()) {
            switch (_getch()) {
                case 'a':
                    dir = LEFT;
                    break;
                case 'd':
                    dir = RIGHT;
                    break;
                case 'w':
                    dir = UP;
                    break;
                case 's':
                    dir = DOWN;
                    break;
                case 'x':
                    gameover = true;
                    break;
            }
        }
    }
    
    void Logic() {
        int prevX = tailX[0];
        int prevY = tailY[0];
        int prev2X, prev2Y;
        tailX[0] = x;
        tailY[0] = y;
        for (int i = 1; i < nTail; i++) {
            prev2X = tailX[i];
            prev2Y = tailY[i];
            tailX[i] = prevX;
            tailY[i] = prevY;
            prevX = prev2X;
            prevY = prev2Y;
        }
        switch (dir) {
            case LEFT:
                x--;
                break;
            case RIGHT:
                x++;
                break;
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            default:
                break;
        }
    
        if (x >= width)
            x = 0;
        else if (x < 0)
            x = width - 1;
    
        if (y >= height)
            y = 0;
        else if (y < 0)
            y = height - 1;
    
        for (int i = 0; i < nTail; i++) {
            if (tailX[i] == x && tailY[i] == y) {
                gameover = true;
            }
        }
    
        if (x == fruitX && y == fruitY) {
            score += 10;
            fruitX = rand() % width;
            fruitY = rand() % height;
            nTail++;
        }
    }
    
    int main() {
        Setup();
        while (!gameover) {
            Draw();
            Input();
            Logic();
            Sleep(10);
        }
        return 0;
    }
    
    1. 简单的弹球游戏
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    bool gameover;
    const int width = 40;
    const int height = 20;
    int x, y;
    int ballX, ballY;
    int ballDirX, ballDirY;
    int paddleX, paddleY;
    int score;
    
    void Setup() {
        gameover = false;
        x = width / 2;
        y = height - 1;
        ballX = width / 2;
        ballY = height - 2;
        ballDirX = -1;
        ballDirY = -1;
        paddleX = width / 2 - 3;
        paddleY = height - 1;
        score = 0;
    }
    
    void Draw() {
        system("cls");
        for (int i = 0; i < width + 2; i++)
            std::cout << "#";
        std::cout << std::endl;
    
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (j == 0)
                    std::cout << "#";
                if (i == ballY && j == ballX)
                    std::cout << "O";
                else if (i == paddleY && j >= paddleX && j < paddleX + 6)
                    std::cout << "=";
                else
                    std::cout << " ";
                if (j == width - 1)
                    std::cout << "#";
            }
            std::cout << std::endl;
        }
    
        for (int i = 0; i < width + 2; i++)
            std::cout << "#";
        std::cout << std::endl;
        std::cout << "Score:" << score << std::endl;
    }
    
    void Input() {
        if (_kbhit()) {
            switch (_getch()) {
                case 'a':
                    paddleX--;
                    break;
                case 'd':
                    paddleX++;
                    break;
                case 'x':
                    gameover = true;
                    break;
            }
        }
    }
    
    void Logic() {
        ballX += ballDirX;
        ballY += ballDirY;
    
        if (ballX >= width - 1 || ballX <= 0)
            ballDirX = -ballDirX;
        if (ballY <= 0)
            ballDirY = -ballDirY;
        if (ballY >= height - 1) {
            if (ballX >= paddleX && ballX < paddleX + 6) {
                score += 10;
                ballDirY = -ballDirY;
            } else {
                gameover = true;
            }
        }
    }
    
    int main() {
        Setup();
        while (!gameover) {
            Draw();
            Input();
            Logic();
            Sleep(10);
        }
        return 0;
    }
    

    以上是一些简单的C++编程小游戏的示例代码,希望对你有所帮助!你可以根据自己的兴趣和需求对这些代码进行修改和扩展,创造出更有趣的小游戏。

    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    C++编程可以实现各种小游戏,下面以一个猜数字的小游戏为例来讲解。这个小游戏的规则是:计算机随机生成一个1到100之间的数字,玩家通过猜测数字来找出正确的答案。每次猜测后,计算机会告诉玩家猜测的数字是偏大还是偏小,直到玩家猜中为止。

    下面是猜数字小游戏的C++代码:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main() {
        // 生成随机数种子
        srand(time(0));
    
        // 生成1到100之间的随机数
        int target = rand() % 100 + 1;
    
        // 玩家猜测的数字
        int guess;
    
        // 猜测次数
        int attempts = 0;
    
        cout << "猜数字游戏开始!" << endl;
    
        do {
            cout << "请输入你猜测的数字:";
            cin >> guess;
    
            attempts++;
    
            if (guess > target) {
                cout << "猜测的数字偏大!" << endl;
            } else if (guess < target) {
                cout << "猜测的数字偏小!" << endl;
            } else {
                cout << "恭喜你猜对了!" << endl;
                cout << "你猜测了" << attempts << "次。" << endl;
            }
        } while (guess != target);
    
        return 0;
    }
    

    上面的代码使用了C++的基本语法和库函数来实现猜数字的小游戏。主要的步骤如下:

    1. 引入需要的头文件:iostream用于输入输出操作,cstdlibctime用于生成随机数。
    2. 使用using namespace std;来避免重复输入std::
    3. 使用srand(time(0));来设置随机数种子,确保每次运行程序生成的随机数不同。
    4. 使用rand() % 100 + 1;生成1到100之间的随机数。
    5. 使用cin来获取玩家的猜测数字。
    6. 使用if-else语句判断玩家猜测的数字与目标数字的大小关系,并给出相应的提示。
    7. 使用do-while循环,直到玩家猜中目标数字。
    8. 输出猜测次数。

    以上就是一个简单的猜数字小游戏的C++代码示例。你可以根据需要进行修改和扩展,实现更多有趣的小游戏。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部