I. new project
II. Create a new source file
main.cpp and mining,cpp
III. create a new header file
mining.h
IV. add picture resources
After adding, a. h header file will be generated in the header file to call resources
After opening, you can see that each resource file is defined
Display the added resources in the resource folder
V. VS installation graphics library
After reinstalling VS, you can call the header file of the graphics library in the project - - > #include<graphics.h> / / header file of the graphic library.
Next is the relevant code:
Header part:
mining.h file
1 #include<time.h> //The header file referenced in this way is c Self contained header file
2 #include<stdlib.h>
3 #include<graphics.h> //Header file of drawing library
4
5 #define MAP_WIDTH 500 //Define map width macro definition method MAP_WIDTH That's 550.
6
7 #define MAP_HEIGHT 550 //Define the height of the map
8
9 #define EACH_SIZE 50 //The size of each grid
10
11 #define MAX_X (MAP_WIDTH/EACH_SIZE) //Count how many lines there are
12
13 #define MAX_Y (MAP_HEIGHT/EACH_SIZE) //Count the number of columns
14
15 #define MINE_COUNT 20 //The number of Mines
16
17 //1.Initialize map
18 void InitMap(); //Defining a function is a module doing only its own thing
19
20 //2.Add one around ray
21 void ChangeState(int x,int y);
22
23 //3.Judge whether it is out of line
24 int IsPosOk(int x, int y);
25
26 //4.Map
27 void DrawMap();
28
29 //5.display information
30 void ShowInfo();
31
32 //6.Achieve mouse click ** Not yet realized
33 int IsOpenMine();
34
35 //7.Recursively open the lattice ** Not yet realized
36 void OpenZeroRecursively(int x,int y);
37
38 //8.Open all the squares
39 void OpenAll();
40
41 //9.Judge the win, click the mine and lose ** Not yet realized
42 void RenGame();
Source file section:
main.cpp
1 #include"mining.h"
2 #include<stdio.h>
3
4 int main()
5 {
6 initgraph(MAP_WIDTH, MAP_HEIGHT);//Draw map window
7
8 InitMap();//Initial commutation function
9
10 DrawMap();//Mapping function
11 getchar();
12
13 OpenAll();//Open all the squares
14 DrawMap();//Map
15
16 getchar();
17 return 0;
18 getchar();//Prevent flash back of results
19 }
mining.cpp
1 #include"mining.h" //The header file referenced in this way is defined by ourselves
2 #include"resource.h"
3
4 int Map[MAX_X][MAX_Y]; //Define a 2D array to make a map
5
6 int nCountOpen; //Number of open cells
7
8 int nCountFlag; //Number of tags
9
10 void InitMap() //First function initialization function
11 {
12 //1.Initialize array
13 for (int i = 0; i < MAX_X; i++)
14 {
15 for (int j = 0; j < MAX_Y; j++)
16 {
17 Map[i][j] = 0;
18 }
19 }
20
21 nCountOpen = 0; //Initialization
22
23 nCountFlag = 0; //Initialization
24
25 //2.lay a mine---> Mines are random
26
27 srand((unsigned int)time(0)); // Seed with random number
28
29 int x, y;
30
31 int nCount=0; //Count the number of buried mines
32
33 //Lay mines
34 while (nCount<MINE_COUNT) //At present, the number of buried mines is less than the maximum number of buried mines, and it is always circulating
35 {
36 x = rand() % MAX_X; // ? %10==0~9 rand Get random number
37
38 y = rand() % (MAX_Y - 1) + 1; //
39
40 if (-1 == Map[x][y]) //-1 Land mines
41 {//Random subscript is a mine
42 continue; //Skipping this loop is not the end loop( break)
43 }
44 Map[x][y] = -1; // Buried mine
45
46 nCount++; //Number of buried mines plus one
47 }
48
49 //Add one around mine nine palace grid
50 for (int i = 0; i < MAX_X; i++)
51 {
52 for (int j = 1; j < MAX_Y; j++) //Because there is no thunder in the first line, we are used to display information, so we can't start from 0
53 {
54 if (-1 == Map[i][j]) //Judge if it's a mine -1 Why it's on the left-->Left value Map[i][j]=-1 This form is wrong
55 {
56 ChangeState(i, j); //subscript
57 }
58 }
59 }
60 }
61
62 void ChangeState(int x, int y) //The second function is to add a nine palace lattice range around the thunder
63 {
64 for (int i = x - 1; i <= x + 1; i++)
65 {
66 for (int j = y - 1; j <= y + 1; j++)
67 {
68 if (-1 == Map[i][j]||IsPosOk(i,j)==0 ) //It's a mine and it's a cross-border decision
69 {
70 continue; //Jump out of this cycle
71 }
72 Map[i][j]+=1; //and Map[i][j]++;It's the same.
73 }
74 }
75 }
76
77 int IsPosOk(int x, int y) //The third function determines whether the current subscript is out of bounds
78 {
79 //Logical expression values 0 and 1
80 return (x >= 0 && x <= MAX_X&&y <= 1 && y < MAX_Y); //Cross border return 0 no cross border return 1
81 }
82
83 void DrawMap() //The fourth function maps the map
84 {
85 IMAGE img;
86
87 for (int i = 0; i < MAX_X; i++)
88 {
89 for (int j = 1; j < MAX_Y; j++)
90 {
91 if (Map[i][j] < 9) //The maximum number of 1 around ray is 8, no more than 9, no less than 9, indicating that the lattice has not been opened
92 {
93 loadimage(&img, L"jpg", MAKEINTRESOURCE(IDR_JPG13), EACH_SIZE, EACH_SIZE); //L Wide byte jpg13 It's a picture of a situation that hasn't been opened
94 }
95 else //Has been opened
96 { //Open a grid and let this grid+10 Indicates that it has been opened
97 if (Map[i][j] >= 11 && Map[i][j] <= 18) //11~18 Express 1~8
98 {
99 loadimage(&img, L"JPG", MAKEINTRESOURCE(IDR_JPG1 + Map[i][j] - 11, EACH_SIZE, EACH_SIZE)); //Use this algorithm to determine which picture to post
100 }
101 else if (Map[i][j] == 9) //9-10==-1 -1 It's mine.
102 {
103 loadimage(&img, L"jpg", MAKEINTRESOURCE(IDR_JPG10), EACH_SIZE, EACH_SIZE); //Landmine picture
104 }
105 else if (Map[i][j] == 10) //10-10==0 0 Blank space
106 {
107 loadimage(&img, L"jpg", MAKEINTRESOURCE(IDR_JPG11), EACH_SIZE, EACH_SIZE); //Paste space picture
108 }
109 else
110 {
111 loadimage(&img, L"jpg", MAKEINTRESOURCE(IDR_JPG9), EACH_SIZE, EACH_SIZE); //Tag picture
112 }
113 }
114 putimage(i*EACH_SIZE, j*EACH_SIZE, &img);
115 }
116 }
117 ShowInfo();
118 }
119
120 void ShowInfo() //The fifth function displays information
121 {
122 //Number of buried mines number of open number of tags
123 //Style font
124 settextstyle(20, 20, L"wingding.ttf"); //Font style label
125
126 //background color
127 setbkmode(TRANSPARENT);
128
129 //Font color
130 settextcolor(YELLOW);
131
132 //Set font location
133 WCHAR szOpen[32];
134 WCHAR szFlag[32];
135 WCHAR szAllMine[32];
136
137 //String formatting
138 //And printf scanf Similar
139 wsprintf(szAllMine, L"Bury thunder:%d", MINE_COUNT);//Burying thunder: 20 put the string "burying thunder" into szAllMine It is formatted as string type
140 wsprintf(szOpen, L"Open:%d", nCountOpen);
141 wsprintf(szFlag, L"Mark:%d", nCountFlag);
142
143 //Coordinate adjustment
144 outtextxy(20, 20, szAllMine);
145 outtextxy(190, 20, szOpen);
146 outtextxy(350, 20, szFlag);
147
148 }
149
150
151
152
153
154
155 void OpenZeroRecursively(int x, int y) //The seventh function recursively opens the lattice
156 {
157
158 }
159
160 void OpenAll() //The eighth function opens all the squares
161 {
162 for (int i = 0; i < MAX_X; i++)
163 {
164 for (int j=1;j<MAX_Y;j++)
165 {
166 if (Map[i][j] < 9)
167 {
168 Map[i][j] += 10;
169 }
170 }
171 }
172 }
173
174 void RenGame() //The ninth function judges win or lose
175 {
176
177 }
Note:
Set font style:
The current results are as follows:
Later, I have the chance to write it and then add it
2019-03-20 13:09:47