"VC + + Game development" intelligence game--nitpick (imitation minesweeper)

Source: Internet
Author: User



In my Learning game development process, encountered the biggest trouble is not aware of a game complete implementation process, the code is second.

Here, summarize the game I have done, mainly combing the whole process.


First look at the final effect



1th step, prepare the material picture

It includes eggs, bones, and numbers as well as bone markers and small hammers for smashing eggs.


2nd step, declare the Egg class

It consists of only 2 member variables

Class Cegg{public:int bitmap;//bitmap int num;//number};


2nd step, Initialize the interface

1.1 Still want to set the window parameter and icon in CMainFrame

1.2 and Gobang game different, here, to delete the toolbar and the status bar. (See the OnCreate () function in the CMainFrame class in the code)


2. Load each resource, initialize the number of bones, number of marked bones, time, etc.


3.1 Randomly set several positions for displaying eggs with bones (num-1 for eggs with bones)

Set 25 bone int aa=0;do{        //With the current number of seconds for generating the stochastic algorithm        int k= (rand () *s)%rownum;        int l= (rand () *s)%colnum;        if (egg[k][l].num!=-1)        {    egg[k][l].num=-1;             aa++;         

Calculate the number of bones around each egg after 3.2.

Assign a value to the grid, calculate the number of bones for (int a=0;a<rownum;a++) for (        int b=0;b<colnum;b++) if (egg[a][b].num==0) {for (int c=a-1;c <a+2;c++) for (int d=b-1;d<b+2;d++) if (c>=0&&c<rownum&&d>=0&&d<colnum) if (egg[c][d].num==-1) egg[a][b].num++;        }



3.3 Displays the number of mines in the OnDraw () function of C**view, displays the button bitmap, displays the colored egg bitmap (via random function)

Display button Dc.selectobject (button[0]);pD C->bitblt (250,10,160,160,&dc,0,0,srccopy);


More code, not all of them.


3.4 Setting when the mouse moves to the view-class interface, the hammer is displayed

BOOL cboneineggview::onsetcursor (cwnd* pWnd, uint nhittest, UINT message) {//TODO: Add the message Handler code here and/or call the default value if (nhittest== Htclient) {setcursor (cur); return 1;} Return Cview::onsetcursor (pWnd, nhittest, message);}


3rd Step, complete the game action response function

Now the interface has been done, as long as the left button press, Zuo Open, right-click function function, and restart the function, scanning function.


Analyze the functions to be completed for each action

Left-click:

First judge the position of the press, only the button bitmap above and the egg above the left click is valid;

If you clicked on the button bitmap, change the picture of the button bitmap (and reinitialize when the left key is released)

If you click on the egg bitmap, if there is no bone in the egg, then display the corresponding number, and redraw the surrounding 8 positions, when the egg has a bone, the game is over, stop timing


Left button to release:

If it is above the button bitmap, reinitialize the interface

If it is above the egg bitmap, prompt the player, the game is over


Right-click: (Only on the egg, and the egg is the initial bitmap-like body, the operation is valid)

If you press the first time, the current position displays a warning flag, the number of bones displayed in the upper left corner-1

If you press the second time, remove the warning flag, the top left corner shows the number of bones +1


Start again:

As with the initialization operation, the timing is set.


Scan function:

If the current position is not an egg with bones and there are 0 eggs with bones around it, scan the 8 locations around it and display the corresponding numbers

void Cboneineggview::scan (void) {for (Int. i=0;i<rownum;i++) for (int j=0;j<colnum;j++) if (egg[i][j].num==0& &egg[i][j].bitmap==1) {for (int n=i-1;n<i+2;n++) for (int m=j-1;m<j+2;m++) if (n>=0&&n<rownum &&m>=0&&m<colnum) if (egg[n][m].num!=-1&&egg[n][m].bitmap==0) {egg[n][m].bitmap=1; CRect Rect;rect.left=n*30+10;rect.right=n*30+40;rect.top=m*30+50;rect.bottom=m*30+80;invalidaterect (&rect);}}


Code address

Nitpick

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"VC + + Game development" intelligence game--nitpick (imitation minesweeper)

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.