C语言实现彩色贪吃蛇

#include
#include
#include
const int HEIGHT = 20;
const int WIDTH = 40;
struct Snakebody {
    int x, y;
    struct Snakebody* next;
};
struct Snakefood {
    int x;
    int y;
};
int sum = 0,JudgeSum = 0, Hard = 0;
int Pause = 200000000;
int JudgeDirection = 4;
Snakebody* Shead = NULL;
Snakebody* Shead_temp = NULL;
Snakebody* Sbody = NULL;
Snakebody* Send = NULL;
Snakefood* Food = NULL;
void Jfood();
void Jwall();
void Jsnake();
void MakeFood();
void ControlMove();
void MoveCursor(int x, int y);
void Move();
void Free();
inline void hide() {
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void MoveCursor(int x, int y)//设置光标位置
{
    COORD pos = { x * 2,y };
    HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);   
    SetConsoleCursorPosition(output, pos); //设置控制台光标位置
}
inline void Score_HardLevel()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    MoveCursor(33, 5);
    printf("Score:%d", sum);
    MoveCursor(33, 7);
    printf("Hard :%d", Hard);
}
inline void printsnake()
{
    printf("                                                                                         \n");
    printf("                       __________                                                        \n");
    printf("                      /          \\                                                      \n");
    printf("                     /  ________  \\                                                     \n");
    printf("                     |  |      |__|                                                      \n");
    printf("                     |  |                                                                \n");
    printf("                     \\  \\_______                              ____                     \n");
    printf("                      \\         \\    ____ ____      ____   __ |  |  ___   ______       \n");
    printf("                       \\_______  \\   |  |/    \\    /    \\_/ / |  | /  /  /      \\   \n");
    printf("                               \\  \\  |    ___  \\  / ____   /  |  |/  /  /  ____  \\   \n");
    printf("                     __        |  |  |   /   \\  \\ | |  |  /   |     /  |  /____\\  |   \n");
    printf("                    \\  \\_______|  |  |  |    |  | | |__|  |   |     \\  |  ________/   \n");
    printf("                     \\            /  |  |    |  |  \\       \\  |  |\\  \\  \\  \\____  \n");
    printf("                      \\__________/   |__|    |__|   \\___/\\__\\ |__| \\__\\  \\______/ \n");
}
inline void Start_screen()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    MoveCursor(1, 1);
    printsnake();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    MoveCursor(18, 16);
    printf("Spike 2020.5.8");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
    MoveCursor(21, 18);
    printf("按回车键开始");
    getchar();
    system("cls");
}
inline void DrawMap() {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    for (int i = 0; i < WIDTH; i++) printf("■");
    printf("\n");
    for (int i = 1; i < HEIGHT - 1; i++)
        for (int j = 0; j < WIDTH; j++)
            if (j == 0 || j == WIDTH - 1 || j == WIDTH - 10) {
                printf("■");
                if (j == WIDTH - 1) printf("\n");
            }
            else printf("  ");
    for (int i = 0; i < WIDTH; i++) printf("■");
    Score_HardLevel();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
    MoveCursor(34, 10);
    printf("↑");
    MoveCursor(31, 11);
    printf("使用←↓→控制蛇");
    MoveCursor(31, 12);
    printf("移动,每获得五分");
    MoveCursor(31, 13);
    printf("难度加一,撞墙游");
    MoveCursor(31, 14);
    printf("戏结束");
}
inline void Initialization_Snake() {
    for (int i = 0; i < 5; i++) {
        Sbody = (Snakebody*)malloc(sizeof(Snakebody));
        Sbody->x = 5 - i;
        Sbody->y = 5;
        Sbody->next = NULL;
        if (Shead == NULL) Shead = Sbody;
        else Send->next = Sbody;
        Send = Sbody;
    }
    Shead_temp = Shead;
    while (Shead_temp->next != NULL) {
        MoveCursor(Shead_temp->x, Shead_temp->y);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
        printf("■");
        Shead_temp = Shead_temp->next;
    }
}
inline void MakeFood() {
    srand((int)time(0));//随机种子
    int x = rand() % 27 + 2;
    int y = rand() % 17 + 2;
    Shead_temp = Shead;
    while (1) {
        if (Shead_temp->next == NULL) {
            if (Shead_temp->x == x && Shead_temp->y == y) {
                x = rand() % 27 + 2;
                y = rand() % 17 + 2;
            }
            break;
        }
        if (Shead_temp->x == x && Shead_temp->y == y) {
            x = rand() % 27 + 2;
            y = rand() % 17 + 2;
        }
        else Shead_temp = Shead_temp->next;
        if (Shead_temp->next == NULL) {
            if (Shead_temp->x == x && Shead_temp->y == y) {
                x = rand() % 27 + 2;
                y = rand() % 17 + 2;
            }
            break;
        }
    }
    MoveCursor(x, y);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN);
    printf("★");
    Food = (Snakefood*)malloc(sizeof(Snakefood));
    Food->x = x;
    Food->y = y;
    Score_HardLevel();
    sum++;
}
inline void ControlMove() {
    if (GetAsyncKeyState(VK_UP) && 0x8000)
        if (JudgeDirection != 2) JudgeDirection = 1;
    if (GetAsyncKeyState(VK_DOWN) && 0x8000)
        if (JudgeDirection != 1) JudgeDirection = 2;
    if (GetAsyncKeyState(VK_RIGHT) && 0x8000)
        if (JudgeDirection != 3) JudgeDirection = 4;
    if (GetAsyncKeyState(VK_LEFT) && 0x8000)
        if (JudgeDirection != 4) JudgeDirection = 3;
    if (GetAsyncKeyState(VK_RETURN) && 0x0D)
        while (1)
            if (GetAsyncKeyState(VK_RETURN) && 0x0D) break;
}
inline void Jfood() {
    Shead_temp = Shead;
    if (Shead_temp->x == Food->x && Shead_temp->y == Food->y) {
        MakeFood();
        JudgeSum += 1;
        if (JudgeSum == 5) {
            JudgeSum = 0;
            Hard += 1;
            Pause -= 20000000;
        }
        while (Shead_temp->next != NULL)
            Shead_temp = Shead_temp->next;
        Snakebody* t = (Snakebody*)malloc(sizeof(Snakebody));
        t->x = Food->x;
        t->y = Food->y;
        t->next = NULL;
        Shead_temp->next = t;
        MoveCursor(Shead_temp->x, Shead_temp->y);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
        printf("■");
    }
}
inline void Move() {
    while (1) {
        hide();
        Shead_temp = Shead;
        while (Shead_temp->next->next != NULL)
            Shead_temp = Shead_temp->next;
        Shead_temp->next = NULL;
        for (int i = 0; i < Pause; i++) {}
        ControlMove();
        MoveCursor(Shead_temp->x, Shead_temp->y);
        printf("  ");
        Snakebody* Shead_2 = (Snakebody*)malloc(sizeof(Snakebody));
        if (JudgeDirection == 1) {
            Shead_2->x = Shead->x;
            Shead_2->y = Shead->y - 1;
        }
        if (JudgeDirection == 2) {
            Shead_2->x = Shead->x;
            Shead_2->y = Shead->y + 1;
        }
        if (JudgeDirection == 3) {
            Shead_2->x = Shead->x - 1;
            Shead_2->y = Shead->y;
        }
        if (JudgeDirection == 4) {
            Shead_2->x = Shead->x + 1;
            Shead_2->y = Shead->y;
        }
        Shead_2->next = Shead;
        Shead = Shead_2;
        MoveCursor(Shead_2->x, Shead_2->y);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
        printf("■");
        Jfood();
        Jwall();
        Jsnake();
    }
}
inline void Jwall() {
    if (Shead->x == 0 || Shead->x == 30 || Shead->y == 0 || Shead->y == 19) {
        MoveCursor(10, 20);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        printf("抱歉,你撞到了墙壁,游戏结束!");
        system("pause>nul");
        exit(0);
    }
}
inline void Jsnake() {
    Shead_temp = Shead->next;
    while (Shead_temp->next != NULL) {
        if ((Shead->x == Shead_temp->x) && (Shead->y == Shead_temp->y)) {
            MoveCursor(10, 20);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            printf("抱歉,你撞到了自己,游戏结束!");
            system("pause>nul");
            exit(0);
        }
        Shead_temp = Shead_temp->next;
    }
}
inline void Free() {
    while (Shead->next != NULL) {
        Shead = Shead->next;
        free(Shead);
    }
    free(Shead);
    while (Shead_temp->next != NULL) {
        Shead_temp = Shead_temp->next;
        free(Shead_temp);
    }
    free(Shead_temp);
    while (Sbody->next != NULL) {
        Sbody = Sbody->next;
        free(Sbody);
    }
    free(Sbody);
    while (Send->next != NULL) {
        Send = Send->next;
        free(Send);
    }
    free(Send);
    free(Food);
}
int main() {
    hide();
    Start_screen();
    DrawMap();
    Initialization_Snake();
    MakeFood();
    Move();
    Free();
}

你可能感兴趣的:(C语言实现彩色贪吃蛇)