C++ 贪吃蛇游戏代码解析 - 从入门到精通
#include<stdio.h>#include<conio.h>//#include<iostream>#include <process.h> #include<stdlib.h>#include<windows.h>#include<time.h>#define framex 2#define framey 2#define wide 40#define high 25\n\nusing namespace std;\n\nint i,j,a[2];\n\n//更改颜色\nint color(int c)\n{\n\tSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色\n\treturn 0;\n}\n\n\n//将光标移动到指定位置 \nvoid gotoxy(HANDLE hout,int x,int y){\n //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标\n COORD pos;\n pos.X=x;\n pos.Y=y;\n //SetConsoleCursorPosition是API中定位光标位置的函数。\n SetConsoleCursorPosition(hout,pos);\n}\nvoid cover (HANDLE hout);\nvoid explation(HANDLE hout)\n {\n \tint i,j=1;\n \tsystem("cls");\n \tcolor(13);\n \tgotoxy(hout,44,3);\n \tcout << "游戏说明"; \n\t color(2);\n\t for(i=6;i<=22;i++)\n\t {\n\t \tfor(j=20;j<=75;j++)\n\t \t{\n\t \t\tgotoxy(hout,j,i);\n\t \t\tif(i==6||i==22)cout << "="; \n\t \t\telse if(j==20||j==75)cout << "||";\n\t \t}\n\t }\n\t color(3);\n\t gotoxy(hout,25,8);\n\t cout << "1、不能穿墙,不能咬到自己"; \n\t color(10);\n\t gotoxy(hout,25,11);\n\t cout << "2、用 \u25b2(W) \u25bc(S) \u25c0(A) \u25b6(D)分别控制蛇的移动";\n\t color(14);\n\t gotoxy(hout,25,14);\n\t cout << "3、按任意键(除去ESC)暂停游戏,方向键继续";\n\t color(11);\n\t gotoxy(hout,25,17);\n\t cout << "4、ESC:自爆,游戏结束";\n\t color(4);\n\t getch();//按任意键返回主界面\n\t system("cls");\n\t cover (hout); \n }\n\n\n//游戏封面\nvoid cover (HANDLE hout){\n\tgotoxy(hout,35,1);\n \tcolor(6);\n \tprintf("/^/^/");\n \t\n \tgotoxy(hout,34,2);\n \tprintf("|| o|");\n \t\n \tgotoxy(hout,33,2);\n \tcolor(2);\n \tprintf("");\n \t\n \tgotoxy(hout,25,3);\n \tcolor(12);\n \tprintf("\/");\n \t\n \tgotoxy(hout,31,3);\n \tcolor(2);\n \tprintf("/");\n \t\n \tgotoxy(hout,37,3);\n \tcolor(6);\n \tprintf("\/");\n \t\n \tgotoxy(hout,41,3);\n \tcolor(10);\n \tprintf(" \ ");\n \t\n \tgotoxy(hout,26,4);\n \tcolor(12);\n \tprintf("\");\n \t\n \tgotoxy(hout,32,4);\n \tprintf("");\n \t\n \tgotoxy(hout,31,4);\n \tcolor(2);\n \tprintf("|");\n \t\n \tgotoxy(hout,43,4);\n \tcolor(10);\n \tprintf("\ ");\n \t\n \tgotoxy(hout,32,5);\n \tcolor(2);\n \tprintf("\");\n \t\n \tgotoxy(hout,44,5);\n \tcolor(10);\n \tprintf("\ ");\n \t\n \tgotoxy(hout,39,6);\n \tprintf("| | \");\n \t\n \tgotoxy(hout,38,7);\n \tprintf("/ / \");\n \t\n \tgotoxy(hout,37,8);\n \tprintf("/ / \ \");\n \t\n \tgotoxy(hout,35,9);\n \tprintf("/ / \ \");\n \t\n \tgotoxy(hout,34,10);\n \tprintf(" / / \ \");\n \t\n \tgotoxy(hout,33,11);\n \tprintf("/ / ---- \ \");\n \t\n \tgotoxy(hout,32,12);\n \tprintf("/ / -~ ~- | |");\n \t\n \tgotoxy(hout,31,13);\n \tprintf("( ( -~ -- ~- / |");\n \t\n \tgotoxy(hout,32,14);\n \tprintf("\ ~--~ -~ ~- ~--~ /");\n \t\n \tgotoxy(hout,33,15);\n \tprintf("~- -~ ~- -~");\n \t\n \tgotoxy(hout,35,16);\n \tprintf("~---~ ~--~"); \n \n \tcolor(9);\n gotoxy(hout,43,18);\n\tcolor(11);\n\tcout << "贪吃蛇大作战";\n\tcolor(14);\n\tfor(i=20;i<=26;i++){\n\t for(j=27;j<=74;j++)\n\t\t{\n\t \tgotoxy(hout,j,i);\n\t \tif(i==20||i==26)\n\t\t{\n\t \t\tcout << "-";\n\t\t\t}\n\t\t\telse if(j==27||j==74)\n\t\t\t{\n\t\t\t\tcout << "|";\n\t\t\t}\n\t\t}\n\t}\n\tcolor(12);\n\tgotoxy(hout,35,22);\n\tcout << "1.开始游戏";\n\tgotoxy(hout,55,22);\n\tcout << "2.游戏说明";\n\tgotoxy(hout,35,24);\n\tcout << "3.退出游戏";\n\tcolor(9);\n\tgotoxy(hout,framex+wide2,framey+19);\n cout << "姓名:C++小将"; \n gotoxy(hout,framex+wide2,framey+21);\n cout << "来自:可达信奥"; \n gotoxy(hout,framex+wide2,framey+23);\n cout << "完成时间:2023年02月09日";\n gotoxy(hout,29,27);\n\tcolor(3);\n\tprintf("做出你的选择:");\n color(14);\n\tint n;\n\tscanf("%d",&n);\n\tswitch(n){\n\t case 1: \n\t \tsystem("cls");//清屏 \n\t\t break;\n\t case 2:\n\t \t explation(hout);\n\t \t break;\n\t case 3:\n\t \t exit(0);\n\t break;\n\t}\n}\n//定义蛇的结构体 \nstruct Snake{\n int x[100];\n int y[100];\n int speed;\n int length;\n int count;\n int tm1;//开始时间 \n int tm2;//持续时间 \n};\n//定义食物的结构体 \nstruct Food{\n int x;\n int y;\n};\n//制作框架 \nvoid makeframe(struct Snake snake){\n //定义显示器变量句柄 \n HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);\n gotoxy(hout,framex+wide2+5,framey);\n cout << " 贪吃蛇游戏"; \n gotoxy(hout,framex+wide2+5,framey+3);\n cout << "使用方向键或wasd移动";\n gotoxy(hout,framex+wide2+5,framey+5);\n cout << "长按方向键可加速";\n gotoxy(hout,framex+wide2+5,framey+7);\n cout << "按任意键暂停,方向键继续";\n //打印上边框 \n for(i=0;i<wide2+1;i++){\n gotoxy(hout,framex+i,framey);\n cout << "";\n }\n //打印下边框 \n for(i=0;i<wide2+2;i++){\n gotoxy(hout,framex+i,framey+high);\n cout << "";\n }\n //打印左边框 \n for(i=0;i<high;i++){\n gotoxy(hout,framex,framey+i);\n cout << "";\n }\n //打印右边框 \n for(i=0;i<high;i++){\n gotoxy(hout,framex+wide2+1,framey+i);\n cout << "";\n }\n}\n//游戏信息\nvoid infor(HANDLE hout,struct Snake* snake){\n gotoxy(hout,framex+wide2+5,framey+15);\n printf("当前速度:%d",-snake->speed+500);\n gotoxy(hout,framex+wide2+5,framey+17);\n printf("当前得分:%d",snake->count);\n gotoxy(hout,framex+wide2+5,framey+19);\n printf("当前长度:%d",snake->length);\n gotoxy(hout,framex+wide2+5,framey+21);\n snake->tm2=time(NULL);\n snake->tm2-=snake->tm1;\n printf("蛇已经活了:%d秒",snake->tm2);\n}\n//初始化蛇 \nvoid initsnake(struct Snake *snake){\n snake->x[0]=framex+2;\n snake->y[0]=framey+high/2;\n snake->count=0;\n snake->length=3;\n snake->speed=400;\n snake->tm1=time(NULL);\n\t \n //初始化由蛇尾至蛇头的坐标 \n for(i=1;i
原文地址: https://www.cveoy.top/t/topic/p9Iw 著作权归作者所有。请勿转载和采集!