Write a mine game with VC ++

zhaozj2021-02-08  263

Department of Information Technology, Huazhong Normal University, 98 (430079) Wang Hongya, from the analysis of the functional characteristics of Windows Minesweeper game, the application of object-oriented visual programming language Visual C gives a specific implementation method of functional module, and provides a small game program General methods and some of the use skills of Visual C . First analyze the most basic functions of mine clearance. Click the left mouse button to the unknown area, if the unknown area has thunder, the game stops, showing all mines. If there is no thunder, it will show the surrounding mine. If there is no thunder around, then check whether there is thunder that is thunder until there is thunder and display, this is actually a recursive process. Click the right mouse button to the unknown area, it is set to have thunder, regardless of whether it is really thunder. You can choose to start, medium, high and high levels and customize mine and regional size. On the left side of the upper part of the mine area, the total mine reduction is indicated by the number of thunder. The upper middle position of the mine zone shows a button for starting and displaying the results of the mouse action. The mine-mine time is displayed on the right side of the mine area. After cleaning all the thunder, a dialog box will be displayed in the list of your names. Sort by time. To complete the above functions, the specific technical details of Visual C are as follows: 1. Apply AppWizard Create an SDI-based application CBMBAPP, remove print and status strip support, modify the menu and corresponding acceleration keys in the resource editor, make it with Windows The mine-sweeping game is consistent. Specific (id-game-junior), id-game-middle, advanced (id-game-seenior), custom (ID-GAME-CUSTOM), color ( ID-GAME-Color, Id-Game-Sort, exits (id-game-exit), help (ID-help). 2. 13 attributes corresponding to each small area of ​​the mine area in the resource editor. Draw the corresponding 13 10-color pitcases with a brush or other drawing tool, three 16-color small bitmap 20 multi-20 20-multiply 20 multiplication corresponding to the small man's expression for replacing the color. The first 16 corresponding monochrome bitmaps, the display time and the mine are 0 ~ 9 ten digital bitmaps (with black). Customized Customer dialogs, with three static text controls and three editing controls, three editing controls respectively correspond to members M_Irownum, M_IColumnNUM, M_IBombNum. The dialog is used to customize mine, ranked, its corresponding MFC class is CCUSTOMER. Customize the Sort dialog, with nine static text controls, where six display list names and time, its corresponding MFC class is Csort. Custom Input dialog, with a static text control and an editing control, edit control is used to enter your name at the end of the game, and the corresponding MFC class is Cinput. 3. Define the related properties of the class BMB to encapsulate each thunder. Class Bomb {public: int isbomb; // Decide whether it is a Ray BOOL ISSEL; / / Decision area is processed and there is Ray BOOL ISDOONE; / / Decision is handled in INT NUM; // Surrounding Thunder Bool Findbomb; // The mine is considered a thunderstorm (but not necessarily a thunder)}; 4. Obtain the precomreteWindow in CMAINFRAME, and set the corresponding attribute to fix the form size, which fixed the display area. The size is the initial 10 ride of the control area of ​​the upper part of the thunder and the external thunder, and some of the code is as follows. CS.Style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER | WS_MINIMIMIGIZEBOX; CS.CY = 10 * 15 6; cs.cx = 10 * 15 60; //6 and 60 are horizontal vertical value, for borders, menus , Title bar, control area. 5. The main job of the game is to show the conversion graphics or animation, and display according to the user's input, and the Windows documentation ----- The window of the window in the window architecture is accepting user input and responsible, so by CVIEW class to complete most of the work of mine clearance. Define the following member variables to record the status of the result or object of the object.

Bomb m_bomb [30] [30]; // The largest mine region cString m_currenttime; // is used to record and display the mine time ctime m_begintime; // Record the time BOOL M_TIMERBEGIN in the start of the game; / / The timer is turned on INT m_ibomnum; // Thunder number int m_irow; // Lei line number int m_icolumn; // Lei column number int m_ibombfound; / / Indicent is considered to be the number of cbitmapButton m_bitbutton; // control Zone bitmap button INT m_currentlevel; / / Indicate the level BOOL M_BISCOLOR; / / instruction of the current game is currently color or monochrome cbitmap m_bmbomb [12]; // Used to store 12 small bitmap INT m_igameover; // The game is not ended 0, cleared all thunder Set 1, being killed 2. Overloading the oncreate function in the CBMBVIEW Create a bitmap button, the two bitmaps of the bitmap button correspond to normal, and the mine is correct. When you want to display the dead state, you should be dynamically destroyed, and recreate one The figure corresponds to the normal and killed in two states, and the ID number of the bitmap button is identical to id_game_begin, so that the game can be restarted when the button is clicked, and some of the code is as follows. CRECT RCCLIENT; CRECLIENTRECT; CRECT Rect (RcClient.cx/2-8, 10 ,rcclient.cx/2 8, 20); m_button.create ("new", BS_DEFPUSHBUTTON | WS_VISIBLE | BS_OWNERDRAW, RECT, THIS, ID_GAME_BEGIN); m_button.loadBitmaps (idb_face1, idb_face2); display time is relatively simple, turn on the timer when responding to the first WM_LBUTTOMDOWN message, and record the start of the game, get the current time in the WM_TIME message response function ONTIMER, After subtracting the game start, the time difference (with a digital bitmap) is displayed at the client area of ​​the display time, when the game is completed (all mine is killed), shut down the timer, stop displaying. WM_LBUTTOMDOWN Message Response Function ONLBUTTOMDOWN is the main executor to handle users, first determined whether the position is thunder in the point, it is closing the timer, destroying the original graph button, creating a new position corresponding to the nature and being killed. The picture button, and calls setState to set it to PushDown (the state of the small man crying), set the m_bgameover, set the TRUE flag game, otherwise call the setState setting bitmap button for Pushdown (the state laughs), and in ONLBUTTOMUP Set the bitmap button to normal state, then call the Caculate function to record the number of surrounding thunders, finally calling invalidate to invalidate the customer area, forcing the onDraw function to redraw the customer area, call the invalidate, should not retrans over the background, avoid flashing, so The response action on the left button in the mine area is completed. WM_RBUTTOMDOWN Message Response Function ONLBUTTOMDOWN will be considered a m_ibombnum.findbomb in the mine position, reduce the remeasure of the upper left corner, and then determine if all the thunder is exhausted, and then the game pops up the Input dialog, let the mine-mine-mine enter the name When you respond to the IDOK notification code, you don't have all rows to make the customer area invalid, forcing the onDraw function to redraw the customer area to complete the movement of the right button in the mine area. OnDraw functions are called every time you click Left or Right button, it will call the heavy thunder area and control area, because the complexity of the clicks and the diversification of the mine attributes, the overdraw function needs to be carefully designed, the following is the process: Figure 1 function Caculate calculation The mine around a thunder, according to the previous analysis, calculating the mine surrounding the thunder itself is a recursive process, paying attention to the boundary condition of recursive when preparing, slightly, not paying, exhausted, exhausted system The resource, the following is the process: Figure 2 6. The response of the menu command is another important way to interact, the following nine command response functions correspond to nine menu items to complete the user's update and setup commands. .

转载请注明原文地址:https://www.9cbs.com/read-1324.html

New Post(0)