diff --git a/Mine-Game/Mine-Game/function.cpp b/Mine-Game/Mine-Game/function.cpp index 7da051cc2d7d7b351f1ee4d61e8c3c65a1894eef..987d37b473456d65fe277b4f89d5fefff8cc6c82 100644 --- a/Mine-Game/Mine-Game/function.cpp +++ b/Mine-Game/Mine-Game/function.cpp @@ -1,21 +1,24 @@ #include"head.h" -int map[ROW + 2][COL + 2]; -int count = 0; void GameInit() { - srand((unsigned int)time(NULL));//随机数种子 - //赋初值为0 + startTime = clock(); + srand((unsigned int)time(NULL));//闅忔満鏁扮瀛 + map = (int**)malloc((ROW + 2) * sizeof(int*)); + for (int i = 0; i < ROW + 2; i++) { + map[i] = (int*)malloc((COL + 2) * sizeof(int)); + } + //璧嬪垵鍊间负0 for (int i = 0; i < ROW + 2; i++) { for (int j = 0; j < COL + 2; j++) { map[i][j] = 0; } } - //-1表示雷 + //-1琛ㄧず闆 int n = 0; while (n < NUM) { - //随机得到行和列 + //闅忔満寰楀埌琛屽拰鍒 int r = rand() % ROW + 1; int c = rand() % COL + 1; @@ -25,7 +28,7 @@ void GameInit() { } } - //确定无雷区域的数字 + //纭畾鏃犻浄鍖哄煙鐨勬暟瀛 for (int i = 1; i <= ROW; i++) { for (int j = 1; j <= COL; j++) { if (map[i][j] != -1) { @@ -39,37 +42,76 @@ void GameInit() { } } - //简单的加密,变成点击之前的状态 + //绠鍗曠殑鍔犲瘑锛屽彉鎴愮偣鍑讳箣鍓嶇殑鐘舵 for (int i = 1; i < ROW + 1; i++) { for (int j = 0; j < COL + 1; j++) { map[i][j] += 20; } } + +} + +void CustomGameInit(int* settings) { + int row, col, num; + // 鑾峰彇灞忓箷鐨勫搴﹀拰楂樺害 + // 鑾峰彇褰撳墠绐楀彛鐨勫彞鏌 + HWND hwnd = GetHWnd(); + + // 璁$畻灞忓箷涓績浣嶇疆 + int centerX = GetSystemMetrics(SM_CXSCREEN) / 2; + int centerY = GetSystemMetrics(SM_CYSCREEN) / 2; + + // 绐楀彛瀹藉害鍜岄珮搴︼紙鍋囪鍊硷級 + int windowWidth = 300; + int windowHeight = 150; + + // 璁剧疆绐楀彛鐨勬柊浣嶇疆锛屼娇鍏跺眳涓 + SetWindowPos(hwnd, NULL, centerX - windowWidth / 2, centerY - windowHeight / 2, 5, 5, SWP_NOSIZE | SWP_NOZORDER); + + // 寮瑰嚭杈撳叆妗 + wchar_t inputStr[5]; + InputBox(inputStr, 5, L"璇疯緭鍏ュ垪鐨勫ぇ灏:", L"鑷閫夋嫨闅惧害", L"10", 250, 100, true); // x, y 鍙傛暟鐜板湪鍙兘涓嶅啀閲嶈 + col = _wtoi(inputStr); + InputBox(inputStr, 5, L"璇疯緭鍏ヨ鐨勫ぇ灏:", L"鑷閫夋嫨闅惧害", L"10", 250, 100, true); + row = _wtoi(inputStr); + InputBox(inputStr, 5, L"璇疯緭鍏ョ偢寮规暟閲:", L"鑷閫夋嫨闅惧害", L"10", 250, 100, true); + num = _wtoi(inputStr); + + // 楠岃瘉鐢ㄦ埛杈撳叆 + if (row <= 0 || col <= 0 || num <= 0) { + MessageBox(GetHWnd(), L"璇疯緭鍏ユ湁鏁堢殑鏁伴噺锛", L"閿欒", MB_OK); + return; + } + + settings[0] = row; + settings[1] = col; + settings[2] = num; } + int PlayGames() { - //定义一个鼠标消息 + //瀹氫箟涓涓紶鏍囨秷鎭 MOUSEMSG msg = { 0 }; int r, c; while (1) { msg = GetMouseMsg(); switch (msg.uMsg) { - case WM_LBUTTONDOWN://如果获取到左击信息,则翻开区块 - r = msg.x / SIZE + 1;//获取数组下标 + case WM_LBUTTONDOWN://濡傛灉鑾峰彇鍒板乏鍑讳俊鎭紝鍒欑炕寮鍖哄潡 + r = msg.x / SIZE + 1;//鑾峰彇鏁扮粍涓嬫爣 c = msg.y / SIZE + 1; - if (map[r][c] >= 19 && map[r][c] <= 28) {//点到空白 + if (map[r][c] >= 19 && map[r][c] <= 28) {//鐐瑰埌绌虹櫧 if (map[r][c] == 20) { OpenZero(r, c); } else { - map[r][c] -= 20;//解密,显示原来的数字 + map[r][c] -= 20;//瑙e瘑锛屾樉绀哄師鏉ョ殑鏁板瓧 count++; } } return map[r][c]; break; - case WM_RBUTTONDOWN://如果获取到右击信息,则标记区块 + case WM_RBUTTONDOWN://濡傛灉鑾峰彇鍒板彸鍑讳俊鎭紝鍒欐爣璁板尯鍧 r = msg.x / SIZE + 1; c = msg.y / SIZE + 1; if (map[r][c] >= 19 && map[r][c] <= 28) { @@ -85,12 +127,12 @@ int PlayGames() { } void OpenZero(int r, int c) { - //先打开这个区块 + //鍏堟墦寮杩欎釜鍖哄潡 map[r][c] -= 20; count++; for (int m = r - 1; m <= r + 1; m++) { for (int n = c - 1; n <= c + 1; n++) { - //保证遍历游戏区 + //淇濊瘉閬嶅巻娓告垙鍖 if (m >= 1 && m <= ROW && n >= 1 && n <= COL) { if (map[m][n] >= 19 && map[m][n] <= 28) { if (map[m][n] > 20) { @@ -104,4 +146,6 @@ void OpenZero(int r, int c) { } } } -} \ No newline at end of file +} + + diff --git a/Mine-Game/Mine-Game/head.h b/Mine-Game/Mine-Game/head.h index c5a3527aa44a2a5e6d0b48cc0aba102d9b73b915..32075a3c9c90f34170152c7c188e235d1220ab22 100644 --- a/Mine-Game/Mine-Game/head.h +++ b/Mine-Game/Mine-Game/head.h @@ -1,16 +1,28 @@ -#define _CRT_SECURE_NO_WARNINGS_ -#include -#include -#include -#include - -#define ROW 10//行 -#define COL 10//列 -#define NUM 10//雷的数量 -#define SIZE 30//图片大小 - -//绘制二维数组 -void GameDraw();//输出图片,在控制台输出二维数组 -void GameInit();//数组赋初值 -int PlayGames();//开始游戏 -void OpenZero(int r,int c);//递归方式打开周围所有的0 +#define _CRT_SECURE_NO_WARNINGS_ +#include +#include +#include +#include +#include +#include +#include //鎾斁闊充箰鎵鐢ㄧ殑澶存枃浠讹紝鏄郴缁熻嚜甯︾殑 +#pragma comment(lib,"winmm.lib")//澶勭悊鎺ュ彛 +extern int ROW; +extern int COL; +extern int NUM; +extern int** map; +extern int count; +extern int gameTime; // 娓告垙鐢ㄦ椂锛堢锛 +extern clock_t startTime; +extern IMAGE img[12]; + + +#define SIZE 30 +// 鍑芥暟澹版槑 +void GameInit(); +void CustomGameInit(int* settings); +int ShowStartMenu(); +int PlayGames(); +void OpenZero(int r, int c); +void GameDraw(); + diff --git a/Mine-Game/Mine-Game/main.cpp b/Mine-Game/Mine-Game/main.cpp index 0599cc2818cf2527e63ac38abd18188637ecf8c2..02b74ede9baf5a1fe58a686a7a92cc6ba85e6fd7 100644 --- a/Mine-Game/Mine-Game/main.cpp +++ b/Mine-Game/Mine-Game/main.cpp @@ -1,53 +1,36 @@ #include"head.h" +#include +#include +#include +#include +int ShowStartMenu(); +void CustomGameInit(); /************************************** -最外一圈是辅助区,方便布雷,与游戏区无关 +鏈澶栦竴鍦堟槸杈呭姪鍖猴紝鏂逛究甯冮浄锛屼笌娓告垙鍖烘棤鍏 ***************************************/ -extern int map[ROW+2][COL+2]; -/* -* ROW*COL-NUM==count,游戏胜利 -*/ -extern int count;//点开的个数 -IMAGE img[12];//存放12张图片 -int main() { - initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE); - loadimage(&img[0], L"safe0.jpg", SIZE, SIZE);//保存图片0 - loadimage(&img[1], L"safe1.jpg", SIZE, SIZE); - loadimage(&img[2], L"safe2.jpg", SIZE, SIZE); - loadimage(&img[3], L"safe3.jpg", SIZE, SIZE); - loadimage(&img[4], L"safe4.jpg", SIZE, SIZE); - loadimage(&img[5], L"safe5.jpg", SIZE, SIZE); - loadimage(&img[6], L"safe6.jpg", SIZE, SIZE); - loadimage(&img[7], L"safe7.jpg", SIZE, SIZE); - loadimage(&img[8], L"afe8.jpg", SIZE, SIZE);//8 - loadimage(&img[9], L"mine1.jpg", SIZE, SIZE);//雷 - loadimage(&img[10], L"normal.jpg", SIZE, SIZE);//空白 - loadimage(&img[11], L"flag1.jpg", SIZE, SIZE);//标记 - - GameInit(); - while (1) { - GameDraw(); - if (PlayGames() == -1) {//点到地雷,游戏结束 - GameDraw(); - MessageBox(0, L"MISSION FAIL!", L"", MB_OK); - break; - } +int ROW = 10; +int COL = 10; +int NUM = 10; +int** map; +int count; +int gameTime; +clock_t startTime; +IMAGE img[12]; - if (ROW * COL - NUM == count) {//点开所有区块或者找到所有地雷,游戏胜利 - MessageBox(0, L"MISSION SUCCESS!", L"", MB_OK); - } - } - closegraph(); - return 0; -} -//打印数组 +bool gameOver = false; +bool gamePaused = false; + + +//缁樺埗缁忓吀妯″紡甯冨眬 +//鎵撳嵃鏁扮粍 void GameDraw() { for (int i = 1; i <= ROW; i++) { for (int j = 1; j <= COL; j++) { printf("%3d", map[j][i]); if (map[i][j] == -1) { - putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);//雷 + putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);//闆 } else if (map[i][j] >= 0 && map[i][j] <= 8) { putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[map[i][j]]); @@ -56,8 +39,196 @@ void GameDraw() { putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[10]); } else if (map[i][j] > 30) - putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);//标记图片 + putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);//鏍囪鍥剧墖 } printf("\n"); } } + + + +int ShowStartMenu() { + // 缁樺埗鎸夐挳 + int screenWidth = 400; // 澧炲姞灞忓箷瀹藉害 + int screenHeight = 300; // 澧炲姞灞忓箷楂樺害 + int padding = 100; + initgraph(screenWidth, screenHeight); // 璁剧疆鏂扮殑绐楀彛澶у皬 + + // 璁剧疆鑳屾櫙棰滆壊 + setbkcolor(WHITE); + cleardevice(); // 搴旂敤鑳屾櫙棰滆壊 + + // 缁樺埗鎸夐挳锛屼娇鍏舵洿缇庤 + setfillstyle(SOLID_FILL, LIGHTGRAY); + int buttonHeight = 50; // 澧炲姞鎸夐挳鐨勯珮搴 + int buttonWidth = screenWidth - 2 * padding; // 瀹藉害涓哄睆骞曞搴﹀噺鍘讳袱渚х殑闂撮殧 + int buttonSpacing = 20; // 澧炲姞鎸夐挳闂寸殑闂磋窛 + + // 璁$畻鎸夐挳鐨勫瀭鐩翠綅缃 + int startY = (screenHeight - (3 * buttonHeight + 2 * buttonSpacing)) / 2; + + // 绠鍗曟ā寮忔寜閽殑鐭╁舰妗 + bar(padding, startY, padding + buttonWidth, startY + buttonHeight); + + // 鑷畾涔夐毦搴︽寜閽殑鐭╁舰妗 + bar(padding, startY + buttonHeight + buttonSpacing, padding + buttonWidth, startY + 2 * buttonHeight + buttonSpacing); + + // 閫鍑烘父鎴忔寜閽殑鐭╁舰妗 + bar(padding, startY + 2 * (buttonHeight + buttonSpacing), padding + buttonWidth, startY + 3 * buttonHeight + 2 * buttonSpacing); + + // 璁剧疆鏂囨湰鏍峰紡锛岄夋嫨鏇村ソ鐨勫瓧浣撳拰澶у皬 + LOGFONT f; + gettextstyle(&f); + f.lfHeight = 36; // 鏇村ぇ鐨勫瓧浣撳ぇ灏 + _tcscpy_s(f.lfFaceName, _T("寰蒋闆呴粦")); // 閫夋嫨鏇寸編瑙傜殑瀛椾綋 + settextcolor(BLACK); // 璁剧疆鏂囨湰棰滆壊 + settextstyle(&f); + + // 缁樺埗鎸夐挳鏂囨湰锛岃皟鏁翠綅缃娇鍏跺眳涓 + int textYOffset = (buttonHeight - f.lfHeight) / 2; // 璁$畻鏂囨湰鐨勫瀭鐩村亸绉婚噺浠ヤ娇鍏跺瀭鐩村眳涓 + outtextxy(padding + 5, startY + textYOffset, L"1. 绠鍗曟ā寮"); + outtextxy(padding + 5, startY + buttonHeight + buttonSpacing + textYOffset, L"2. 鑷閫夋嫨闅惧害"); + outtextxy(padding + 5, startY + 2 * (buttonHeight + buttonSpacing) + textYOffset, L"3. 閫鍑烘父鎴"); + + // 绛夊緟榧犳爣鐐瑰嚮 + MOUSEMSG m; // 瀹氫箟榧犳爣娑堟伅缁撴瀯浣 + while (true) { + m = GetMouseMsg(); // 鑾峰彇榧犳爣娑堟伅 + if (m.uMsg == WM_LBUTTONDOWN) { + // 妫鏌ョ粡鍏告ā寮忔寜閽偣鍑 + if (m.x >= padding && m.x <= (padding + buttonWidth) && m.y >= startY && m.y <= (startY + buttonHeight)) { + closegraph(); + return 1; + } + // 妫鏌ヨ嚜瀹氫箟妯″紡鎸夐挳鐐瑰嚮 + else if (m.x >= padding && m.x <= (padding + buttonWidth) && m.y >= (startY + buttonHeight + buttonSpacing) && m.y <= (startY + 2 * buttonHeight + buttonSpacing)) { + closegraph(); + return 2; + } + // 妫鏌ラ鍑烘父鎴忔寜閽偣鍑 + else if (m.x >= padding && m.x <= (padding + buttonWidth) && m.y >= (startY + 2 * (buttonHeight + buttonSpacing)) && m.y <= (startY + 3 * buttonHeight + 2 * buttonSpacing)) { + return 3; + } + } + } + closegraph(); +} + +int main() { + initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE); + loadimage(&img[0], L"safe0.jpg", SIZE, SIZE);//淇濆瓨鍥剧墖0 + loadimage(&img[1], L"safe1.jpg", SIZE, SIZE); + loadimage(&img[2], L"safe2.jpg", SIZE, SIZE); + loadimage(&img[3], L"safe3.jpg", SIZE, SIZE); + loadimage(&img[4], L"safe4.jpg", SIZE, SIZE); + loadimage(&img[5], L"safe5.jpg", SIZE, SIZE); + loadimage(&img[6], L"safe6.jpg", SIZE, SIZE); + loadimage(&img[7], L"safe7.jpg", SIZE, SIZE); + loadimage(&img[8], L"afe8.jpg", SIZE, SIZE);//8 + loadimage(&img[9], L"mine1.jpg", SIZE, SIZE);//闆 + loadimage(&img[10], L"normal.jpg", SIZE, SIZE);//绌虹櫧 + loadimage(&img[11], L"flag1.jpg", SIZE, SIZE);//鏍囪 + + int mode = ShowStartMenu(); + int gameSettings[3]; // 鏁扮粍鏉ュ瓨鍌ㄨ銆佸垪鍜岀偢寮规暟閲 + while(1){ + switch (mode) { + case 1: + // Classic Mode + //initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE); + //GameInit(); + + //GameDraw(); + break; + case 2: + // Custom Mode + //closegraph(); + CustomGameInit(gameSettings); + ROW = gameSettings[0]; + COL = gameSettings[1]; + NUM = gameSettings[2]; + break; + case 3: + // Exit Game + return 0; + default: + // Invalid choice handling + return -1; + } + HWND hwnd=initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE); + GameInit(); + mciSendString(L"open ./music/start.mp3 alias bgm", 0, 0, 0); + mciSendString(L"play bgm", 0, 0, 0); + //mciSendString(L"close bgm", 0, 0, 0); + while (!gameOver) { + + GameDraw(); + + if (_kbhit()) + { + char input = _getch(); + + if (input == 'p' || input == 'P') + { + MessageBox(0, L"娓告垙鏆傚仠", L"", MB_OK); + gamePaused = true; + } + else if (input == 'q' || input == 'Q') + { + MessageBox(0, L"閫鍑烘父鎴", L"", MB_OK); + gameOver = true; + } + } + if (!gameOver && !gamePaused) + { + if (PlayGames() == -1) {//鐐瑰埌鍦伴浄锛屾父鎴忕粨鏉 + mciSendString(L"open ./music/boom1.mp3 alias boom", 0, 0, 0); + mciSendString(L"play ./music/boom1.mp3", 0, 0, 0); + //mciSendString(L"close ./music/boom1.mp3", 0, 0, 0); + gameTime = (clock() - startTime) / CLOCKS_PER_SEC; // 璁$畻娓告垙鐢ㄦ椂锛堢锛 + GameDraw(); + char message[100];//灏唃ameTime杞崲涓哄瓧绗﹀瀷鐨勪腑闂村彉閲 + sprintf(message, "寰堥仐鎲句綘杈撲簡锛佹父鎴忕敤鏃讹細%d 绉", gameTime); + MessageBoxA(0, message, "娓告垙缁撴潫", MB_OK); + gameOver = true; + break; + } + + if (ROW * COL - NUM == count) {//鐐瑰紑鎵鏈夊尯鍧楁垨鑰呮壘鍒版墍鏈夊湴闆凤紝娓告垙鑳滃埄 + gameTime = (clock() - startTime) / CLOCKS_PER_SEC; // 璁$畻娓告垙鐢ㄦ椂锛堢锛 + GameDraw(); + char message[100]; + sprintf(message, "鎭枩浣犺耽浜嗭紒娓告垙鐢ㄦ椂锛%d 绉", gameTime); + MessageBoxA(0, message, "娓告垙缁撴潫", MB_OK); + gameOver = true; + } + }else { + // 娓告垙鏆傚仠鏃剁殑寰幆 + char input = _getch(); + if (input == 'r' || input == 'R') + { + MessageBox(0, L"缁х画娓告垙", L"", MB_OK); + gamePaused = false; + } + else if (input == 'q' || input == 'Q') + { + MessageBox(0, L"閫鍑烘父鎴", L"", MB_OK); + gameOver = true; + } + + } + } + if (MessageBox(hwnd, L"閲嶆柊寮濮嬶紵", L"璇疯緭鍏ユ偍鐨勯夋嫨", MB_OKCANCEL)== IDCANCEL) { + break; + } + else { + gameOver = false; + gamePaused = false; + } + + } + closegraph(); + + return 0; + +} \ No newline at end of file diff --git a/Mine-Game/Mine-Game/music/.keep b/Mine-Game/Mine-Game/music/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/Mine-Game/Mine-Game/music/boom.mp3 b/Mine-Game/Mine-Game/music/boom.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..33470dd76a71b30259a4923810c0198e3574d64b Binary files /dev/null and b/Mine-Game/Mine-Game/music/boom.mp3 differ diff --git a/Mine-Game/Mine-Game/music/boom1.mp3 b/Mine-Game/Mine-Game/music/boom1.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..10ca7399af43acf2983a3219b3c2fd5aac7a86e2 Binary files /dev/null and b/Mine-Game/Mine-Game/music/boom1.mp3 differ diff --git a/Mine-Game/Mine-Game/music/start.mp3 b/Mine-Game/Mine-Game/music/start.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..e6fea047b7856509b2a9ab68a8b2a0b5d79c529c Binary files /dev/null and b/Mine-Game/Mine-Game/music/start.mp3 differ diff --git a/Mine-Game/Mine-Game/music/start1.mp3 b/Mine-Game/Mine-Game/music/start1.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..7b119b6b728526f98640bef6aeb886e7dc4c2196 Binary files /dev/null and b/Mine-Game/Mine-Game/music/start1.mp3 differ