[VC ++ Game Development] Intelligence games-pick bones in eggs (imitation mine removal) and pick bones in vc eggs

Source: Internet
Author: User

[VC ++ Game Development] Intelligence games-pick bones in eggs (imitation mine removal) and pick bones in vc eggs


When I learned about game development, the biggest trouble I encountered was that I did not know the complete implementation process of a game, but the code was second.

Here, I will summarize the games I have played, mainly to sort out each step.


Let's take a look at the final effect.



Step 2: Prepare materials and Images

Including eggs, Bones, numbers, and hammers used to identify and smash eggs.


Step 2: declare the Egg class

It contains only two member variables.

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


 

Step 2: Initialize the interface

1.1 you still need to set window parameters and icons in CMainFrame

1.2 is different from the wuziqi game. Here, you need to delete the toolbar and status bar. (For details, see the OnCreate () function in the CMainFrame class in the Code)


2. Load resources, initialize the number of bones, number of labeled bones, and time


3.1 randomly set several locations for displaying eggs with bones (num-1 indicates eggs with bones)

// Set 25 bone int aa = 0; do {// generate a random algorithm int k = (rand () * s) % rowNum in the current number of seconds; int l = (rand () * s) % colNum; if (egg [k] [l]. num! =-1) {egg [k] [l]. num =-1; aa ++ ;}} while (aa! = Bone );

After 3.2, the number of bones around each egg is calculated.

// Assign a value to the square and 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 In the OnDraw () function of C ** View, the number of mines, the button bitmap, and the color egg bitmap are displayed (through the random function)

// Display the button Dc. SelectObject (button [0]); pDC-> BitBlt (160,160, 10, & Dc, SRCCOPY );


A lot of code, not all posted


3.4 set a hammer to be displayed when the mouse moves to the video interface

BOOL CBoneInEggView: OnSetCursor (CWnd * pWnd, UINT nHitTest, UINT message) {// TODO: add the message processing program code and/or call the default value if (nHitTest = HTCLIENT) {SetCursor (cur); return 1;} return CView: OnSetCursor (pWnd, nHitTest, message );}


Step 2: complete the game action response function

Now that the interface is ready, you only need to complete the left-click, left-click release, right-click function, and re-start function, scan function.


Analyze the functions to be completed for each action

Left click:

First, determine the position where the button is pressed. It is valid only when the left button is pressed on the button bitmap and the eggs;

If you click a button bitmap, the bitmap of the button is changed (reinitialize when the left button is released)

If you click on the egg bitmap, if there are no bones in the egg, the corresponding number is displayed, and the surrounding eight locations are re-painted. When the egg contains bones, the game ends and the timing is stopped.


Left-click release:

If it is on the button bitmap, reinitialize the interface

If it is on the egg bitmap, it prompts the player that the game is over.


Right-click: (only on the eggs, and the eggs are the initial bitmap, this operation is valid)

If you press the button for the first time, a warning sign is displayed for the current position. The number of bones displayed in the upper left corner is-1.

If you press the button for the second time, remove the warning sign. The number of bones displayed in the upper left corner + 1


Start again:

Same as the initialization operation, you also need to set the timing.


Scan function:

If the current position is not an egg with a bone and there are 0 eggs with a bone around it, scan the eight 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

Pick bones in the eggs

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.