每天一個(gè)C語(yǔ)言小項(xiàng)目,提升你的編程能力!
迷宮游戲其實(shí)有很多種類型,比如說(shuō)營(yíng)救公主的一款,又比如說(shuō)闖關(guān)的一款,其他的一些種類這里就不一一列舉了。但是不管哪一款迷宮游戲,唯一不變的就是迷宮的實(shí)現(xiàn)這一核心功能,每一款迷宮游戲無(wú)非就是根據(jù)自己的游戲性質(zhì)在迷宮之中添加一些必要的元素罷了。
我們今天的主要目標(biāo)就是用C語(yǔ)言來(lái)實(shí)現(xiàn)迷宮的原本功能,純天然無(wú)污染,沒(méi)有添加任何防腐劑(因?yàn)闆](méi)有其他的花里胡哨的功能)
上面就是咱們的效果圖預(yù)覽,以圖形庫(kù)easyX來(lái)搭配C語(yǔ)言語(yǔ)法實(shí)現(xiàn)的迷宮界面,然后也實(shí)現(xiàn)了例如一些按鍵操作,邊界檢測(cè)這樣的功能,整體來(lái)說(shuō)還是比較完善的。
本項(xiàng)目編譯環(huán)境:Visual Studio 2019/2022,EasyX插件
代碼展示:
#include#include #include ////////////////////////////////////////////////////// // 定義全局變量 // BYTE** g_aryMap = NULL; // 迷宮地圖 SIZE g_szMap; // 迷宮地圖的尺寸 IMAGE g_imgSight(360, 280); // 游戲的視野 RECT g_rtSight; // 游戲的視野的范圍 IMAGE g_imgItem(180, 20); // 地圖元素 IMAGE g_imgGPS; // 迷你地圖,用于顯示游戲者在地圖中的位置 POINT g_ptGPS; // 迷你地圖的顯示位置 SIZE g_szGPS; // 迷你地圖的尺寸 POINT g_ptPlayer; // 游戲者的位置 // 枚舉地圖元素,兼做元素位置的 x 坐標(biāo) enum MAPITEM { MAP_WALL = 0, MAP_PLAYER = 20, MAP_GROUND = 40, MAP_MARKRED = 60, MAP_MARKGREEN = 80, MAP_MARKYELLOW = 100, MAP_ENTRANCE = 120, MAP_EXIT = 140, MAP_OUTSIDE = 160 }; // 枚舉用戶的控制命令 enum CMD { CMD_QUIT = 1, CMD_UP = 2, CMD_DOWN = 4, CMD_LEFT = 8, CMD_RIGHT = 16, CMD_MARKRED = 32, CMD_MARKGREEN = 64, CMD_MARKYELLOW = 128, CMD_CLEARMARK = 256 }; ////////////////////////////////////////////////////// // 函數(shù)聲明 // void Welcome(); // 繪制游戲界面 void InitImage(); // 初始化游戲圖片 void InitGame(); // 初始化游戲數(shù)據(jù) void GetMazeSize(); // 提示用戶輸入迷宮大小 void MakeMaze(int width, int height); // 生成迷宮:初始化(注:寬高必須是奇數(shù)) void TravelMaze(int x, int y); // 生成迷宮:遍歷 (x, y) 四周 MAPITEM GetMazeItem(int x, int y); // 獲取指定坐標(biāo)的迷宮元素 void Paint(); // 繪制視野范圍內(nèi)的迷宮 int GetCmd(); // 獲取用戶輸入的命令 void DispatchCmd(int cmd); // 處理用戶輸入的命令 void OnUp(); // 向上移動(dòng) void OnLeft(); // 向左移動(dòng) void OnRight(); // 向右移動(dòng) void OnDown(); // 向下移動(dòng) void OnMark(MAPITEM value); // 在地圖中做標(biāo)記 bool CheckWin(); // 檢查是否到出口 bool Quit(); // 詢問(wèn)用戶是否退出游戲 ////////////////////////////////////////////////////// // 函數(shù)定義 // // 主程序 void main() { // 初始化 initgraph(640, 480); // 創(chuàng)建繪圖窗口 srand((unsigned)time(NULL)); // 設(shè)置隨機(jī)種子 // 顯示主界面 Welcome(); // 初始化 InitImage(); InitGame(); // 游戲過(guò)程 int c; while( !(((c = GetCmd()) & CMD_QUIT) && Quit()) ) { DispatchCmd(c); Paint(); if (CheckWin()) break; // 延時(shí) Sleep(100); } // 清理迷宮地圖占用的內(nèi)存 for(int x = 0; x < g_szMap.cx + 2; x++) delete[] g_aryMap[x]; delete [] g_aryMap; // 關(guān)閉圖形模式 closegraph(); } // 繪制游戲界面 void Welcome() { // 繪制漸變色外框 for(int i=0; i<128; i++) { setlinecolor(RGB(0, 0, (127 - i) << 1)); rectangle(149 - i, 109 - (i >> 1), 490 + i, 370 + (i >> 1)); } // 設(shè)置字體樣式 settextcolor(WHITE); setbkmode(TRANSPARENT); // 繪制標(biāo)題 settextstyle(36, 0, _T("宋體")); outtextxy(248, 40, _T("迷宮")); // 繪制操作說(shuō)明 settextstyle(12, 0, _T("宋體")); outtextxy(50, 382, _T("控制說(shuō)明:")); outtextxy(74, 400, _T("方向鍵或 A/S/D/W:移動(dòng)")); outtextxy(74, 418, _T("空格、Y、G:在地圖上做紅、黃、綠色 M 標(biāo)記")); outtextxy(74, 436, _T("C:清除地圖上的標(biāo)記")); outtextxy(74, 454, _T("ESC:退出程序")); } // 初始化游戲圖片 void InitImage() { // 預(yù)繪制游戲圖片到 IMAGE 緩存(可以修改為加載圖片以獲得更好效果) SetWorkingImage(&g_imgItem); cleardevice(); // 繪制 PLAYER setorigin(MAP_PLAYER, 0); setfillcolor(YELLOW); setlinecolor(YELLOW); fillellipse(2, 2, 17, 17); setlinecolor(BLACK); line(7, 7, 7, 8); line(12, 7, 12, 8); arc(5, 6, 14, 14, 3.34, 6.08); // 繪制墻壁 setorigin(MAP_WALL, 0); settextcolor(BROWN); setfillstyle((BYTE*)"x20x20x20xffx04x04x04xff"); setlinecolor(BROWN); solidrectangle(1, 1, 18, 18); rectangle(0, 0, 19, 19); // 繪制紅色標(biāo)記 setorigin(MAP_MARKRED, 0); setlinecolor(RED); moveto(5, 15); linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10); // 繪制綠色標(biāo)記 setorigin(MAP_MARKGREEN, 0); setlinecolor(GREEN); moveto(5, 15); linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10); // 繪制黃色標(biāo)記 setorigin(MAP_MARKYELLOW, 0); setlinecolor(YELLOW); moveto(5, 15); linerel(0, -10); linerel(5, 5); linerel(5, -5); linerel(0, 10); // 繪制入口 setorigin(MAP_ENTRANCE, 0); setlinecolor(GREEN); settextstyle(12, 0, _T("宋體")); outtextxy(4, 4, _T("入")); // 繪制出口 setorigin(MAP_EXIT, 0); outtextxy(4, 4, _T("出")); // 繪制迷宮外面的空地 setorigin(MAP_OUTSIDE, 0); settextcolor(GREEN); setfillstyle((BYTE*)"x50x55x22x20x05x55x22x02"); solidrectangle(0, 0, 19, 19); // 恢復(fù)坐標(biāo)系 setorigin(0, 0); // 顯示作者 SetWorkingImage(); settextcolor(BLUE); TCHAR author[] = _T("Powered by zhaoh1987@qq.com"); outtextxy(471, 4, author); settextcolor(LIGHTBLUE); outtextxy(470, 3, author); } // 初始化游戲數(shù)據(jù) void InitGame() { // 提示用戶輸入迷宮大小 GetMazeSize(); // 初始化參數(shù) if (g_aryMap != NULL) { // 清理迷宮地圖占用的內(nèi)存 for(int x = 0; x < g_szMap.cx + 2; x++) delete[] g_aryMap[x]; delete [] g_aryMap; } MakeMaze(g_szMap.cx, g_szMap.cy); // 創(chuàng)建迷宮 g_ptPlayer.x = 2; // 設(shè)置游戲者的位置 g_ptPlayer.y = 2; g_rtSight.left = 0; // 設(shè)置視野范圍 g_rtSight.top = 0; g_rtSight.right = 17; g_rtSight.bottom= 13; // 設(shè)置 GPS 顯示區(qū) setfillcolor(BLUE); solidrectangle(522, 368, 637, 471); if (g_szMap.cx > g_szMap.cy) { g_szGPS.cx = 100; g_szGPS.cy = (int)(100.0 * g_szMap.cy / g_szMap.cx + 0.5); } else { g_szGPS.cy = 100; g_szGPS.cx = (int)(100.0 * g_szMap.cx / g_szMap.cy + 0.5); } Resize(&g_imgGPS, g_szGPS.cx, g_szGPS.cy); g_ptGPS.x = 530 + 50 - g_szGPS.cx / 2; g_ptGPS.y = 370 + 50 - g_szGPS.cy / 2; // 畫(huà)迷你地圖外框 setlinecolor(RED); rectangle(g_ptGPS.x - 1, g_ptGPS.y - 1, g_ptGPS.x + g_szGPS.cx, g_ptGPS.y + g_szGPS.cy); // 畫(huà)迷你地圖入口和出口 setlinecolor(YELLOW); moveto(g_ptGPS.x - 8, g_ptGPS.y + g_szGPS.cy / g_szMap.cy); linerel(7, 0); linerel(-3, -3); moverel(3, 3); linerel(-3, 3); moveto(g_ptGPS.x + g_szGPS.cx, g_ptGPS.y + g_szGPS.cy - g_szGPS.cy / g_szMap.cy); linerel(7, 0); linerel(-3, -3); moverel(3, 3); linerel(-3, 3); // 繪制游戲區(qū) Paint(); } // 提示用戶輸入迷宮大小 void GetMazeSize() { g_szMap.cx = g_szMap.cy = 0; // 獲取用戶輸入的寬高 TCHAR s[4]; while(g_szMap.cx < 20 || g_szMap.cx > 200) { InputBox(s, 4, _T("請(qǐng)輸入迷宮的寬度 范圍:20~200"), _T("輸入"), _T("25")); g_szMap.cx = _ttoi(s); } while(g_szMap.cy < 20 || g_szMap.cx > 200) { InputBox(s, 4, _T("請(qǐng)輸入迷宮的高度 范圍:20~200"), _T("輸入"), _T("25")); g_szMap.cy = _ttoi(s); } // 確保寬高為奇數(shù) if (g_szMap.cx % 2 != 1) g_szMap.cx++; if (g_szMap.cy % 2 != 1) g_szMap.cy++; } // 生成迷宮:初始化(注:寬高必須是奇數(shù)) void MakeMaze(int width, int height) { if (width % 2 != 1 || height % 2 != 1) return; int x, y; // 定義迷宮尺寸,并分配迷宮內(nèi)存 g_aryMap = new BYTE*[width + 2]; for(x = 0; x < width + 2; x++) { g_aryMap[x] = new BYTE[height + 2]; memset(g_aryMap[x], MAP_WALL, height + 2); } // 定義邊界 for (x = 0; x <= width + 1; x++) g_aryMap[x][0] = g_aryMap[x][height + 1] = MAP_GROUND; for (y = 1; y <= height; y++) g_aryMap[0][y] = g_aryMap[width + 1][y] = MAP_GROUND; // 定義入口和出口 g_aryMap[1][2] = MAP_ENTRANCE; g_aryMap[width][height - 1] = MAP_EXIT; // 從任意點(diǎn)開(kāi)始遍歷生成迷宮 TravelMaze(((rand() % (width - 1)) & 0xfffe) + 2, ((rand() % (height - 1)) & 0xfffe) + 2); // 將邊界標(biāo)記為迷宮外 for (x = 0; x <= width + 1; x++) g_aryMap[x][0] = g_aryMap[x][height + 1] = MAP_OUTSIDE; for (y = 1; y <= height; y++) g_aryMap[0][y] = g_aryMap[width + 1][y] = MAP_OUTSIDE; } // 生成迷宮:遍歷 (x, y) 四周 void TravelMaze(int x, int y) { // 定義遍歷方向 int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; // 將遍歷方向亂序 int n, t, i; for(i = 0; i < 4; i++) { n = rand() % 4; t = d[i][0], d[i][0] = d[n][0], d[n][0] = t; t = d[i][1], d[i][1] = d[n][1], d[n][1] = t; } // 嘗試周圍四個(gè)方向 g_aryMap[x][y] = MAP_GROUND; for(i = 0; i < 4; i++) if (g_aryMap[x + 2 * d[i][0]][y + 2 * d[i][1]] == MAP_WALL) { g_aryMap[x + d[i][0]][y + d[i][1]] = MAP_GROUND; TravelMaze(x + d[i][0] * 2, y + d[i][1] * 2); // 遞歸 } } // 獲取指定坐標(biāo)的迷宮元素 MAPITEM GetMazeItem(int x, int y) { return (MAPITEM)g_aryMap[x][y]; } // 繪制視野范圍內(nèi)的迷宮 void Paint() { int x1, y1; // 繪制視野內(nèi)的迷宮 SetWorkingImage(&g_imgSight); for(int x = g_rtSight.left; x <= g_rtSight.right; x++) for(int y = g_rtSight.top; y <= g_rtSight.bottom; y++) { x1 = (x - g_rtSight.left) * 20; y1 = (y - g_rtSight.top) * 20; putimage(x1, y1, 20, 20, &g_imgItem, GetMazeItem(x, y), 0); } // 繪制游戲者 x1 = (g_ptPlayer.x - g_rtSight.left) * 20; y1 = (g_ptPlayer.y - g_rtSight.top) * 20; putimage(x1, y1, 20, 20, &g_imgItem, MAP_PLAYER, 0); // 繪制迷你地圖 SetWorkingImage(&g_imgGPS); cleardevice(); int tx = (int)((g_ptPlayer.x - 1) * g_szGPS.cx / (double)(g_szMap.cx - 1) + 0.5); int ty = (int)((g_ptPlayer.y - 1) * g_szGPS.cy / (double)(g_szMap.cy - 1) + 0.5); setlinecolor(YELLOW); circle(tx, ty, 1); // 更新到繪圖窗口 SetWorkingImage(); putimage(150, 110, 340, 260, &g_imgSight, 10, 10); putimage(g_ptGPS.x, g_ptGPS.y, &g_imgGPS); } // 獲取用戶輸入的命令 int GetCmd() { int c = 0; if (GetAsyncKeyState(VK_LEFT) & 0x8000) c |= CMD_LEFT; if (GetAsyncKeyState(VK_RIGHT) & 0x8000) c |= CMD_RIGHT; if (GetAsyncKeyState(VK_UP) & 0x8000) c |= CMD_UP; if (GetAsyncKeyState(VK_DOWN) & 0x8000) c |= CMD_DOWN; if (GetAsyncKeyState('A') & 0x8000) c |= CMD_LEFT; if (GetAsyncKeyState('D') & 0x8000) c |= CMD_RIGHT; if (GetAsyncKeyState('W') & 0x8000) c |= CMD_UP; if (GetAsyncKeyState('S') & 0x8000) c |= CMD_DOWN; if (GetAsyncKeyState(' ') & 0x8000) c |= CMD_MARKRED; if (GetAsyncKeyState('G') & 0x8000) c |= CMD_MARKGREEN; if (GetAsyncKeyState('Y') & 0x8000) c |= CMD_MARKYELLOW; if (GetAsyncKeyState('C') & 0x8000) c |= CMD_CLEARMARK; if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) c |= CMD_QUIT; return c; } // 處理用戶輸入的命令 void DispatchCmd(int cmd) { if (cmd & CMD_UP) OnUp(); if (cmd & CMD_DOWN) OnDown(); if (cmd & CMD_LEFT) OnLeft(); if (cmd & CMD_RIGHT) OnRight(); if (cmd & CMD_MARKRED) OnMark(MAP_MARKRED); if (cmd & CMD_MARKGREEN) OnMark(MAP_MARKGREEN); if (cmd & CMD_MARKYELLOW) OnMark(MAP_MARKYELLOW); if (cmd & CMD_CLEARMARK) OnMark(MAP_GROUND); } // 向上移動(dòng) void OnUp() { if (g_ptPlayer.y > 1 && GetMazeItem(g_ptPlayer.x, g_ptPlayer.y - 1) != MAP_WALL) { g_ptPlayer.y--; if (g_ptPlayer.y - g_rtSight.top < 4 && g_rtSight.top > 0) { g_rtSight.top--; g_rtSight.bottom--; } } } // 向左移動(dòng) void OnLeft() { if (g_ptPlayer.x > 1 && GetMazeItem(g_ptPlayer.x - 1, g_ptPlayer.y) != MAP_WALL && GetMazeItem(g_ptPlayer.x - 1, g_ptPlayer.y) != MAP_ENTRANCE) { g_ptPlayer.x--; if (g_ptPlayer.x - g_rtSight.left < 5 && g_rtSight.left > 0) { g_rtSight.left--; g_rtSight.right--; } } } // 向右移動(dòng) void OnRight() { if (g_ptPlayer.x < g_szMap.cx && GetMazeItem(g_ptPlayer.x + 1, g_ptPlayer.y) != MAP_WALL) { g_ptPlayer.x++; if (g_rtSight.right - g_ptPlayer.x < 5 && g_rtSight.right <= g_szMap.cx) { g_rtSight.left++; g_rtSight.right++; } } } // 向下移動(dòng) void OnDown() { if (g_ptPlayer.y < g_szMap.cy && GetMazeItem(g_ptPlayer.x, g_ptPlayer.y + 1) != MAP_WALL) { g_ptPlayer.y++; if (g_rtSight.bottom - g_ptPlayer.y < 4 && g_rtSight.bottom <= g_szMap.cy) { g_rtSight.top++; g_rtSight.bottom++; } } } // 在地圖中做標(biāo)記 void OnMark(MAPITEM value) { g_aryMap[g_ptPlayer.x][g_ptPlayer.y] = value; } // 檢查是否到出口 bool CheckWin() { if (g_ptPlayer.x == g_szMap.cx && g_ptPlayer.y == g_szMap.cy - 1) { HWND hwnd = GetHWnd(); if (MessageBox(hwnd, _T("恭喜你走出來(lái)了! 您想再來(lái)一局嗎?"), _T("恭喜"), MB_YESNO | MB_ICONQUESTION) == IDYES) { InitGame(); return false; } else return true; } return false; } // 詢問(wèn)用戶是否退出游戲 bool Quit() { HWND hwnd = GetHWnd(); return (MessageBox(hwnd, _T("您確定要退出游戲嗎?"), _T("詢問(wèn)"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK); }
大家趕緊去動(dòng)手試試吧!
審核編輯:湯梓紅
-
游戲
+關(guān)注
關(guān)注
2文章
742瀏覽量
26312 -
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7604瀏覽量
136692 -
編程
+關(guān)注
關(guān)注
88文章
3614瀏覽量
93686 -
源碼
+關(guān)注
關(guān)注
8文章
639瀏覽量
29185
原文標(biāo)題:C語(yǔ)言零基礎(chǔ)項(xiàng)目:迷宮游戲!詳細(xì)思路+源碼分享
文章出處:【微信號(hào):cyuyanxuexi,微信公眾號(hào):C語(yǔ)言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論