#include #include #include #include #include <windows.h> #include <conio.h>

using namespace std;

// 全局常量 const int WIDTH = 30; // 游戏区宽度 const int HEIGHT = 20; // 游戏区高度

// 方向枚举 enum Dir { STOP = 0, LEFT, RIGHT, UP, DOWN };

// 蛇身体坐标结构体 struct Point { int x, y; Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} };

// 全局变量 vector snake; // 蛇身体 Point food; // 食物坐标 Dir dir; // 当前移动方向 int score; // 得分 bool gameOver; // 游戏结束标记

// 函数声明 void Setup(); // 初始化游戏 void Draw(); // 绘制画面 void Input(); // 键盘输入处理 void Logic(); // 游戏逻辑更新 void HideCursor(); // 隐藏控制台光标

int main() { HideCursor(); Setup(); // 游戏主循环 while (!gameOver) { Draw(); Input(); Logic(); Sleep(80); // 控制游戏速度,数值越小越快 } cout << "\n游戏结束!最终得分:" << score << endl; return 0; }

// 隐藏闪烁光标 void HideCursor() { CONSOLE_CURSOR_INFO cursor; cursor.dwSize = 1; cursor.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); }

// 初始化游戏数据 void Setup() { gameOver = false; dir = RIGHT; score = 0; snake.clear(); // 修复:使用push_back代替emplace_back snake.push_back(Point(WIDTH / 2, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 1, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 2, HEIGHT / 2));

srand((unsigned)time(NULL));
// 随机生成食物
food.x = rand() % WIDTH;
food.y = rand() % HEIGHT;

}

// 绘制游戏界面 void Draw() { // 定位光标到左上角,防止屏幕滚动 COORD coord = {0, 0}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

// 上边界
for (int i = 0; i < WIDTH + 2; i++)
    cout << "#";
cout << endl;

// 逐行绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
    for (int x = 0; x < WIDTH; x++) {
        if (x == 0) cout << "#"; // 左边界

        if (x == snake[0].x && y == snake[0].y)
            cout << "O"; // 蛇头
        else {
            bool isBody = false;
            for (int i = 1; i < snake.size(); i++) {
                if (snake[i].x == x && snake[i].y == y) {
                    cout << "o"; // 蛇身体
                    isBody = true;
                    break;
                }
            }
            if (!isBody) {
                if (x == food.x && y == food.y)
                    cout << "F"; // 食物
                else
                    cout << " "; // 空白
            }
        }

        if (x == WIDTH - 1) cout << "#"; // 右边界
    }
    cout << endl;
}

// 下边界
for (int i = 0; i < WIDTH + 2; i++)
    cout << "#";
cout << endl;

// 提示文字
cout << "得分:" << score << " | WASD控制,X退出游戏" << endl;

}

// 键盘监听 void Input() { if (_kbhit()) { // 判断是否有按键按下 switch (_getch()) { case 'a': if (dir != RIGHT) dir = LEFT; break; case 'd': if (dir != LEFT) dir = RIGHT; break; case 'w': if (dir != DOWN) dir = UP; break; case 's': if (dir != UP) dir = DOWN; break; case 'x': // 按X直接退出 gameOver = true; break; } } }

// 核心逻辑:移动、吃食物、碰撞检测 void Logic() { // 1. 身体跟随头部移动(从后往前赋值) for (int i = snake.size() - 1; i > 0; i--) { snake[i] = snake[i - 1]; } // 更新蛇头坐标 switch (dir) { case LEFT: snake[0].x--; break; case RIGHT: snake[0].x++; break; case UP: snake[0].y--; break; case DOWN: snake[0].y++; break; default: break; }

// 2. 撞墙检测
if (snake[0].x < 0 || snake[0].x >= WIDTH ||
    snake[0].y < 0 || snake[0].y >= HEIGHT) {
    gameOver = true;
}

// 3. 撞到自己身体
for (int i = 1; i < snake.size(); i++) {
    if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
        gameOver = true;
    }
}

// 4. 吃到食物,加分+加长蛇身,刷新食物
if (snake[0].x == food.x && snake[0].y == food.y) {
    score += 10;
    snake.push_back(Point(food.x, food.y));
    // 生成新食物,避免生成在蛇身上
    bool overlap;
    do {
        overlap = false;
        food.x = rand() % WIDTH;
        food.y = rand() % HEIGHT;
        for (vector<Point>::size_type i = 0; i < snake.size(); i++) {
            Point p = snake[i];
            if (p.x == food.x && p.y == food.y) {
                overlap = true;
                break;
            }
        }
    } while (overlap);
}

}

2 comments

  • 1