【AI编程】——使用ChatGPT实现C++贪吃蛇

User
我想使用C++写一个贪吃蛇小游戏

以下是ChatGPT的回答

为了创建一个贪吃蛇小游戏,我们将遵循以下步骤来组织我们的计划:

  1. 初始化游戏环境

    • 设置游戏窗口大小。
    • 初始化蛇的起始位置。
    • 放置初始食物。
  2. 游戏循环逻辑

    • 处理用户输入。
    • 根据用户输入更新蛇的方向。
    • 移动蛇头,并且根据移动的结果更新蛇身。
    • 检查游戏结束条件(蛇碰到自己或游戏边界)。
    • 如果蛇头与食物位置重合,增长蛇身并重新生成食物。
  3. 渲染更新

    • 清除屏幕。
    • 绘制蛇和食物。
    • 更新得分和其他游戏信息。
  4. 结束游戏

    • 显示最终得分。
    • 提供重新开始或退出游戏的选项。

现在,我们将这个计划转化为C++代码:

初始化部分

首先,我们需要包含一些必要的头文件,并定义一些基本的枚举和结构体来表示游戏的状态。

#include <iostream>
#include <conio.h> // 用于_getch()来获取用户输入
#include <windows.h> // 用于Sleep()

// 定义游戏区域的大小
const int width = 20;
const int height = 20;

// 定义蛇的移动方向
enum eDirection {
    
     STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

// 定义蛇的结构
struct Snake {
    
    
    int x, y; // 蛇头的位置
    int tailX[100], tailY[100]; // 蛇尾的位置数组
    int nTail; // 蛇尾的长度
} snake;

// 定义食物的位置
struct Food {
    
    
    int x, y; // 食物的位置
} food;

bool gameOver; // 游戏是否结束
int score; // 游戏得分

游戏初始化函数

在游戏开始之前,我们需要初始化游戏的一些基本状态。

void Setup() {
    
    
    gameOver = false;
    dir = STOP;
    snake.x = width / 2;
    snake.y = height / 2;
    snake.nTail = 0; // 初始没有尾巴
    food.x = rand() % width; // 随机生成食物的位置
    food.y = 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 == snake.y && j == snake.x)
                std::cout << "O"; // 蛇头
            else if (i == food.y && j == food.x)
                std::cout << "F"; // 食物
            else {
    
    
                bool print = false;
                for (int k = 0; k < snake.nTail; k++) {
    
    
                    if (snake.tailX[k] == j && snake.tailY[k] == i) {
    
    
                        std::cout << "o"; // 蛇尾
                        print = true;
                    }
                }
                if (!print) 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 = snake.tailX[0];
    int prevY = snake.tailY[0];
    int prev2X, prev2Y;
    snake.tailX[0] = snake.x;
    snake.tailY[0] = snake.y;
    for (int i = 1; i < snake.nTail; i++) {
    
    
        prev2X = snake.tailX[i];
        prev2Y = snake.tailY[i];
        snake.tailX[i] = prevX;
        snake.tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }

    switch (dir) {
    
    
    case LEFT:
        snake.x--;
        break;
    case RIGHT:
        snake.x++;
        break;
    case UP:
        snake.y--;
        break;
    case DOWN:
        snake.y++;
        break;
    default:
        break; // 停止状态
    }

    // 检查游戏结束条件
    if (snake.x >= width || snake.x < 0 || snake.y >= height || snake.y < 0)
        gameOver = true;
    for (int i = 0; i < snake.nTail; i++)
        if (snake.tailX[i] == snake.x && snake.tailY[i] == snake.y)
            gameOver = true;

    // 检查蛇头是否与食物位置重合
    if (snake.x == food.x && snake.y == food.y) {
    
    
        score += 10;
        food.x = rand() % width;
        food.y = rand() % height;
        snake.nTail++;
    }
}

int main() {
    
    
    Setup();
    while (!gameOver) {
    
    
        Draw();
        Input();
        Logic();
        Sleep(10); // 控制游

戏速度
    }
    return 0;
}

以上代码提供了贪吃蛇游戏的一个基本实现。你可以根据需要调整参数,增加更多的特性,例如障碍物、不同级别的复杂性,甚至是更高级的图形库来提升游戏体验。

注意事项

  • 请确保在支持conio.hwindows.h的环境中编译和运行此代码,例如在Windows系统的命令行或者使用支持这些库的IDE。
  • 由于使用了system("cls")来清屏,这可能会导致游戏画面闪烁,特别是在游戏速度较快时。为了获得更流畅的体验,你可能需要探索使用更高级的图形库。

如果你有任何具体问题或需要进一步的帮助,请随时告诉我!

复制到Visual Studio

在这里插入图片描述

运行

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39811006/article/details/136199127