IE盒子

搜索
查看: 140|回复: 2

贪吃蛇(c语言实现)

[复制链接]

2

主题

10

帖子

21

积分

新手上路

Rank: 1

积分
21
发表于 2023-3-21 18:51:39 | 显示全部楼层 |阅读模式
应用界面

菜单栏



游戏说明



关于界面



游戏界面








截了几张图(名字有点中二哈哈)

游戏结束时的排名界面



最后附上代码

头文件 snake.h

#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>

#define MAP_HEIGHT 20
#define MAP_WIDTH  40
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'

typedef struct {
        int x;
        int y;
}Food, StrongFood, Snakenode, Barrier;

typedef struct {
        Snakenode snakeNode[1000];
        int length;
        int speed;
}Snake;

void GotoXY(int, int);
void Hide();
int Menu();
void Help();
void About();
void InitMap();
void PrintFood();
void PrintStrongFood();
void PrintBarrier();
int MoveSnake();
int IsCorrect();
void SpeedControl();源文件 snake.c

#include"snake.h"

Snake snake;
Food food;
StrongFood strongfood;
Barrier barrier;
char now_Dir = RIGHT;
char direction = RIGHT;
int a[10] = { 0 };

int Menu() {
        GotoXY(45, 8);
        printf("贪吃蛇 Greedy Snake");
        GotoXY(45, 10);
        printf("(仅支持键盘操作)");
        GotoXY(43, 14);
        printf("1.开始游戏");
        GotoXY(43, 16);
        printf("2.帮助");
        GotoXY(43, 18);
        printf("3.关于");
        GotoXY(43, 22);
        printf("其他任意位置退出游戏");
        Hide();
        char ch;
        int result = 0;
        ch = _getch();
        switch (ch) {
        case'1': result = 1; break;
        case'2': result = 2; break;
        case'3': result = 3; break;
        }
        system("cls");
        return result;
}

void GotoXY(int x, int y) {
        HANDLE hout;
        COORD cor;
        hout = GetStdHandle(STD_OUTPUT_HANDLE);
        cor.X = x;
        cor.Y = y;
        SetConsoleCursorPosition(hout, cor);
}

void Hide() {
        HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_CURSOR_INFO cor_info = { 1,0 };
        SetConsoleCursorInfo(hout, &cor_info);
}

void About() {
        GotoXY(44, 12);
        printf("杭州电子科技大学");
        GotoXY(46, 14);
        printf("Made By Ding");
        GotoXY(43, 16);
        printf("按任意键返回上级菜单");
        Hide();
        char ch = _getch();
        system("cls");
}

void Help() {
        GotoXY(40, 6);
        printf("w 上");
        GotoXY(40, 8);
        printf("s 下");
        GotoXY(40, 10);
        printf("a 左");
        GotoXY(40, 12);
        printf("d 右");
        GotoXY(40, 16);
        printf("当蛇撞到墙或障碍物时游戏结束");
        GotoXY(40, 18);
        printf("吃到$时蛇身增加一级");
        GotoXY(40, 20);
        printf("吃到€时蛇身增加两级");
        GotoXY(40, 22);
        printf("吃到食物时地图上出现障碍物\"X\"");
        GotoXY(40, 25);
        printf("按任意键返回上级菜单");
        Hide();
        char ch = _getch();
        system("cls");
}

void InitMap() {
        Hide();
        GotoXY(14, 21);
        printf("Greedy Snake");
        snake.snakeNode[0].x = MAP_WIDTH / 2 - 1;
        snake.snakeNode[0].y = MAP_HEIGHT / 2 - 1;
        GotoXY(snake.snakeNode[0].x, snake.snakeNode[0].y);
        printf("@");
        snake.length = 3;
        snake.speed = 250;
        now_Dir = RIGHT;
        for (int i = 1; i < snake.length; i++) {
                snake.snakeNode.y = snake.snakeNode[i - 1].y;
                snake.snakeNode.x = snake.snakeNode[i - 1].x - 1;
                GotoXY(snake.snakeNode.x, snake.snakeNode.y);
                printf("O");
        }
        for (int i = 0; i <= MAP_WIDTH; i++) {
                GotoXY(i, 0);
                printf("_");
                GotoXY(i, MAP_HEIGHT - 1);
                printf("_");
        }
        for (int i = 1; i < MAP_HEIGHT; i++) {
                GotoXY(0, i);
                printf("|");
                GotoXY(MAP_WIDTH, i);
                printf("|");
        }
        PrintFood();
        PrintStrongFood();
        GotoXY(50, 4);
        printf("当前得分:0   ");
        GotoXY(50, 8);
        printf("当前速度:I   ");
        GotoXY(50, 12);
        printf("当前形态:蛇胚                    ");
}

