Use VC to create a single-host five-game

Source: Internet
Author: User
Tags textout

 

Use VC to create a single-host five-game

Computer Language: 22:08:12 read 134 comments 2 font size: large, medium, and small subscriptions

1. Create a user logon box

For example, there are two users, Zhang San and Li Si.

Define two global variables

CString nameblack; // Save the name of a blacklisted player.

CString namewhite; // Save the white player name

GetDlgItemText (IDC_EDIT1, nameblack );

GetDlgItemText (IDC_EDIT2, namewhite );

To make this logon box run first

Int CMainFrame: OnCreate (maid)

Add

Clogin login; // Clogin is the class name of the dialog box.

Login. DoModal ();

Then, the dialog box will be run first.

2. In the CMainFrame: PreCreateWindow (CREATESTRUCT & cs) function, set the height and width of a single document, as well as the start point.

Cs. x = 100;

Cs. y = 0;

Cs. cx = 850; // The width of the interface

Cs. cy = 700; // The height of the interface

Cs. hMenu = 0;

3. Set the title of the interface.

Set in Doc class

BOOL CGobangDoc: OnNewDocument ()

Add one sentence

SetTitle ("Zhang Jing ");

4. Import background images

Import resource images to bitmap resources.

Get the ID of the background image

For example, the ID number is IDB_Chessboard.

Then define the member variables in the view class.

CBitmap m_chessboard;

CDC Chessboard;

In the OnDraw function of the view class

M_chessboard.LoadBitmap (IDB_Chessboard );

Chessboard. CreateCompatibleDC (pDC );

Chessboard. SelectObject (m_chessboard );

PDC-> BitBlt (850,600, & Chessboard, SRCCOPY); (8 parameters)

In this way, the background image is drawn.

5. Use the same method to open the start button, exit button, regret button, and start again. And

PDC-> BitBlt (): This function places these bitmap images in the appropriate positions.

Note that you need to paste the largest image first, and then the smallest image. Otherwise, if the order is messy, the large image will be covered, thus affecting the effect of the game.

Note that the ReleaseDC (pDC) is very important after the image is pasted. Otherwise, the memory will leak.

6. The vertical and horizontal coordinates of the mouse are displayed on the interface.

Add a message response with the mouse moving in the view class

Void CGobangView: OnMouseMove (UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

 

CView: OnMouseMove (nFlags, point );

Int x = point. x;

Int y = point. y;

CString st;

St. Format ("% d, % d"), x, y );

CDC * pDC = GetDC ();

PDC-> TextOut (200,0, st );

ReleaseDC (pDC );

SetCursor (LoadCursor (NULL, IDC_CROSS); // load a cross-shaped mouse cursor.

CView: OnMouseMove (nFlags, point );

}

7. Add a PrintState (CDC * pDC) function to display the white child, sunspots, and players playing chess on the top of the page.

Define a global variable type1. If type1 is set to 1, the player is playing the game. If type1 is set to 0, the player is playing the game. If type1 is set to 2, the game has not started yet. There are three states and three different writing methods, but they are all similar.

Void CGobangView: PrintState (CDC * pDC) // write this function in the repainting function PrintAll.

{

CString st;

If (type1 = 1)

{

St = "";

PDC-> TextOut (280,10, st );

// This function is used to place the content in the st at 280 from the Left Border and 10 from the top border.

St = nameblack + "playing chess with sunspots ";

// UpdateData (true );

// PrintPart (8,-1, 0, pDC );

UpdateData (false );

}

Else if (type1 = 0)

{

St = "";

PDC-> textout (280,10, St );

St = namewhite + "playing white ";

// Updatedata (true );

// Printpart (8,-1, 1, PDC );

Updatedata (false );

}

Else

If (type1 = 2)

{

St. Format ("the game has not started ");

}

PDC-> textout (280,10, St );

PDC-> textout (, "sunspot :");

PDC-> textout (expires, 25, "White Sub ");

PDC-> textout (190,25, nameblack );

PDC-> textout (, 25, namewhite );

St. releasebuffer (); // The releasebuffer () method is used only when St. getbuffer () is called.

}

8. Add the void CGobangView: PrintAll (CDC * pDC) function.

Void CGobangView: PrintAll (CDC * pDC)

{

Int x;

Int y;

PDC-> BitBlt (800,600, & Chessboard, SRCCOPY); // draw the background

PDC-> BitBlt (590,145,119, 64, & Begin, SRCCOPY); // draw the start button

PDC-> BitBlt (590,275,115, 63, & End, SRCCOPY); // paste the End button

PDC-> BitBlt (530,400,300,100, & Score, SRCCOPY); // click the regret button

PDC-> BitBlt (80,530,300,100, & restart, SRCCOPY); // paste the start button again

PrintState (pDC );

For (x = 0; x <15; x ++) // draw the entire chess board.

For (y = 0; y <15; y ++)

{

If (chess [x] [y] = 1)

Redraw (x, y, 0, pDC); // draws White Paper

Else if (chess [x] [y] = 2)

Redraw (x, y, 1, pDC); // draws sunspots

}

}

