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

#define WIDTH 20 #define HEIGHT 20 #define MAX_FOOD 4

// 蛇的结构体 struct Snake { int x[100]; int y[100]; int length; int direction; };

// 食物的结构体 struct Food { int x; int y; };

// 设置控制台光标位置 void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }

// 隐藏控制台光标 void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }

// 初始化蛇 void initSnake(Snake& snake) { snake.length = 3; snake.x[0] = WIDTH / 2; snake.y[0] = HEIGHT / 2; for (int i = 1; i < snake.length; i++) { snake.x[i] = snake.x[i - 1] - 1; snake.y[i] = snake.y[i - 1]; } snake.direction = 1; }

// 初始化食物 void initFood(Food food[], int& foodCount) { srand((unsigned)time(NULL)); foodCount = rand() % 4 + 1; for (int i = 0; i < foodCount; i++) { food[i].x = rand() % (WIDTH - 2) + 1; food[i].y = rand() % (HEIGHT - 2) + 1; } }

// 绘制游戏界面 void draw(Snake& snake, Food food[], int foodCount) { system("cls"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) { std::cout << "#"; } else if (i == snake.y[0] && j == snake.x[0]) { std::cout << "O"; } else { bool isFood = false; for (int k = 0; k < foodCount; k++) { if (i == food[k].y && j == food[k].x) { std::cout << "X"; isFood = true; break; } } if (!isFood) { bool isSnakePart = false; for (int k = 1; k < snake.length; k++) { if (i == snake.y[k] && j == snake.x[k]) { std::cout << "o"; isSnakePart = true; break; } } if (!isSnakePart) { std::cout << " "; } } } } std::cout << std::endl; } }

// 移动蛇 void move(Snake& snake) { for (int i = snake.length - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } switch (snake.direction) { case 0: snake.y[0]--; break; case 1: snake.x[0]++; break; case 2: snake.y[0]++; break; case 3: snake.x[0]--; break; } }

// 改变蛇的方向 void changeDirection(Snake& snake) { if (_kbhit()) { char ch = _getch(); switch (ch) { case 'w': if (snake.direction != 2) snake.direction = 0; break; case 'd': if (snake.direction != 3) snake.direction = 1; break; case 's': if (snake.direction != 0) snake.direction = 2; break; case 'a': if (snake.direction != 1) snake.direction = 3; break; } } }

// 检查是否吃到食物 bool checkEat(Snake& snake, Food food[], int& foodCount) { for (int i = 0; i < foodCount; i++) { if (snake.x[0] == food[i].x && snake.y[0] == food[i].y) { snake.length++; // 移除被吃掉的食物 for (int j = i; j < foodCount - 1; j++) { food[j] = food[j + 1]; } foodCount--; // 如果没有食物了,重新生成 if (foodCount == 0) { initFood(food, foodCount); } return true; } } return false; }

// 检查是否撞到墙或自己 bool checkCollision(Snake& snake) { if (snake.x[0] == 0 || snake.x[0] == WIDTH - 1 || snake.y[0] == 0 || snake.y[0] == HEIGHT - 1) { return true; } for (int i = 1; i < snake.length; i++) { if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) { return true; } } return false; }

int main() { Snake snake; Food food[MAX_FOOD]; int foodCount;

initSnake(snake);
initFood(food, foodCount);
hideCursor();

while (true) {
    draw(snake, food, foodCount);
    changeDirection(snake);
    move(snake);
    checkEat(snake, food, foodCount);
    if (checkCollision(snake)) {
        std::cout << "Game Over!" << std::endl;
        break;
    }
    Sleep(160);
}

return 0;

}