实例介绍
【实例截图】
【核心代码】
#include<iostream> #include <conio.h> #include<windows.h> #include<stdio.h> #include<math.h> #include<string.h> #include <cstdlib> #include <time.h> #include <conio.h> #include <queue> using namespace std; void color(int col) { HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)); SetConsoleTextAttribute(hConsole, col); } inline void shuchu(int y, int x, int p) { HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x x; pos.Y = y; SetConsoleCursorPosition(hOut, pos); if (p == '□') printf(" "); else if (p == '■') printf("■"); else if (p == 2) { color(8); printf("●"); color(7); } else if (p == 3) { color(4); printf("●"); color(7); } } void chushihua(int map[][39]) { int i; for (i = 0; i < 39; i ) { map[0][i] = map[i][0] = map[38][i] = map[i][38] = 0x7fffffff; } int j; for (i = 1; i < 38; i ) for (j = i; j < 38; j ) map[i][j] = map[j][i] = 0; } void xinxi(int y, int x, const char* p) { HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos); printf("%s", p); pos.X = 0; pos.Y = 0; } void xinxi2(int y, int x, int p) { HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos); printf("%d ", p); pos.X = 0; pos.Y = 0; } void showkuangjia() { int i; HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); for (i = 0; i < 39; i ) printf("★"); for (int j = 1; j < 38; j ) { pos.X = 0; pos.Y = j; SetConsoleCursorPosition(hOut, pos); printf("★"); pos.X = 76; pos.Y = j; SetConsoleCursorPosition(hOut, pos); printf("★"); } pos.X = 0; pos.Y = 38; SetConsoleCursorPosition(hOut, pos); for (i = 0; i < 39; i ) printf("★"); xinxi(5, 83, "采蘑菇的白娘子"); xinxi(7, 86, "得分"); xinxi(8, 87, "0 "); xinxi(10, 83, "BY 青春微凉"); } void bimu() { int i, j; HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = 2; for (i = 1; i < 38; i ) { pos.Y = i; SetConsoleCursorPosition(hOut, pos); for (j = 1; j < 38; j ) { printf("■"); Sleep(1); } } } typedef struct { int x, y; }node; void kaimu(int map[][39]) { queue<node>que; int i, j; int dirx[] = { 0,1,1,1,0,-1,-1,-1 }; int diry[] = { -1,-1,0,1,1,1,0,-1 }; map[19][19] = 1; node now, next; now.x = 19; now.y = 19; que.push(now); while (!que.empty()) { now = que.front(); que.pop(); shuchu(now.y, now.x, '□'); Sleep(1); map[now.y][now.x] = 1; for (i = 0; i < 8; i ) { next.x = now.x dirx[i]; next.y = now.y diry[i]; if (map[next.y][next.x] == 0) { map[next.y][next.x] = 1; que.push(next); } } } } void snakelocal(int* y, int* x, int* D, int map[][39]) { srand(time(0)); *y = rand() % 25 5; *x = rand() % 25 5; *D = rand() % 4; map[*y][*x] = 100; shuchu(*y, *x, '■'); if (*D == 2) { map[*y - 1][*x] = 101; shuchu(*y - 1, *x, '■'); } else if (*D == 3) { map[*y][*x 1] = 101; shuchu(*y, *x 1, '■'); } else if (*D == 0) { map[*y 1][*x] = 101; shuchu(*y 1, *x, '■'); } else { map[*y][*x - 1] = 101; shuchu(*y, *x - 1, '■'); } } inline void yidong(int y, int x) { shuchu(y, x, '■'); } void quwei(int map[][39], int y, int x) { shuchu(y, x, '□'); map[y][x] = 1; } void DFS(int map[][39], int y, int x, int num) { int dirx[] = { 0,1,0,-1 }; int diry[] = { -1,0,1,0 }; int i; for (i = 0; i < 4; i ) { if (map[y diry[i]][x dirx[i]] == num) { map[y][x] = num; y = diry[i]; x = dirx[i]; DFS(map, y, x, num 1); return; } } quwei(map, y, x); } void mogu(int map[][39]) { srand(time(0)); int x, y; while (1) { x = rand() % 29 5; y = rand() % 29 5; if (map[y][x] < 100) { map[y][x] = 2; shuchu(y, x, 2); return; } } } void DFS_add(int map[][39], int y, int x, int num) { int dirx[] = { 0,1,0,-1 }; int diry[] = { -1,0,1,0 }; int i; for (i = 0; i < 4; i ) { if (map[y diry[i]][x dirx[i]] == num) { map[y][x] = num; y = diry[i]; x = dirx[i]; DFS_add(map, y, x, num 1); return; } } map[y][x] = num; shuchu(y, x, '■'); } void dumogu(int map[][39]) { int i, j; for (i = 1; i < 39; i ) for (j = 1; j < 39; j ) if (map[i][j] == 3) { map[i][j] = 1; shuchu(i, j, '□'); } srand(time(0)); int x, y, k = 0, tem = rand() % 6 1; while (k < tem) { x = rand() % 29 5; y = rand() % 29 5; if (map[y][x] == 1) { map[y][x] = 3; shuchu(y, x, 3); } } } void kaishi(int map[][39], int Y, int X, int Dir, int ms) { int dirx[] = { 0,1,0,-1 }; int diry[] = { -1,0,1,0 }; int defen = 0; queue<int>anjian; anjian.push(Dir); int kk = ms - '0' 1; ms = -30 * (ms - '0') 300; while (1) { mogu(map); while (map[Y diry[Dir]][X dirx[Dir]] == 1) { if (GetAsyncKeyState(VK_UP)) anjian.push(0); if (GetAsyncKeyState(VK_DOWN)) anjian.push(2); if (GetAsyncKeyState(VK_LEFT)) anjian.push(3); if (GetAsyncKeyState(VK_RIGHT)) anjian.push(1); if (map[Y diry[Dir]][X dirx[Dir]] > 100) break; yidong(Y diry[Dir], X dirx[Dir]); DFS(map, Y, X, 101); map[Y][X] = 101; Y = diry[Dir]; X = dirx[Dir]; map[Y][X] = 100; Sleep(ms); if (!anjian.empty()) { Dir = anjian.front(); anjian.pop(); } } if (map[Y diry[Dir]][X dirx[Dir]] == 2) { map[Y diry[Dir]][X dirx[Dir]] = 100; shuchu(Y diry[Dir], X dirx[Dir], '■'); DFS_add(map, Y, X, 101); map[Y][X] = 101; Y = diry[Dir]; X = dirx[Dir]; dumogu(map); defen = defen 5 * kk; xinxi2(8, 87, defen); } else { HANDLE hOut; COORD pos = { 0, 0 }; hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = 34; pos.Y = 15; SetConsoleCursorPosition(hOut, pos); color(4); printf("Game Over"); if (map[Y diry[Dir]][X dirx[Dir]] == 3) { pos.X = 34; pos.Y = 16; SetConsoleCursorPosition(hOut, pos); printf("GAME OVER!"); } else if (map[Y diry[Dir]][X dirx[Dir]] == 0x7fffffff) { pos.X = 34; pos.Y = 16; SetConsoleCursorPosition(hOut, pos); printf("GAME OVER!"); } else if (map[Y diry[Dir]][X dirx[Dir]] == 101) { pos.X = 34; pos.Y = 16; SetConsoleCursorPosition(hOut, pos); printf("GAME OVER!"); } else { pos.X = 34; pos.Y = 16; SetConsoleCursorPosition(hOut, pos); printf("GAME OVER!"); } color(15); Sleep(1000); pos.X = 0; pos.Y = 0; SetConsoleCursorPosition(hOut, pos); return; } } } int main() { int map[39][39]; int ms; while (1) { chushihua(map); showkuangjia(); bimu(); xinxi(5, 20, "红蘑菇是有毒的,千万不要碰哦!"); xinxi(7, 25, "请输入难度级别(0~9):"); while (putchar(ms = getch()), ms<'0' || ms>'9') { xinxi(8, 25, "输入错误,请重新输入:"); } kaimu(map); int X, Y, Dir; snakelocal(&Y, &X, &Dir, map); kaishi(map, Y, X, Dir, ms); } }
标签: 贪吃蛇
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论