C语言开发‘盲人体验器’【附源码】
程序介绍
这是一款与盲人相关的游戏,虽然不能够完全的模拟出盲人走盲道的各种感受,但是可以模拟出当你不知道路在何方时的内心焦急,恐惧和不知所措。玩这个游戏时需要的是耐心,耐得住孤独,冷静的思考与判断。
玩法介绍
使用上下左右键或者 w s a d 键进行控制盲人的移动。盲人在盲人道上时是安全的。当处在墙上时是危险的,需要返回盲人道。
当盲人正式踏上盲人道时,游戏开始,此时,盲人道会被黑暗遮挡住,一切的信息来源可以通过查看遮挡物的颜色判断。
当遮挡物的颜色为黑色时,说明盲人在盲人道上,当遮挡物的颜色为红色时,说明盲人在墙上,需要盲人返回上一步,重新寻找去向。由于所有的操作过程都不知道盲人的位置。所以就会感觉到盲道特别长,操作不难,只是需要勇气和耐心。
示例图
?
源码

/////////////////////////////////////////////////// // 程序名称:盲人体验器 // 编译环境:Mictosoft Visual Studio 2013, EasyX_20200315(beta) /////////////////////////////////////////////////// #include#include #include struct POS { int x; int y; }; POS point; struct ROAD { int rows; // 代表行 int cells; // 代表列 int IsSafe; // 1 代表安全 0 代表危险 -1 代表死亡 int TheDirecion; // 1 代表竖着 0 代表转折 -1 代表横着 }; ROAD road[32][24]; void initRoad(); // 初始化道路 void drawRoad(); // 绘制道路 void Wall(int x1, int y1, int x2, int y2, int TheDirecoin); // 绘制墙 void BlindRoad(int x1, int y1, int x2, int y2, int TheDirecoin); // 绘制盲人道 void keyDown(); // 方向键 void drawBlind(int x, int y); // 绘制盲人 int main() { initgraph(640, 480); bool IsAgain = true; while (IsAgain) { initRoad(); point.x = 10; point.y = 10; while (true) { BeginBatchDraw(); cleardevice(); if (_kbhit()) keyDown(); drawRoad(); drawBlind(point.x, point.y); int rows = (point.x - 10) / 20; int cells = (point.y - 10) / 20; if (road[rows][cells].IsSafe == -1) { (MessageBox(NULL, L"挑战失败\n重来一局吗?", L"提示", MB_YESNO | MB_ICONQUESTION) == IDYES) ? IsAgain = true : IsAgain = false; break; } if (rows == 31) { (MessageBox(NULL, L"恭喜您成功啦。\n重来一局吗?", L"胜利", MB_YESNO | MB_ICONQUESTION) == IDYES) ? IsAgain = true : IsAgain = false; break; } if (rows > 1 && rows < 30) { setfillcolor(BLACK); if (road[rows][cells].IsSafe == 0) { /*Beep(500, 500); */ // 想闭着眼睛玩可以去掉注释 setfillcolor(RED); } bar(80, 0, 600, 480); } EndBatchDraw(); } } return 0; } void keyDown() { char userKey = _getch(); if (userKey == -32) // 表明这是方向键 userKey = -_getch(); // 获取具体方向,并避免与其他字母的 ASCII 冲突 switch (userKey) { case 'w': case 'W': case -72: point.y -= 20; break; case 's': case 'S': case -80: point.y += 20; break; case 'a': case 'A': case -75: point.x -= 20; break; case 'd': case 'D': case -77: point.x += 20; break; } } void initRoad() { for (int i = 0; i < 32; i++) { for (int j = 0; j < 24; j++) { road[i][j].rows = 10 + i * 20; road[i][j].cells = 10 + j * 20; road[i][j].IsSafe = -1; if (i == 0 || i == 31 || i == 30) { road[i][j].IsSafe = 1; road[i][j].TheDirecion = -1; } } } srand((unsigned int)time(NULL)); int ret = 0; int befret = 0; for (int i = 0; i < 32; i++) { if ((i - 1) % 4 == 0) { ret = rand() % 19 + 2; // 生成一个2到21的随机数 if (i + 4 < 32) { for (int j = i; j <= i + 4; j++) { road[j][ret].IsSafe = 1; road[j][ret].TheDirecion = 1; (road[j][ret + 1].IsSafe == 1) ? 1 : (road[j][ret + 1].IsSafe = 0, road[j][ret + 1].TheDirecion = 1); (road[j][ret - 1].IsSafe == 1) ? 1 : (road[j][ret - 1].IsSafe = 0, road[j][ret - 1].TheDirecion = 1); } } } } for (int i = 2; i < 28; i++) { if ((i - 1) % 4 == 0) { int num = 0; int REMB[3]; for (int j = 0; j < 24; j++) { if (road[i][j].IsSafe == 1) { REMB[num] = j; num++; } } if (num == 1) { continue; } else { for (int j = REMB[0]; j <= REMB[1]; j++) { road[i][j].IsSafe = 1; road[i][j].TheDirecion = -1; if (j == REMB[0] || j == REMB[1]) { road[i][j].TheDirecion = 0; } (road[i - 1][j].IsSafe == 1) ? 1 : (road[i - 1][j].IsSafe = 0, road[i - 1][j].TheDirecion = -1); (road[i + 1][j].IsSafe == 1) ? 1 : (road[i + 1][j].IsSafe = 0, road[i + 1][j].TheDirecion = -1); } } } } } void drawRoad() { for (int i = 0; i < 32; i++) { for (int j = 0; j < 24; j++) { if (road[i][j].IsSafe == 1) { setfillcolor(YELLOW); BlindRoad(road[i][j].rows - 10, road[i][j].cells - 10, road[i][j].rows + 10, road[i][j].cells + 10, road[i][j].TheDirecion); } if (road[i][j].IsSafe == 0) { setfillcolor(RED); Wall(road[i][j].rows - 10, road[i][j].cells - 10, road[i][j].rows + 10, road[i][j].cells + 10, road[i][j].TheDirecion); } if (road[i][j].IsSafe == -1) { float H = 180; // 色相 float S = 1; // 饱和度 float L = 0.7f; // 亮度 L = j*i*0.001f; setfillcolor(HSLtoRGB(H, S, L)); bar(road[i][j].rows - 10, road[i][j].cells - 10, road[i][j].rows + 10, road[i][j].cells + 10); } } } } void drawBlind(int x, int y) { setlinecolor(BLACK); setfillcolor(RGB(233, 217, 191)); fillcircle(x, y, 10); setfillcolor(BLACK); line(x - 5, y, x + 5, y); fillcircle(x - 5, y, 4); fillcircle(x + 5, y, 4); } void Wall(int x1, int y1, int x2, int y2, int TheDirecoin) // 绘制墙 { setfillcolor(RED); bar(x1, y1, x2, y2); int x = (int)((x1 + x2) / 2.0); int y = (int)((y1 + y2) / 2.0); setlinecolor(WHITE); if (TheDirecoin == 1) { line(x, y1, x, y); line(x1, y1, x2, y1); line(x1, y, x1, y2); line(x2, y, x2, y2); line(x1, y, x2, y); line(x1, y2, x2, y2); } else { line(x1, y1, x1, y2); line(x, y1, x, y2); line(x2, y1, x2, y2); line(x, y, x2, y); line(x1, y1, x, y1); line(x1, y2, x, y2); } } void BlindRoad(int x1, int y1, int x2, int y2, int TheDirecoin) // 绘制盲人道 { setlinecolor(BLACK); setfillcolor(YELLOW); fillrectangle(x1, y1, x2, y2); POS pos[3][3]; double dx = (x2 - x1) / 4.0; double dy = (y2 - y1) / 4.0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { pos[i][j].x = (int)(x1 + dx*(j + 1)); pos[i][j].y = (int)(y1 + dy*(i + 1)); } } if (TheDirecoin == 0) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { circle(pos[i][j].x, pos[i][j].y, 1); } } } if (TheDirecoin == -1) { line(pos[0][0].x, pos[0][0].y, pos[2][0].x, pos[2][0].y); line(pos[0][1].x, pos[0][1].y, pos[2][1].x, pos[2][1].y); line(pos[0][2].x, pos[0][2].y, pos[2][2].x, pos[2][2].y); } if (TheDirecoin == 1) { line(pos[0][0].x, pos[0][0].y, pos[0][2].x, pos[0][2].y); line(pos[1][0].x, pos[1][0].y, pos[1][2].x, pos[1][2].y); line(pos[2][0].x, pos[2][0].y, pos[2][2].x, pos[2][2].y); } }
========================
今天的学习就到此结束,如果你想学习更多编程知识,欢迎关注我~ - End - ———————————————— 不管你是转行也好,初学也罢,进阶也可,如果你想学编程,进阶程序员 【值得关注】我的 编程学习交流俱乐部 【点击进入】 ———————————————— 编程入门资料(网盘链接免费分享):