| #include <stdio.h> |
| #include <windows.h> |
| #include <stdlib.h> |
| #include <conio.h> |
| #include <time.h> |
| #include <malloc.h> |
| #define _Y 20 |
| #define _X 100 |
| char data[_Y][_X]; |
| |
| HANDLE hOutput, hOutBuf; |
| HANDLE houtpoint; |
| COORD coord = {5, 0}; |
| DWORD bytes = 0; |
| int hop_flag = 0; |
| typedef struct snake_xy |
| { |
| int x; |
| int y; |
| } xy; |
| struct Node |
| { |
| xy xy1; |
| struct Node *next; |
| }; |
| |
| void begin(); |
| void menu(int *); |
| void snake(int a, int *b); |
| void change(int *); |
| void background(int x, int y); |
| void gotoxy(int x, int y); |
| struct Node *newNode(xy n); |
| struct Node *creatlist(); |
| void insertNode_to_head(struct Node *snakehead, xy xy2); |
| void freeList(struct Node *headNode); |
| void printList(struct Node *headnode, int score, int level); |
| void fruits(int *flag, xy *fruit); |
| void move(struct Node *headNode, char direction, int level); |
| void insertnewNode_to_back(struct Node *headnode); |
| |
| void printPic() |
| { |
| int i, j; |
| hop_flag = !hop_flag; |
| if (!hop_flag) |
| { |
| houtpoint = hOutput; |
| } |
| else |
| { |
| houtpoint = hOutBuf; |
| } |
| |
| for (i = 0; i < _Y; i++) |
| { |
| for (j = 0; j < _X; j++) |
| { |
| if (i == 0 || i == _Y - 1 || j == 0 || j == _X - 1) |
| { |
| data[i][j] = '*'; |
| } |
| else |
| { |
| data[i][j] = ' '; |
| } |
| } |
| } |
| coord.Y = 1; |
| for (i = 0; i < _Y; i++) |
| { |
| coord.Y++; |
| WriteConsoleOutputCharacter(houtpoint, data[i], _X, coord, &bytes); |
| } |
| |
| SetConsoleActiveScreenBuffer(houtpoint); |
| } |
| |
| int main() |
| { |
| hOutBuf = CreateConsoleScreenBuffer( |
| GENERIC_WRITE, |
| FILE_SHARE_WRITE, |
| NULL, |
| CONSOLE_TEXTMODE_BUFFER, |
| NULL); |
| hOutput = CreateConsoleScreenBuffer( |
| GENERIC_WRITE, |
| FILE_SHARE_WRITE, |
| NULL, |
| CONSOLE_TEXTMODE_BUFFER, |
| NULL); |
| srand((unsigned)time(NULL)); |
| begin(); |
| system("cls"); |
| srand(time(NULL)); |
| int p = 1; |
| int level = 1; |
| int on = 0; |
| printf(" 按w/s选择,输入a确认\n\n"); |
| Sleep(2000); |
| while (on == 0) |
| { |
| if (p == 1) |
| { |
| menu(&p); |
| } |
| switch (p) |
| { |
| case 0: |
| snake(level, &on); |
| break; |
| case 1: |
| change(&level); |
| system("cls"); |
| break; |
| case 2: |
| return 0; |
| break; |
| default: |
| break; |
| } |
| } |
| return 0; |
| } |
| |
| void begin() |
| { |
| int i = 0; |
| for (i = 0; i < 30; i++) |
| { |
| printf("*"); |
| } |
| printf("\n\n"); |
| printf("\t 贪吃蛇游戏\n\n"); |
| for (i = 0; i < 30; i++) |
| { |
| printf("*"); |
| } |
| Sleep(1500); |
| } |
| void menu(int *select) |
| { |
| int a = 0; |
| char b = 0; |
| int c = 0; |
| system("cls"); |
| system("color 6"); |
| system("cls"); |
| printf("\n\n"); |
| while (c == 0) |
| { |
| int i = 0; |
| system("cls"); |
| system("color 6"); |
| gotoxy(5, 0); |
| switch (a) |
| { |
| case 0: |
| printf("\t 开始游戏\n"); |
| break; |
| case 1: |
| printf("\t 难度设置\n"); |
| break; |
| case 2: |
| printf("\t 退出游戏\n"); |
| break; |
| default: |
| break; |
| } |
| b = getch(); |
| switch (b) |
| { |
| case 'w': |
| if (a == 0) |
| a = 2; |
| else |
| a--; |
| break; |
| case 's': |
| if (a == 2) |
| a = 0; |
| else |
| a++; |
| break; |
| case 'a': |
| c = 1; |
| break; |
| default: |
| break; |
| } |
| } |
| *select = a; |
| } |
| void change(int *level) |
| { |
| int a = 1; |
| printf("\t 当前难度:%d \n", *level); |
| printf("\t 请输入变更后的难度:>(1-5):\n"); |
| printf("\t"); |
| scanf("%d", &a); |
| getchar(); |
| *level = a; |
| } |
| void snake(int level, int *on) |
| { |
| xy fruit; |
| char direction = 'd'; |
| int flag = 0; |
| int flag1 = 0; |
| int score = 0; |
| char flag3 = 0; |
| struct Node *list = creatlist(); |
| xy xys = {50, 10}; |
| struct Node *snakehead = newNode(xys); |
| list->next = snakehead; |
| xys.x = 49; |
| insertNode_to_head(snakehead, xys); |
| xys.x = 48; |
| insertNode_to_head(snakehead, xys); |
| Sleep(2000); |
| system("cls"); |
| fflush(stdin); |
| printList(list, score, level); |
| fruits(&flag1, &fruit); |
| while (flag == 0) |
| { |
| fflush(stdin); |
| if (kbhit()) |
| { |
| char turn = getch(); |
| if (direction == 'd' || direction == 'a') |
| { |
| if (turn == 'w' || turn == 's') |
| direction = turn; |
| } |
| if (direction == 'w' || direction == 's') |
| { |
| if (turn == 'a' || turn == 'd') |
| direction = turn; |
| } |
| } |
| else |
| { |
| move(list, direction, level); |
| printList(list, score, level); |
| fruits(&flag1, &fruit); |
| } |
| if (snakehead->xy1.x == 4 || snakehead->xy1.x == 104 || snakehead->xy1.y == 2 || snakehead->xy1.y == 21) |
| { |
| flag = 1; |
| gotoxy(50, 10); |
| printf("你输了"); |
| printf("是否重新开始?:>(Y/N)"); |
| scanf("%c", &flag3); |
| if (flag3 == 'N' || flag3 == 'n') |
| { |
| *on = 1; |
| freeList(list); |
| return; |
| } |
| system("cls"); |
| } |
| if (snakehead->xy1.x == fruit.x && fruit.y == snakehead->xy1.y) |
| { |
| score++; |
| flag1 = 0; |
| insertnewNode_to_back(list); |
| } |
| } |
| } |
| void gotoxy(int x, int y) |
| { |
| COORD p; |
| p.X = x; |
| p.Y = y; |
| SetConsoleCursorPosition(houtpoint, p); |
| } |
| struct Node *creatlist() |
| { |
| struct Node *headnode = (struct Node *)malloc(sizeof(struct Node)); |
| headnode->next = NULL; |
| return headnode; |
| } |
| struct Node *newNode(xy n) |
| { |
| struct Node *newNode1 = (struct Node *)malloc(sizeof(struct Node)); |
| newNode1->xy1 = n; |
| newNode1->next = NULL; |
| return newNode1; |
| } |
| void printList(struct Node *headnode, int score, int level) |
| { |
| system("cls"); |
| printPic(); |
| struct Node *nowNode = headnode->next; |
| while (nowNode != NULL) |
| { |
| int x = nowNode->xy1.x; |
| int y = nowNode->xy1.y; |
| gotoxy(x, y); |
| printf("*"); |
| nowNode = nowNode->next; |
| } |
| gotoxy(105, 10); |
| printf("your score:%d", score); |
| gotoxy(105, 11); |
| printf("当前的难度是:%d\n", level); |
| gotoxy(105, 12); |
| printf("用WASD进行移动"); |
| } |
| void insertNode_to_head(struct Node *snakehead, xy xy2) |
| { |
| struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); |
| newNode->xy1 = xy2; |
| newNode->next = snakehead->next; |
| snakehead->next = newNode; |
| } |
| void freeList(struct Node *headNode) |
| { |
| struct Node *pwdNode = headNode; |
| struct Node *pwdNodeback = pwdNode->next; |
| while (pwdNode) |
| { |
| pwdNode = pwdNodeback; |
| pwdNodeback = pwdNodeback->next; |
| free(pwdNode); |
| } |
| } |
| void move(struct Node *headNode, char direction, int level) |
| { |
| struct Node *nowNode = headNode->next; |
| struct Node *nowNode1 = (struct Node *)malloc(sizeof(struct Node *)); |
| nowNode1->xy1 = nowNode->xy1; |
| struct Node *nowNode2 = (struct Node *)malloc(sizeof(struct Node *)); |
| nowNode2->xy1 = nowNode->next->xy1; |
| switch (direction) |
| { |
| case 'd': |
| nowNode->xy1.x = nowNode->xy1.x + 1; |
| Sleep(110 - level * 10); |
| break; |
| case 'a': |
| nowNode->xy1.x = nowNode->xy1.x - 1; |
| Sleep(110 - level * 10); |
| break; |
| case 's': |
| nowNode->xy1.y = nowNode->xy1.y + 1; |
| Sleep(110 - level * 10); |
| |
| break; |
| case 'w': |
| nowNode->xy1.y = nowNode->xy1.y - 1; |
| Sleep(110 - level * 10); |
| break; |
| default: |
| break; |
| } |
| while (nowNode->next != NULL) |
| { |
| nowNode2->xy1 = nowNode->next->xy1; |
| nowNode->next->xy1 = nowNode1->xy1; |
| nowNode1->xy1 = nowNode2->xy1; |
| nowNode = nowNode->next; |
| } |
| } |
| void fruits(int *flag, xy *fruit) |
| { |
| if (*flag == 0) |
| { |
| *flag = 1; |
| (*fruit).x = 6 + rand() % 96 + 1; |
| (*fruit).y = 4 + rand() % 16 + 1; |
| } |
| gotoxy((*fruit).x, (*fruit).y); |
| printf("*"); |
| } |
| void insertnewNode_to_back(struct Node *headnode) |
| { |
| struct Node *pwdNode = headnode->next; |
| struct Node *NewNode = (struct Node *)malloc(sizeof(struct Node *)); |
| while (pwdNode->next != NULL) |
| { |
| pwdNode = pwdNode->next; |
| } |
| pwdNode->next = NewNode; |
| NewNode->next = NULL; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库