C語言開發‘盲人體驗器’【附源碼】

C語言編程學習基地 2022-01-07 19:17:30 阅读数:171

盲人
程序介紹
這是一款與盲人相關的遊戲,雖然不能够完全的模擬出盲人走盲道的各種感受,但是可以模擬出當你不知道路在何方時的內心焦急,恐懼和不知所措。玩這個遊戲時需要的是耐心,耐得住孤獨,冷靜的思考與判斷。
 
玩法介紹
使用上下左右鍵或者 w s a d 鍵進行控制盲人的移動。盲人在盲人道上時是安全的。當處在牆上時是危險的,需要返回盲人道。
當盲人正式踏上盲人道時,遊戲開始,此時,盲人道會被黑暗遮擋住,一切的信息來源可以通過查看遮擋物的顏色判斷。
當遮擋物的顏色為黑色時,說明盲人在盲人道上,當遮擋物的顏色為紅色時,說明盲人在牆上,需要盲人返回上一步,重新尋找去向。由於所有的操作過程都不知道盲人的比特置。所以就會感覺到盲道特別長,操作不難,只是需要勇氣和耐心。
 
示例圖
源碼
///////////////////////////////////////////////////
// 程序名稱:盲人體驗器
// 編譯環境:Mictosoft Visual Studio 2013, EasyX_20200315(beta)
///////////////////////////////////////////////////

#include <graphics.h>
#include <ctime>
#include <conio.h>
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 -
————————————————
不管你是轉行也好,初學也罷,進階也可,如果你想學編程,進階程序員
【值得關注】我的 編程學習交流俱樂部 【點擊進入】
————————————————
編程入門資料(網盤鏈接免費分享):
 
 
版权声明:本文为[C語言編程學習基地]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201071917297179.html