C++基於EasyX實現簡單掃雷遊戲
阿新 • • 發佈:2020-03-02
本文例項為大家分享了C++ EasyX實現簡單掃雷遊戲的具體程式碼,供大家參考,具體內容如下
【實現程式碼】
#include <cmath> #include <time.h> #include <easyx.h> #include <conio.h> using namespace std; #define Size 500 //定義視窗大小 #define SquareSize 50 //定義格子大小 #define BackGroundColor LIGHTGRAY //定義背景顏色 const int N = Size / SquareSize; //設定地圖大小為N * N const int Num = N * N / 5; //設定總地雷數 int num = Num,all = N * N - Num; //剩餘地雷數和未格子訪問數 int Map[N + 10][N + 10]; //地圖表示(i,j)旁邊的地雷數目,等於-1表示該位置為地雷 bool vis[N + 10][N + 10]; //標記是否(i,j)被訪問 bool vis_lm[N + 10][N + 10]; //標記(i,j)是否被標記為地雷 int dx[] = { 0,1,-1,-1 }; int dy[] = { 1,1 }; void Init() { initgraph(Size,Size); setbkcolor(BackGroundColor); settextcolor(YELLOW); settextstyle(25,_T("宋體")); cleardevice(); } void BuildGameMap() { //構建地圖 srand((unsigned int)time(NULL)); //產生隨機數 for (int i = 1; i <= Num; ++i) { int x = rand() % N + 1,y = rand() % N + 1; while (Map[x][y] == -1) { x = rand() % N + 1,y = rand() % N + 1; } Map[x][y] = -1; } //計算每個格子旁邊的地雷數目 for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (Map[i][j] == -1) continue; for (int k = 0; k < 8; ++k) { if (Map[i + dx[k]][j + dy[k]] == -1) ++Map[i][j]; } } } } void InitGameGraph() { //地圖初始化 memset(Map,sizeof(Map)); memset(vis,false,sizeof(vis)); memset(vis_lm,sizeof(vis_lm)); num = Num,all = N * N - Num; BuildGameMap(); cleardevice(); //清屏 setfillcolor(WHITE); for (int i = 0; i <= Size; i += SquareSize) { line(i,i,Size); } for (int i = 0; i <= Size; i += SquareSize) { line(0,Size,i); } } int Translate_X(int x) { //左邊變換 return x * SquareSize + SquareSize / 2; } void GameOver(bool tag) { RECT r = { 0,Size - 1,Size - 1 }; if (tag) { drawtext(_T("Win"),&r,DT_CENTER | DT_VCENTER | DT_SINGLELINE); } else { //遊戲失敗畫出所有地雷 setfillcolor(RED); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (Map[i][j] == -1) { if (vis_lm[i][j]) continue; //該地雷已被玩家標記,跳過 outtextxy(Translate_X(i - 1),Translate_X(j - 1),'X'); //否則顯示出來 } else if (vis_lm[i][j]) { //玩家標記了錯誤的地雷,標紅 fillcircle(Translate_X(i - 1),SquareSize / 2 - 10); } } } drawtext(_T("Failed"),DT_CENTER | DT_VCENTER | DT_SINGLELINE); } } void Help() { //除錯函式(其實是掛) for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (Map[i][j] == -1) { outtextxy(Translate_X(i - 1),'X'); } } } } void StartGame() { Init(); while (true) { InitGameGraph(); //Help(); bool IF_WIN = true; while (all || num) { //勝利條件是所有非地雷格子均被訪問且所有地雷均被標記 MOUSEMSG msg = GetMouseMsg(); //獲取使用者滑鼠資訊 int x = ceil(msg.x * 1.0 / SquareSize * 1.0),y = ceil(msg.y * 1.0 / SquareSize * 1.0); if (vis[x][y]) continue; //該格已被訪問,跳過 if (msg.mkLButton) { //滑鼠左鍵按下 if (Map[x][y] == -1) { //如果是地雷那麼遊戲結束 IF_WIN = false; break; } outtextxy(Translate_X(x - 1),Translate_X(y - 1),Map[x][y] + '0'); --all; vis[x][y] = true; } else if (msg.mkRButton) { //滑鼠右鍵按下 if (!vis_lm[x][y]) { //如果該格子已被標記,消除標記 vis_lm[x][y] = true; if (Map[x][y] == -1) --num; solidcircle(Translate_X(x - 1),SquareSize / 2 - 10); } else if (vis_lm[x][y]) { //否則標記 vis_lm[x][y] = false; if (Map[x][y] == -1) ++num; clearrectangle((x - 1) * SquareSize + 1,(y - 1) * SquareSize + 1,x * SquareSize - 1,y * SquareSize - 1); } } } GameOver(IF_WIN); int t = _getch(); } } int main() { StartGame(); return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。