9. Add redraw (X, Y, 1, PDC); // you can determine whether to draw a white child or a black child based on the transmitted coordinates and the chess type.

Void cgobangview: redraw (int x, int y, int type, CDC * PDC )]

{

If (type = 0)

{

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & Mask, mergepaint );

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & blackchess, srcand );

}

Else if (type = 1)

{

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & Mask, mergepaint );

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & whitechess, srcand );

}

10. Add a function to attach a pawn when the mouse clicks down.

Void cgobangview: printpart (int x, int y, int type, CDC * PDC)

{

If (chess [x] [Y] = 0)

{

If (type = 0)

{

{

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & Mask, mergepaint); // draw the background of the pawns

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & blackchess, srcand); // draw real pawns

}

Chess [x] [Y] = 1;

Type1 = 1;

}

Else if (type = 1)

{

{

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & Mask, mergepaint );

PDC-> bitblt (73 + 30 * x-14, 86 + 30 * y-14, 27,28, & whitechess, srcand );

}

Chess [x] [Y] = 2;

Type1 = 0;

}

}

// Afxbeginthread (& thread1, 0 );

// Afxbeginthread (& Thread, 0 );

Playsound (makeintresource (idr_luozishengyi), afxgetresourcehandle (), snd_async | snd_resource );

}

11. Add the message response function with the left mouse click (this step is critical)

Void CGobangView: OnLButtonDown (UINT nFlags, CPoint point)

{

CDC * pDC = GetDC ();

Int x = point. x;

Int y = point. y;

If (x> = 582 & x <= 720 & y> = 140 & y <= 212)

{

MessageBox ("game preparation starts ");

PlaySound (MAKEINTRESOURCE (IDR_bgsound), AfxGetResourceHandle (), SND_ASYNC | SND_RESOURCE | SND_LOOP );

Paste = 1;

}

If (x> = 584 & x <= 709 & y> = 268 & y <= 346)

{

If (MessageBox ("Do you really want to quit the game! "," Title ", MB_OKCANCEL) = IDOK)

Exit (0 );

}

If (x> = 92 & x <= 248 & y> = 537 & y <= 583)

{

If (MessageBox ("do you want to start again! "," Title ", MB_OKCANCEL) = IDOK)

{

For (int I = 0; I <15; I ++)

For (int j = 0; j <15; j ++)

{

Chess [I] [j] = 0;

}

Paste = 1;

Win_state = 0;

PrintAll (pDC );

}

}

If (x> = 530 & x <788 & y> = 400 & y <= 500 & win_state = 0)

{

MessageBox ("regret ");

{

Chess [temp_mx] [temp_my] = 0;

Paste = 1;

If (type1 = 0)

{

Type1 = 1;

UpdateData (false );

}

Else

If (type1 = 1)

{

Type1 = 0;

UpdateData (false );

}

UpdateData (false );

}

PrintAll (pDC );

}

// The following are the specific pawns and the algorithms used to determine the outcome.

If (paste = 1)

{

Int mx = (point. x-61)/30;

Int my = (point. y-68)/30;

Temp_mx = mx;

Temp_my = my;

If (mx> = 0 & mx <15 & my> = 0 & my <15)

{

PrintState (pDC );

If (type1 = 0) // Determination of white pawns

{

PrintPart (mx, my, 0, pDC );

//.... Is an algorithm used to determine whether to win or lose.

Judge eight directions from the current position

If one direction satisfies five, the game ends.

If you win

{

Nameblack = "Black wins ";

PDC-> TextOut (190,25, nameblack );

Win_state = 1; // The game is over

PlaySound (MAKEINTRESOURCE (IDR_BLACK_WIN), AfxGetResourceHandle (), SND_ASYNC | SND_RESOURCE); // plays the Kaige of victory

MessageBox ("Black wins ");

Paste = 0; // no more posts

UpdateData (false );

}

}

For sunspots, the principle is the same as that for white devils.

}

12. Important knowledge points

When using BitBlt, there are two attributes

A mergepaint // first reversed, then OR

The other is SRCAND // perform the and operation.

Any color is the same as the white color. The OR operation result is the white color.

If any color is the same as the black color, OR the result is the color itself, AND the result is black.

13. Small knowledge points

All added in the InitInstance function of the App class

Center the window

AfxGetMainWnd ()-> CenterWindow (); // center the window directly.

Maximize windows

M_nCmdShow = SW_SHOWMAXIMIZED;

Minimum window

M_nCmdShow = SW_SHOWMINIMIZED;

 

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.