#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

// 游戏设置
const int width = 20;
const int height = 20;
bool gameOver;
int score;

// 蛇的位置和方向
int x, y, fruitX, fruitY;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

// 蛇的身体
vector<int> tailX;
vector<int> tailY;
int nTail;

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

// 隐藏控制台光标
void hideCursor() {
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
}

// 初始化游戏
void Setup() {
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    srand(time(NULL));
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
    nTail = 0;
    hideCursor();
}

// 绘制游戏界面
void Draw() {
    system("cls"); // 清屏
    
    // 绘制顶部边界
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;

    // 绘制中间部分
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "#";
                
            if (i == y && j == x)
                cout << "O"; // 蛇头
            else if (i == fruitY && j == fruitX)
                cout << "F"; // 食物
            else {
                bool print = false;
                for (int k = 0; k < nTail; k++) {
                    if (tailX[k] == j && tailY[k] == i) {
                        cout << "o"; // 蛇身
                        print = true;
                    }
                }
                if (!print)
                    cout << " ";
            }
                
            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    // 绘制底部边界
    for (int i = 0; i < width + 2; i++)
        cout << "#";
    cout << endl;
    
    // 显示分数
    cout << "Score: " << score << endl;
    cout << "Use WASD to control, X to exit" << 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':
                gameOver = true;
                break;
        }
    }
}

// 游戏逻辑
void Logic() {
    // 保存之前的位置用于移动尾部
    int prevX = tailX.size() > 0 ? tailX[0] : x;
    int prevY = tailY.size() > 0 ? tailY[0] : y;
    int prev2X, prev2Y;
    
    // 移动尾部
    tailX.insert(tailX.begin(), x);
    tailY.insert(tailY.begin(), y);
    if (tailX.size() > nTail) {
        tailX.pop_back();
        tailY.pop_back();
    }
    
    // 移动蛇头
    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;
        nTail++;
        
        // 生成新食物,不能在蛇身上
        bool onSnake;
        do {
            onSnake = false;
            fruitX = rand() % width;
            fruitY = rand() % height;
            for (int i = 0; i < nTail; i++) {
                if (tailX[i] == fruitX && tailY[i] == fruitY) {
                    onSnake = true;
                    break;
                }
            }
        } while (onSnake);
    }
}

int main() {
    Setup();
    while (!gameOver) {
        Draw();
        Input();
        Logic();
        Sleep(100); // 控制游戏速度
    }
    
    // 游戏结束
    gotoxy(width/2 - 5, height/2);
    cout << "GAME OVER!" << endl;
    gotoxy(width/2 - 7, height/2 + 1);
    cout << "Final Score: " << score << endl;
    gotoxy(0, height + 3);
    
    return 0;
}    

0 条评论

目前还没有评论...

信息

ID
116
时间
1000ms
内存
256MiB
难度
8
标签
递交数
56
已通过
9
上传者