正文
增加 37 行删除 37 行
12 行未改动1314151617181920212223242526272829303136 行未改动68697071727374757677787980818283848586878889909192933 行未改动9798991001011021031041051061071081091102 行未改动11311411511611711811912012112212312412512621 行未改动14814915015115215315415515615715815916016116216316416516616716816917017125 行未改动19719819920020120220320420511 行未改动21721821922022122222322422518 行未改动24424524624724824925025125225325425525625725825910 行未改动2702712722732742752762772782 行未改动28128228328428528628728828929029129229329426 行未改动3213223233243253263273283293303313323333343353363373383393403413423436 行未改动35035135235335435535635735812 行未改动37137237337437537637737837918 行未改动3983994004014024034044054064 行未改动4114124134144154164174184194204214224234244254264274284294304314324334344 行未改动43944044144244344444544644744844945045145245345445545645716 行未改动47447547647747847948012 行未改动public:int x, y;Node(int x1, int y1);};class UserData {public:string name;long long score;int gt;int gr;UserData(string s, long long sc,int gametime,int grade);friend bool operator < (UserData a, UserData b);};#define RIGHT 0x4d#define LEFT 0x4b#define UP 0x48#define DOWN 0x5036 行未改动bool GameMenu(); //游戏菜单void ShowRanking(); //排行榜void ShowAbout(); //相关信息void InputData(); //输入玩家名字int main() {while (true) {if (!GameMenu()) return 0;}return 0;}Node::Node(int x1, int y1) { //构造Node对象x = x1; y = y1;}int SuiJi(){srand((unsigned)time(NULL));return (rand()*rand()+rand()*rand())%14;}bool operator < (UserData a, UserData b) { //重载运算符,按分数由大到小排列if(a.score != b.score)return a.score > b.score;if(a.gt !=b.gt)3 行未改动}UserData::UserData(string s, long long sc,int gametime_,int _grade) { //构造UserData对象name = s; score = sc; gt=gametime_; gr=_grade;}void color(WORD A){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), A);}void Color(int a){switch (a%4){2 行未改动case 2:color(YELLOW);break;case 3:color(PURPLE);break;}}void GoTo(short x, short y) { //定位光标COORD coord = { x, y };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}void ShowInformation() { //输出游戏信息color(YELLOW);GoTo(78, 5);printf("贪吃蛇游戏");21 行未改动GoTo(78,25);printf("剩余时间:%d秒",20+(snakeLen-3)*5-gametime);sysj=20+(snakeLen-3)*5-gametime;}void DrawBoard() { //绘制墙体HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获得输出句柄CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; //光标信息SetConsoleCursorInfo(hOut, &cursor_info); //隐藏光标COORD size = { 120, 30 };SetConsoleScreenBufferSize(hOut, size); //重设缓冲区大小SMALL_RECT rc = { 0 , 0, 120, 30 };SetConsoleWindowInfo(hOut, true, &rc); //重设窗口大小SetConsoleTextAttribute(hOut, CYAN);for (int i = STARTX - 2; i <= ENDX + 2; i += 2) { //横向墙体GoTo(i, STARTY - 1);printf("■");GoTo(i, ENDY + 1);25 行未改动mj=clock();point=20;sysj=20;}void RunSnake(int x, int y) { //绘制蛇身SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);score += snakeLen + 1;if (x == foodx && y == foody) {11 行未改动isSnake[back.y][back.x] = false;GoTo(back.x, back.y);printf(" ");}void Try(int& x, int& y) { //试走int key, cnt = 100;while (cnt--) { //多次检测键盘状态if (_kbhit()) {18 行未改动else if (curDiraction == 2) ++y;else if (curDiraction == 3) x -= 2;else x += 2;}bool IsGoodCoord(int x, int y) { //判断光标是否合法if (x <= ENDX && y <= ENDY && x >= STARTX && y >= STARTY)return true;elsereturn false;}void AddFood() { //增加食物SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), PURPLE);srand((unsigned)time(NULL));while (true) {10 行未改动printf("#");elseprintf("&");}void EartFood() { //吃东西point+=4;int sb=gametime=(clock()-mj)/1000;sysj=point-sb;2 行未改动snake.push_front(Node(foodx, foody));isSnake[foody][foodx] = true;++snakeLen;if (speed >= 55) speed -= 5;GoTo(foodx, foody);draw();AddFood();}void InitSnake() { //初始化蛇身SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);snakeLen = 3, gox = 18, goy = 14;snake.clear();26 行未改动}else key = getch();}}bool StartGame() { //启动游戏Init();while (sysj>0) { //开挂RunSnake(gox, goy);ShowInformation();Try(gox, goy);Sleep(speed);}InputData();return true;}bool GameMenu() { //游戏菜单system("cls");DrawBoard();GoTo(STARTX + 22, STARTY + 4);6 行未改动GoTo(STARTX + 24, STARTY + 14);printf("3: 关于游戏");GoTo(STARTX + 24, STARTY + 16);printf("4: 退出游戏");while (true) {if (_kbhit()) {char key = getch();switch (key) {12 行未改动}}}}void ShowRanking() { //展示排行榜vector<UserData> vu;FILE *fp = fopen("Gamedata2.txt", "r");if (fp == NULL) fp = fopen("Gamedata2.txt", "w+");18 行未改动printf("生存时间");GoTo(STARTX + 44, STARTY + 2);printf("排行");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);for (int i = 0; i < len && i < 10; ++i) { //打印前十名用户数据char const *p = vu[i].name.c_str();Color(score);GoTo(STARTX + 8, STARTY + 4 + i);4 行未改动printf("%d秒", vu[i].gt);GoTo(STARTX + 44, STARTY + 4 + i);printf(" %d", i + 1);}GoTo(STARTX + 4, ENDY - 2);printf("----------------- 按'1'返回游戏菜单 ---------------");while (true) {if (_kbhit()) {char key = getch();if (key == '1') break;}}}void ShowAbout() { //展示游戏相关system("cls");DrawBoard();SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);GoTo(STARTX + 4, STARTY + 2);printf("------------------- 贪吃蛇游戏 -------------------");GoTo(STARTX + 10,STARTY + 8);printf("贪吃蛇游戏");4 行未改动GoTo(STARTX + 10,STARTY + 14);printf("吃的越多,蛇就越长,您的等级也将越高");GoTo(STARTX + 10,STARTY + 16);printf("当蛇吃到自己或撞上墙时,游戏结束。");GoTo(STARTX + 4, ENDY - 2);printf("----------------- 按'1'返回游戏菜单 ---------------");while (true) {if (_kbhit()) {char key = getch();if (key == '1') break;}}}void InputData() { //用户输入名字char name[20];if(score>=1000){16 行未改动cout<<"哟!这分数也能上榜??"<<endl;Sleep(1000);}EndGame();}```12 行未改动1314151617181920212223242526272829303136 行未改动68697071727374757677787980818283848586878889909192933 行未改动9798991001011021031041051061071081091102 行未改动11311411511611711811912012112212312412512621 行未改动14814915015115215315415515615715815916016116216316416516616716816917017125 行未改动19719819920020120220320420511 行未改动21721821922022122222322422518 行未改动24424524624724824925025125225325425525625725825910 行未改动2702712722732742752762772782 行未改动28128228328428528628728828929029129229329426 行未改动3213223233243253263273283293303313323333343353363373383393403413423436 行未改动35035135235335435535635735812 行未改动37137237337437537637737837918 行未改动3983994004014024034044054064 行未改动4114124134144154164174184194204214224234244254264274284294304314324334344 行未改动43944044144244344444544644744844945045145245345445545645716 行未改动47447547647747847948012 行未改动public:int x, y;Node(int x1, int y1);};class UserData {public:string name;long long score;int gt;int gr;UserData(string s, long long sc,int gametime,int grade);friend bool operator < (UserData a, UserData b);};#define RIGHT 0x4d#define LEFT 0x4b#define UP 0x48#define DOWN 0x5036 行未改动bool GameMenu(); //游戏菜单void ShowRanking(); //排行榜void ShowAbout(); //相关信息void InputData(); //输入玩家名字int main() {while (true) {if (!GameMenu()) return 0;}return 0;}Node::Node(int x1, int y1) { //构造Node对象x = x1; y = y1;}int SuiJi(){srand((unsigned)time(NULL));return (rand()*rand()+rand()*rand())%14;}bool operator < (UserData a, UserData b) { //重载运算符,按分数由大到小排列if(a.score != b.score)return a.score > b.score;if(a.gt !=b.gt)3 行未改动}UserData::UserData(string s, long long sc,int gametime_,int _grade) { //构造UserData对象name = s; score = sc; gt=gametime_; gr=_grade;}void color(WORD A){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), A);}void Color(int a){switch (a%4){2 行未改动case 2:color(YELLOW);break;case 3:color(PURPLE);break;}}void GoTo(short x, short y) { //定位光标COORD coord = { x, y };SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}void ShowInformation() { //输出游戏信息color(YELLOW);GoTo(78, 5);printf("贪吃蛇游戏");21 行未改动GoTo(78,25);printf("剩余时间:%d秒",20+(snakeLen-3)*5-gametime);sysj=20+(snakeLen-3)*5-gametime;}void DrawBoard() { //绘制墙体HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获得输出句柄CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; //光标信息SetConsoleCursorInfo(hOut, &cursor_info); //隐藏光标COORD size = { 120, 30 };SetConsoleScreenBufferSize(hOut, size); //重设缓冲区大小SMALL_RECT rc = { 0 , 0, 120, 30 };SetConsoleWindowInfo(hOut, true, &rc); //重设窗口大小SetConsoleTextAttribute(hOut, CYAN);for (int i = STARTX - 2; i <= ENDX + 2; i += 2) { //横向墙体GoTo(i, STARTY - 1);printf("■");GoTo(i, ENDY + 1);25 行未改动mj=clock();point=20;sysj=20;}void RunSnake(int x, int y) { //绘制蛇身SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);score += snakeLen + 1;if (x == foodx && y == foody) {11 行未改动isSnake[back.y][back.x] = false;GoTo(back.x, back.y);printf(" ");}void Try(int& x, int& y) { //试走int key, cnt = 100;while (cnt--) { //多次检测键盘状态if (_kbhit()) {18 行未改动else if (curDiraction == 2) ++y;else if (curDiraction == 3) x -= 2;else x += 2;}bool IsGoodCoord(int x, int y) { //判断光标是否合法if (x <= ENDX && y <= ENDY && x >= STARTX && y >= STARTY)return true;elsereturn false;}void AddFood() { //增加食物SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), PURPLE);srand((unsigned)time(NULL));while (true) {10 行未改动printf("#");elseprintf("&");}void EartFood() { //吃东西point+=4;int sb=gametime=(clock()-mj)/1000;sysj=point-sb;2 行未改动snake.push_front(Node(foodx, foody));isSnake[foody][foodx] = true;++snakeLen;if (speed >= 55) speed -= 5;GoTo(foodx, foody);draw();AddFood();}void InitSnake() { //初始化蛇身SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);snakeLen = 3, gox = 18, goy = 14;snake.clear();26 行未改动}else key = getch();}}bool StartGame() { //启动游戏Init();while (sysj>0) { //开挂RunSnake(gox, goy);ShowInformation();Try(gox, goy);Sleep(speed);}InputData();return true;}bool GameMenu() { //游戏菜单system("cls");DrawBoard();GoTo(STARTX + 22, STARTY + 4);6 行未改动GoTo(STARTX + 24, STARTY + 14);printf("3: 关于游戏");GoTo(STARTX + 24, STARTY + 16);printf("4: 退出游戏");while (true) {if (_kbhit()) {char key = getch();switch (key) {12 行未改动}}}}void ShowRanking() { //展示排行榜vector<UserData> vu;FILE *fp = fopen("Gamedata2.txt", "r");if (fp == NULL) fp = fopen("Gamedata2.txt", "w+");18 行未改动printf("生存时间");GoTo(STARTX + 44, STARTY + 2);printf("排行");SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);for (int i = 0; i < len && i < 10; ++i) { //打印前十名用户数据char const *p = vu[i].name.c_str();Color(score);GoTo(STARTX + 8, STARTY + 4 + i);4 行未改动printf("%d秒", vu[i].gt);GoTo(STARTX + 44, STARTY + 4 + i);printf(" %d", i + 1);}GoTo(STARTX + 4, ENDY - 2);printf("----------------- 按'1'返回游戏菜单 ---------------");while (true) {if (_kbhit()) {char key = getch();if (key == '1') break;}}}void ShowAbout() { //展示游戏相关system("cls");DrawBoard();SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), YELLOW);GoTo(STARTX + 4, STARTY + 2);printf("------------------- 贪吃蛇游戏 -------------------");GoTo(STARTX + 10,STARTY + 8);printf("贪吃蛇游戏");4 行未改动GoTo(STARTX + 10,STARTY + 14);printf("吃的越多,蛇就越长,您的等级也将越高");GoTo(STARTX + 10,STARTY + 16);printf("当蛇吃到自己或撞上墙时,游戏结束。");GoTo(STARTX + 4, ENDY - 2);printf("----------------- 按'1'返回游戏菜单 ---------------");while (true) {if (_kbhit()) {char key = getch();if (key == '1') break;}}}void InputData() { //用户输入名字char name[20];if(score>=1000){16 行未改动cout<<"哟!这分数也能上榜??"<<endl;Sleep(1000);}EndGame();}```