Next is the function of the game design, to have the basic functions of mine clearance, left click on the minefield, right button red flag, and can count the number of thunder, you can start again, the following is the function of the game initial
void game (void) {while (1) {if (FLAG = = 1) {gamebegin ();//Draw out the game interface and determine whether to play and restart gameplay ();//The function of the game process if (AGAIN = = 1) {AGAIN = 0; Continue;}} FLAG = 0;if (m.umsg = = wm_lbuttondown)//left mouse button pressed event {mousegetxy (); if (MouseX > && mousex<110 && Mouse Y>5 && Mousey <) {FLAG = 1;continue;}} if (_kbhit ())//Determine if there is a key to exit {break;}}
Here flag is the game after the failure to restart the logo, the first time the same is true, and again is the game midway is not finished and want to restart the logo, the first time to play to draw the game interface, the following is the drawing function
void Gamebegin (void) {int i, j;cleardevice (); PLAY = 1;minenum = 0;setfillstyle (bs_solid);//Here the usage and TC differ setfillcolor (white); bar (0, 0, 200, 230); Set the background area for (i = 0; i < i++)//plot each mined area (small) {for (j = 0; J <) J + +) {Drawempty (i, J, 0, Lightgray);}} Drawsmile ();//Draw the middle of the smiley face Srand ((unsigned) time (NULL)),//The number of random number of different seed numbers for (i = 0; i <; i++) {for (j = 0; J <; J + +) {M Ine[i][j].num = rand ()% 8;//random number generation range 0-7if (mine[i][j].num = = 1) {minenum++;} Else{mine[i][j].num = 2;} printf ("%3d", mine[i][j].num); Mine[i][j].flag = 0;} printf ("\ n"); _stprintf_s (Randminenum, _t ("%d"), minenum);//Convert Minrnum to String type SetBkColor (white); SetColor (RED); Settextstyle (0, _t ("0")); Outtextxy (2, 2, randminenum);} Minenum = 100-minenum;}
The drawing functions and TC names function the same, but the parameters passed are simplified, the drawempty () function is a function of drawing small lattice, where the coordinates need to be converted from screen coordinates to the coordinates of the window, need to understand the conversion between coordinates
For example, in the upper left corner of the screen to establish a coordinate system, the starting point is (0,0), the window vertex screen coordinates (x, y), the window point of the screen coordinates of P (XP,YP), the P point in the window is the origin of the window coordinates is (XP-X,YP-Y)
void Drawempty (int i, int j, int mode, int color)//Draw 16*16 's small lattice {setfillstyle (bs_solid); Setfillcolor (color); if (mode = = 0) {b AR (Ten + J * 20-8, + i * 20-8, + J * + 8, + + I * 20 + 8);} else if (mode = = 1) {Bar (ten + J * 20-7, + i * 20-7, + J * + 7, + + I * 20 + 7);}}
This draws 100 small cells through a for loop
Next, we'll draw the yellow smiley face in the middle
void Drawsmile (void) {Setfillstyle (bs_solid); Setfillcolor (YELLOW); FillEllipse (90, 5, 110, 25);//Draw an ellipse (smiley) function, The parameter is the upper-left lower-right corner coordinate of the ellipse bounding rectangle Setfillstyle (bs_solid); Setfillcolor (BLACK);//Draw the eye fillellipse (x, x, y, n); FillEllipse (103, 13, 107 Bar (97, 20, 103, 21);//Draw Mouth}
These are basic drawing functions, is to understand C better, although not as MFC's encapsulation, but can understand the basic principle of WINAPI is similar
After the interface is painted well, this function is simple, using random number function to create a mine, thunder number is uncontrollable, you can use while loop to determine the 10 thunder stop, but it takes time, you can also grow the algorithm pseudo-random mine, that is limited to a number of each region
The Srand function is the generation of random number seed numbers, which produce different seed numbers over time and need to use the Time.h header file
Rand () and n redundancy is the generation of 0-(n-1) in the range of random numbers, the specific mathematical algorithm self-baidu
The following sprintf is used with the wide character, vs2013 is the C11 standard, other compilers can use sprintf () to convert the remaining number of mines into a string display
The syntax of the specific function refer to the official help of Easyx, it's here today!
C Language New handwriting minesweeper Strategy 2