C language write Minesweeper Games 2

Source: Internet
Author: User

This is written in the afternoon, this time the modification increases the mouse event, the need is the Windows message mechanism, the WINAPI function, the following is the newly added definition

Struct{int num;//Lattice Current state, 1 means that there is thunder, 0 means no thunder or already shows the number int roundnum;//statistic lattice around the number of mines int flag;//right-click to display the red flag, 0 means no, 1 means there is}mine[10][10]; Point mouse;//defines the mouse event int mousex, mousey;//the mouse's x, y coordinates int minenum;//The count of processed squares TCHAR randminenum[100];//Displays the string of numbers

MINE[10][10] is a 10*10 lattice, point is the alias of the mouse structure, you can see the definition of x and y coordinates

typedef struct tagpoint{    LONG  x;    LONG  y;} Point, *ppoint, near *nppoint, far *lppoint;

Then add a statement that determines the position of the mouse in the game () to determine whether to start again

if (WM_LBUTTONDOWN)//left mouse button pressed event {GetCursorPos (&mouse);//Get the current coordinates of the mouse, point Mouse define mousex = mouse.x; Mousey = mouse.y;if (MouseX > && mousex<110 && mousey>5 && Mousey <) {FLAG = 1;co Ntinue;}} if (_kbhit ())//Determine if there is a key to exit {break;}

Then in Gamebegin () Add the operation about the mouse, and randomly generate the number of mines, the following is the complete code

void Gamebegin (void) {int i, j;cleardevice (), if (PLAY! = 1) {setcursorpos (100, 10),//Set mouse initial position mousex = 100; Mousey = 10;} PLAY = 1;minenum = 0;setfillstyle (Solid_fill);//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);}} SetColor (GREEN);//drawsmile (); rand ();//Generate random number for (i = 0; i <; i++) {for (j = 0; J <; J + +) {Srand ((unsigned) time (N ULL));//Depending on the time given to the random number of different seed number Mine[i][j].num = rand ()% 8;//random number generation range 0-7if (Mine[i][j].num = 1) {minenum++;} Else{mine[i][j].num = 2;} Mine[i][j].flag = 0;} _stprintf_s (Randminenum, _t ("%d"), minenum);//Convert Minrnum to String type SetColor (white); Settextstyle (0, _t ("0")); O Uttextxy (2, 2, randminenum); minenum = 100-minenum;}}

Because many WINAPI and other functions are called, it is to include the

#include <stdio.h> #include <graphics.h> #include <windows.h> #include <easyx.h> #include < Conio.h>  //_kbhit () #include <stdlib.h>//rand (), Srand () #include <time.h>   //srand ((unsigned ) time (NULL))

Ok,today is enough!

C language write Minesweeper Games 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.