#include #include <conio.h> #include <windows.h> #include using namespace std;

// 定义常量 #define WIDTH 60 // 游戏区域宽度 #define HEIGHT 20 // 游戏区域高度 #define SNAKE_MAX 100 // 蛇的最大长度

// 方向键的ASCII码 #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77

// 坐标结构体 struct Pos { int x, y; } snake[SNAKE_MAX], food;

int n = 1; // 蛇的长度 int direction = RIGHT; // 初始方向 int score = 0; // 分数 bool gameOver = false; // 游戏结束标志 int key = RIGHT; // 当前移动方向

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

// 将光标移动到指定位置 void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }

// 初始化游戏 void initGame() { // 初始化蛇的位置 snake[0].x = WIDTH / 2; snake[0].y = HEIGHT / 2;

// 生成第一个食物
srand((unsigned)time(NULL));
food.x = rand() % (WIDTH - 2) + 1;
food.y = rand() % (HEIGHT - 2) + 1;

// 初始化方向和其他参数
direction = RIGHT;
key = RIGHT;
n = 1;
score = 0;
gameOver = false;

}

// 绘制游戏界面 void drawGame() { // 清屏 system("cls");

// 绘制上边界
for (int i = 0; i <= WIDTH; i++) {
    cout << "#";
}
cout << endl;

// 绘制中间部分
for (int i = 1; i < HEIGHT; i++) {
    for (int j = 0; j <= WIDTH; j++) {
        if (j == 0 || j == WIDTH) {
            cout << "#"; // 绘制左右边界
        } else {
            bool print = false;
            // 绘制蛇
            for (int k = 0; k < n; k++) {
                if (snake[k].x == j && snake[k].y == i) {
                    if (k == 0) cout << "@"; // 蛇头
                    else cout << "O";      // 蛇身
                    print = true;
                    break;
                }
            }
            // 绘制食物
            if (!print && food.x == j && food.y == i) {
                cout << "*";
            } else {
                cout << " ";
            }
        }
    }
    cout << endl;
}

// 绘制下边界
for (int i = 0; i <= WIDTH; i++) {
    cout << "#";
}
cout << endl;

// 显示分数
gotoxy(WIDTH + 2, 2);
cout << "得分: " << score;
gotoxy(WIDTH + 2, 4);
cout << "按ESC退出游戏";

} void checkCollision() { // 检查是否撞墙 if (snake[0].x == 0 || snake[0].x == WIDTH || snake[0].y == 0 || snake[0].y == HEIGHT) { gameOver = true; }

// 检查是否撞到自己
for (int i = 1; i < n; i++) {
    if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
        gameOver = true;
        break;
    }
}

} // 控制蛇的移动 void moveSnake() { // 更新蛇头位置 Pos newHead = snake[0];

// 根据方向更新头部位置
switch(direction) {
    case UP:    newHead.y--; break;
    case DOWN:  newHead.y++; break;
    case LEFT:  newHead.x--; break;
    case RIGHT: newHead.x++; break;
}

// 检查是否吃到食物
if (newHead.x == food.x && newHead.y == food.y) {
    // 增加长度和分数
    n++;
    score += 10;
    
    // 生成新食物
    bool validPosition;
    do {
        validPosition = true;
        food.x = rand() % (WIDTH - 2) + 1;
        food.y = rand() % (HEIGHT - 2) + 1;
        
        // 确保食物不在蛇身上
        for (int i = 0; i < n; i++) {
            if (food.x == snake[i].x && food.y == snake[i].y) {
                validPosition = false;
                break;
            }
        }
    } while (!validPosition);
} else {
    // 如果没吃到食物,移动时删除尾部
    for (int i = n - 1; i > 0; i--) {
        snake[i] = snake[i - 1];
    }
}

// 将新头部添加到蛇身体的开头
snake[0] = newHead;

// 检查碰撞
checkCollision();

}

// 检查碰撞

// 处理键盘输入 void keyboardControl() { if (_kbhit()) { int keyPress = _getch();

    // 如果是方向键,会有两个键码,第一个是0或224
    if (keyPress == 0 || keyPress == 224) {
        keyPress = _getch();
        
        // 防止直接反向移动
        if ((keyPress == UP && direction != DOWN) ||
            (keyPress == DOWN && direction != UP) ||
            (keyPress == LEFT && direction != RIGHT) ||
            (keyPress == RIGHT && direction != LEFT)) {
            direction = keyPress;
        }
    }
    
    // ESC键退出游戏
    if (keyPress == 27) {
        gameOver = true;
    }
}

}

int main() { // 设置控制台窗口大小 char cmd[50]; sprintf(cmd, "mode con cols=%d lines=%d", WIDTH + 5, HEIGHT + 5); system(cmd);

// 隐藏光标
hideCursor();

// 初始化游戏
initGame();

// 游戏主循环
while (!gameOver) {
    // 处理键盘输入
    keyboardControl();
    
    // 移动蛇
    moveSnake();
    
    // 绘制游戏界面
    drawGame();
    
    // 控制游戏速度
    Sleep(30);
}

// 游戏结束
system("cls");
gotoxy(WIDTH / 2 - 5, HEIGHT / 2);
cout << "游戏结束!";
gotoxy(WIDTH / 2 - 7, HEIGHT / 2 + 2);
cout << "最终得分: " << score;
gotoxy(WIDTH / 2 - 10, HEIGHT / 2 + 4);
cout << "按任意键退出...";
_getch();

return 0;

}

0 条评论

目前还没有评论...