The original version of mine sweeping can be traced back to 1973, a game called "square".
Soon, "square" was rewritten into the game "Rlogic". In "Rlogic", the player's task is to act as US Marine Corps Team members, find a safe route without mines for the command center. If all the roads are blocked by mines, you will lose. Two years later, Tom Anderson wrote the game "mine" on the basis of "Rlogic", which laid the prototype of modern mine clearance game.
In 1981, Robert Dole and Carter Johnson of Microsoft worked in windows 3 1 the game was loaded on the system, and the mine sweeping game was officially promoted all over the world.
The playing method of this game is to randomly arrange a certain number of mines in a 9 * 9 (primary), 16 * 16 (intermediate), 16 * 30 (Advanced) or custom size square matrix (10 for primary, 40 for intermediate and 99 for advanced). Players open the boxes one by one to find out all mines as the final goal of the game. If the player opens a box with mines, the game is over.
Today we will finish the production of the small game "mine sweeping".
PS: to install easyx graphics library #include < graphics H > (received at the end of the text)
Development environment: vs2013+easyx
First, create a project and put the prepared material resources under the same level directory (the materials can be collected at the bottom of the article)
OK, let's start writing code now!
Step 1: write the header file and some macro definitions first
Explain why it's graphics h
Because the Easyx library comes with graphics h. So you just need to add the next Easyx library. You can also get it at the end of the text~
#include<stdio.h> #include<graphics. h> / / contains the graphics library header file #define ROW 9 / / line #define COL 9 / / there are 81 cells in the column #define MINE_ Num 18 / / number of Mines #define IMG_SIZE 40
Step 2: define and initialize the function (get the material at the end of the text)
IMAGE img[12];//Define a picture array with a size of 12 to store all pictures respectively int mine[ROW+2][COL+2]; int openr, openc;//Subscript of the mouse clicked array int num;//How many non ray lattices are opened //Initialization data void GameInit() { //1. There are nine pictures in one line of the created window, and each picture is 40px, initgraph(IMG_SIZE*ROW, IMG_SIZE*COL,SHOWCONSOLE); //Load picture (assignment) load loads the first parameter and stores the variable error of the picture: character set problem for (int i = 0; i < 12; i++) { char file[20] = ""; sprintf(file, "./image/%d.jpg", i); loadimage(&img[i], file, IMG_SIZE, IMG_SIZE); } //Mine for (int i = 0; i < MINE_NUM; ) { //Exclude auxiliary area int row = rand() % ROW+1;//0-8 1-9 int col = rand() % COL+1; if (mine[row][col] == 0) { mine[row][col] = 9; i++; } } //Add 1 to the nine palaces of Lei (except Lei) for (int i = 1; i < ROW+1; i++) { for (int k = 1; k < COL+1; k++) { if (mine[i][k] == 9) { //Traverse Ray's nine palaces carefully for (int a = i - 1; a <= i + 1; a++) { for (int b = k - 1; b <= k + 1; b++) { //Non ray lattice plus 1 if (mine[a][b] != 9) { mine[a][b]++; } } } } } } //At the beginning of encryption, all are mask graphs for (int i = 1; i < ROW + 1; i++) { for (int k = 1; k < COL + 1; k++) { mine[i][k] += 20; } } } void GameDraw() { for (int i = 1; i < ROW+1; i++) { for (int k = 1; k < COL+1; k++) { int row = (k - 1)*IMG_SIZE; int col = (i - 1)*IMG_SIZE; if (mine[i][k] >= 0 && mine[i][k] <= 9) { putimage(row,col , &img[mine[i][k]]); } else if (mine[i][k]>=20 && mine[i][k]<30) { putimage(row, col, &img[10]); } else if (mine[i][k]>29) { putimage(row, col, &img[11]); } } } }
Step 3: process mouse messages
void OpenNull(int row, int col); //Handle mouse messages, not keyboard messages void MouseEvent() { //Check for mouse messages if (MouseHit()) { //Get the mouse message, coordinates, left or right button MOUSEMSG msg = GetMouseMsg(); //Convert coordinates to array subscripts openr = msg.y / IMG_SIZE+1; openc = msg.x / IMG_SIZE+1; //Determine whether it is left or right switch (msg.uMsg) { case WM_LBUTTONDOWN: //If the grid is not opened, open the grid, otherwise it will not be processed if (mine[openr][openc] > 9) { mine[openr][openc] -= 20; OpenNull(openr,openc); num++; } break; case WM_RBUTTONDOWN: //sign if (mine[openr][openc] > 9 && mine[openr][openc] <= 29) { mine[openr][openc] += 20; } else { mine[openr][openc] -= 20; } break; } } }
Step 4: print two-dimensional array
void show() { for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { printf("%3d ", mine[i][k]); } putchar('\n'); } }
Step 5: perform a recursive operation to open all blanks and the numbers around the blanks
void OpenNull(int row,int col) { //Click blank to open if (mine[row][col] == 0) { for (int i = row - 1; i <= row + 1; i++) { for (size_t k = col-1; k <= col+1; k++) { //If it is empty or not mine, open it if ((mine[i][k] == 20 || mine[i][k] != 29)&& mine[i][k]>9) { mine[i][k] -= 20; num++; OpenNull(i, k); } } } } }
Step 6: the judgment function of winning or losing the game
void Jude() { //Judge lose if (mine[openr][openc] == 9) { int isok = MessageBox(GetHWnd(), "Continue!", "It's thunder", MB_OKCANCEL); if (isok == IDOK) { mine[openr][openc] += 20; } else { exit(666); } } //Judge to win if (num == ROW*COL - MINE_NUM) { int isok = MessageBox(GetHWnd(), "Continue!", "It's thunder", MB_OKCANCEL); if (isok == IDOK) { num = 0; GameInit(); } else { exit(666); } } }
Step 7: finally, our main function
int main() { GameInit(); show(); //Loop processing game logic while (1) { MouseEvent(); GameDraw(); Jude(); } getchar(); return 0; }
Well, that's all the basic code of mine sweeping game~
Students who need complete source code comparison and complete source code material graphic library development tools can receive ~ or add groups at the end of the article [684197747]
In the future, the UP master will release more project source codes and learning materials. I hope you can continue to pay attention~
If you have questions, you can ask questions in the group. I hope you can get the knowledge you want here. If it is helpful to you, you can pay attention to praise comments. If you have suggestions, you can also put forward them in the comment area. I hope to learn and progress with you!!!