VC version of the Snake Game

Source: Internet
Author: User
Tags textout

When I was studying C ++, I designed a C ++ version of the Snake game. At that time, I packed a lot of things into the Snake class, to transplant it to VC, you only need to modify the Snake class and add it to VC.

Here, I feel the advantages of reusability. I will package things irrelevant to the display or platform together. If we can further abstract things as abstract as possible, we can make full use of their reusability.

For more information about Snake design, see my blog "C ++ version of Snake Games".

The following is the game interface under VC:

Here we will talk about the things in VC different from those in C ++ and new homes.

1) play background music:

// Import to database # include <mmsystem. h> # pragma comment (lib, "winmm. lib ") // TODO: Add your command handler code here // Add background music PlaySound (NULL, NULL, NULL ); // stop the music mciSendString ("open. /res/audio streams alias bk ", 0, 0, 0); mciSendString (" play bk repeat ", 0, 0, 0 ); ///////////////

2) The display is different. This is the biggest change, but it is not difficult because two-dimensional arrays are used in C ++. Here we only need to follow the values of two-dimensional arrays, to be displayed as different images.

Check part of the Code:

//////////////////////////////////////// /// // CSnakeView message handlersvoid CSnakeView:: display (int image [20] [20], CDC * pDC) {int nSize = 20; CRect rect; CRect rc; CBitmap bitmap9; bitmap9.LoadBitmap (IDB_BITMAP8); CBrush brush9; round (& bitmap9); GetClientRect (& rect); pDC-> FillRect (rect, & brush9); // pDC-> Rectangle (421,421,); CBitmap bitmap10; bitmap1 0. loadBitmap (IDB_BITMAP9); CBrush brush10; substring (& bitmap10); CRect rec1, rec2, rec3, rec4; rec1 = CRect (,); rec2 = CRect (, 20, 440,440); rec3 = CRect (20,440, 20,420,440,440); rec4 = CRect (); pDC-> FillRect (rec1, & brush10); pDC-> FillRect (rec2, & brush10); pDC-> FillRect (rec3, & brush10); pDC-> FillRect (rec4, & brush10); for (I = 0; I <20; I ++) {for (j = 0; j <20; j ++) {rc = CRect (j * nSize + 20, I * nSize + 20, (j + 1) * n Size + 20, (I + 1) * nSize + 20); if (image [I] [j]! = 0) {if (image [I] [j] = 3) {CBitmap bitmap2; bitmap2.LoadBitmap (IDB_BITMAP3); CBrush brush1; brush1.CreatePatternBrush (& bitmap2 ); CBrush * pOldBrush = pDC-> SelectObject (& brush1); pDC-> Ellipse (rc); pDC-> SelectObject (pOldBrush); // pDC-> FillRect (rc, & brush1);} else if (image [I] [j] = 1) {if (direction = 2) {CBitmap bitmap; bitmap. loadBitmap (IDB_BITMAP1); CBrush brush; brush. createPatternBrush (& bitmap); pDC-> FillRect (rc, & brush);} else if (direction = 4) {CBitmap bitmap4; bitmap4.LoadBitmap (IDB_BITMAP5); CBrush brush4; transform (& bitmap4); pDC-> FillRect (rc, & brush4);} else if (direction = 6) {CBitmap bitmap5; bitmap5.LoadBitmap (IDB_BITMAP6); CBrush brush5; transform (& bitmap5); pDC-> FillRect (rc, & brush5);} else if (direction = 8) {CBitmap bitmap6; bitmap6.LoadBitmap (IDB_BITMAP7); CBrush brush6; brush6.CreatePatternBrush (& bitmap6); pDC-> FillRect (rc, & brush6) ;}} else if (image [I] [j] == 2) {CBitmap bitmap3; bitmap3.LoadBitmap (IDB_BITMAP4); CBrush brush3; brush3.CreatePatternBrush (& bitmap3); CBrush * pOldBrush = pDC-> SelectObject (& brush3); pDC-> Ellipse (rc ); pDC-> SelectObject (pOldBrush); // pDC-> FillRect (rc, & brush3) ;}}} CBitmap bitmap11; bitmap11.LoadBitmap (IDB_BITMAP10); CBrush brush11; brush11.CreatePatternBrush (& bitmap11); CRect rec5; rec5 = CRect (540,495, 100); pDC-> FillRect (rec5, & brush11); char buf []; // GotoXY (); sprintf (buf, "SCORE: %-5d", score); // record SCORE // pDC-> SetBkMode (TRANSPARENT ); pDC-> TextOut (445,120, buf); // GotoXY (); sprintf (buf, "LEVEL = %-5d", level ); // record grade pDC-> TextOut (445,150, buf );}

3) To avoid flickering screens, the dual-buffering mechanism is used.

Code:

Void CSnakeView: OnDraw (CDC * pDC) {CSnakeDoc * pDoc = GetDocument (); ASSERT_VALID (pDoc); int m_nWidth, m_nHeight; CDC m_memDC; CBitmap m_memBmp; // 1. used to map the memory environment of the screen // obtain the size of the game window. Used to set the following memory size CRect windowRect; GetClientRect (& windowRect); m_nWidth = windowRect. width (); m_nHeight = windowRect. height (); // The memory device environment is associated with the screen memory environment m_memDC.CreateCompatibleDC (pDC); // The memory is associated with the screen for the image. The hero is the size of the game window m_membmp .createcompatiblebitmap (pDC, m_nWidth, m_nHeight); // The memory device environment is associated with the memory bitmap, so that m_memDC can draw m_memDC.FillSolidRect (windowRect, RGB (255,255,255); m_memDC.SelectObject (& m_memBmp ); display (image, & m_memDC); // copy the image in the memory DC to the computer screen pDC-> BitBlt (, m_nWidth, m_nHeight, & m_memDC, SRCCOPY ); m_memDC.DeleteDC (); // Delete dcm_membmp .deleteobject (); // delete a bitmap // TODO: add draw code for native data here}

4) The other is to use the key response function and Timer time control in VC, and the other is similar to C ++.

Reprinted please indicate the source: http://blog.csdn.net/u012027907

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.