void PrintFood() {
        int flag = 1;
        while (flag) {
                flag = 0;
                food.x = rand() % (MAP_WIDTH - 2) + 1;
                food.y = rand() % (MAP_HEIGHT - 2) + 1;
                for (int k = 0; k <= snake.length - 1; k++) {
                        if (snake.snakeNode[k].x == food.x && snake.snakeNode[k].y == food.y) {
                                flag = 1;
                                break;
                        }
                }
        }
        GotoXY(food.x, food.y);
        printf("$");
}
void PrintStrongFood() {
        int flag = 1;
        while (flag) {
                flag = 0;
                strongfood.x = rand() % (MAP_WIDTH - 2) + 1;
                strongfood.y = rand() % (MAP_HEIGHT - 2) + 1;
                for (int k = 0; k <= snake.length - 1; k++) {
                        if (snake.snakeNode[k].x == strongfood.x && snake.snakeNode[k].y == strongfood.y) {
                                flag = 1;
                                break;
                        }
                    }
                if (strongfood.x == food.x && strongfood.y == food.y) {
                        flag = 1;
            }
    }
        GotoXY(strongfood.x, strongfood.y);
        printf("€");
}

void PrintBarrier() {
        int flag = 1;
        GotoXY(barrier.x, barrier.y);
        printf(" ");
        while (flag) {
                flag = 0;
                barrier.x = rand() % (MAP_WIDTH - 2) + 1;
                barrier.y = rand() % (MAP_HEIGHT - 2) + 1;
                for (int k = 0; k <= snake.length - 1; k++) {
                        if (snake.snakeNode[k].x == barrier.x && snake.snakeNode[k].y == barrier.y) {
                                flag = 1;
                                break;
                        }
                        }
                        if (barrier.x == food.x && barrier.y == food.y || barrier.x == strongfood.x && barrier.y == strongfood.y) {
                                flag = 1;
                }
        }
        GotoXY(barrier.x, barrier.y);
        printf("X");
}

int MoveSnake() {
        Snakenode temp;
        int flag = 0;
        temp = snake.snakeNode[snake.length - 1];
        for (int i = snake.length - 1; i >= 1; i--)
                snake.snakeNode = snake.snakeNode[i - 1];
        GotoXY(snake.snakeNode[1].x, snake.snakeNode[1].y);
        printf("O");
        if (_kbhit()) {
                direction = _getch();
                switch (direction) {
                case UP:
                        if (now_Dir != DOWN)
                                now_Dir = direction;
                        break;
                case DOWN:
                        if (now_Dir != UP)
                                now_Dir = direction;
                        break;
                case LEFT:
                        if (now_Dir != RIGHT)
                                now_Dir = direction;
                        break;
                case RIGHT:
                        if (now_Dir != LEFT)
                                now_Dir = direction;
                        break;
                }
        }
        switch (now_Dir) {
        case UP:snake.snakeNode[0].y--; break;
        case DOWN:snake.snakeNode[0].y++; break;
        case LEFT:snake.snakeNode[0].x--; break;
        case RIGHT:snake.snakeNode[0].x++; break;
        }
        GotoXY(snake.snakeNode[0].x, snake.snakeNode[0].y);
        printf("@");
        if (snake.snakeNode[0].x == food.x && snake.snakeNode[0].y == food.y) {
                snake.length++;
                flag = 1;
                snake.snakeNode[snake.length - 1] = temp;
        }
        if (snake.snakeNode[0].x == strongfood.x && snake.snakeNode[0].y == strongfood.y) {
                snake.length += 2;
                flag = 2;
                snake.snakeNode[snake.length] = temp;
        }
        if (!flag) {
                GotoXY(temp.x, temp.y);
                printf(" ");
        }
        else if(flag == 1) {
                PrintFood();
                PrintBarrier();
                GotoXY(50, 4);
                printf("当前得分:%d   ", snake.length - 3);
        }
        else if (flag == 2) {
                PrintStrongFood();
                PrintBarrier();
                GotoXY(temp.x, temp.y);
                printf(" ");
                GotoXY(50, 4);
                printf("当前得分:%d   ", snake.length - 3);
        }
        if (!IsCorrect()) {
                system("cls");
                GotoXY(30, 10);
                printf("最终得分:%d   ", snake.length - 3);
                int i = 0;
                while (i < 10) {
                        if ((snake.length - 3) > a) {
                                int j = 9;
                                while (j > i) {
                                        a[j] = a[j - 1];
                                        j--;
                                }
                                a = snake.length - 3;
                                break;
                        }
                        i++;
                }
               
                GotoXY(30, 12);
                printf("不错的!");
                GotoXY(30, 14);
                printf("按任意键返回主菜单");
                GotoXY(60, 2);
                printf("得分前十排行榜");
                for (int i = 0; i < 10; i++) {
                        GotoXY(63, (i + 2)*2);
                        printf("%02d: %d  分", i + 1, a);
                }
                char c = _getch();
                system("cls");
                return 0;
        }
        SpeedControl();
        Sleep(snake.speed);
        return 1;
}

int IsCorrect() {
        if (snake.snakeNode[0].x == 0 || snake.snakeNode[0].y == 0 || snake.snakeNode[0].x == MAP_WIDTH - 1 || snake.snakeNode[0].y == MAP_HEIGHT - 1)
                return 0;
        for (int i = 1; i < snake.length; i++) {
                if (snake.snakeNode[0].x == snake.snakeNode.x && snake.snakeNode[0].y == snake.snakeNode.y) {
                        for (int j = i + 1; j < snake.length; j++) {
                                GotoXY(snake.snakeNode[j].x, snake.snakeNode[j].y);
                                printf(" ");
                        }
                        snake.length = i + 1;
                }
                       
        }
        if (snake.snakeNode[0].x == barrier.x && snake.snakeNode[0].y == barrier.y) {
                return 0;
        }
        return 1;
}

void SpeedControl() {
        switch (snake.length) {
        case 4:
        case 5:
        case 6:
        case 7:
                snake.speed = 200;
                GotoXY(50, 8);
                printf("当前速度:II  ");
                GotoXY(50, 12);
                printf("当前形态:小白蛇               ");
                break;
        case 8:
        case 9:
        case 10:
        case 11:
                snake.speed = 180;
                GotoXY(50, 8);
                printf("当前速度:III ");
                GotoXY(50, 12);
                printf("当前形态:珊瑚蛇               ");
                break;
        case 12:
        case 13:
        case 14:
        case 15:
                snake.speed = 160;
                GotoXY(50, 8);
                printf("当前速度:IV  ");
                GotoXY(50, 12);
                printf("当前形态:眼镜王蛇             ");
                break;
        case 16:
        case 17:
        case 18:
        case 19:
                snake.speed = 140;
                GotoXY(50, 8);
                printf("当前速度:V   ");
                GotoXY(50, 12);
                printf("当前形态:亚马逊森蟒           ");
                break;
        case 20:
        case 21:
        case 22:
        case 23:
                snake.speed = 120;
                GotoXY(50, 8);
                printf("当前速度:VI  ");
                GotoXY(50, 12);
                printf("当前形态:日本八岐大蛇         ");
                break;
        case 24:
        case 25:
        case 26:
        case 27:
                snake.speed = 100;
                GotoXY(50, 8);
                printf("当前速度:VII ");
                GotoXY(50, 12);
                printf("当前形态:冥蛇-阿波菲斯        ");
                break;
        case 29:
        case 30:
        case 31:
        case 32:
                snake.speed = 80;
                GotoXY(50, 8);
                printf("当前速度:VIII");
                GotoXY(50, 12);
                printf("当前形态:尘世巨蟒-约尔曼冈德   ");
                break;
        case 33:
        case 34:
        case 35:
        case 36:
                snake.speed = 60;
                GotoXY(50, 8);
                printf("当前速度:IX  ");
                GotoXY(50, 12);
                printf("当前形态:羽蛇神-魁札科亚特尔   ");
                break;
        case 37:
        case 38:
        case 39:
        case 40:
                snake.speed = 40;
                GotoXY(50, 8);
                printf("当前速度:X   ");
                GotoXY(50, 12);
                printf("当前形态:永恒之蛇-乌洛波洛斯   ");
                break;
        default:break;
        }
}源文件 snake_main.c

#include"snake.h"
int main() {
        srand((unsigned int)time(0));
        int end = 1, result;
        while (end) {
                result = Menu();
                switch (result) {
                case 1:
                        InitMap();
                        while (MoveSnake());
                        break;
                case 2:
                        Help();
                        break;
                case 3:
                        About();
                        break;
                default:
                        end = 0;
                        break;
                }
        }
        return 0;
}
回复

使用道具 举报

2

主题

7

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2023-3-21 18:52:16 | 显示全部楼层
[害羞]
回复

使用道具 举报

1

主题

3

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2023-3-21 18:52:58 | 显示全部楼层
[害羞]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表