MFC realizes lianliankan game map display

  • 2020-06-15 09:49:39
  • OfStack

MFC realized lianlianzhan in the early process of the game encountered a big problem is how to show the map, finally looked at other people's source code to solve.

The first is the generation of map array. There are a lot of maps on the Internet. I used the type of randomly generated map and put it in two consecutive places.


void CGameDlg::InitMap() 
{
 for (int i = 0; i < MAX_X; i++) //  Initialize the map An array of 
 {
  for (int j = 0; j < MAX_Y; j++)
  {
   map[i][j] = 0;
  }
 }

 // Random number seed 
 srand((unsigned int)time(NULL));

 for (int i = 1; i < MAX_X - 1; i++)
 {
  for (int j = 1; j < MAX_Y - 1; j = j+2)
  {
   int type = rand() % m_typeNum + 1;//  Randomly generated 1 The picture type number of 
   map[i][j] = type;
   map[i][j+1] = type; //  Make sure the same image appears twice in a row 
  }
 }
 int k = 0;
 while (k < 100) //  Randomly select two positions to swap 100 time 
 {
  int x1 = 0, y1 = 0;
  int x2 = 0, y2 = 0;
  while (x1 == x2 && y1 == y2) //  Make sure the two positions are different 
  {
   x1 = rand() % (MAX_X - 2) + 1;
   y1 = rand() % (MAX_Y - 2) + 1;

   x2 = rand() % (MAX_X - 2) + 1;
   y2 = rand() % (MAX_Y - 2) + 1;
  }
  int temp = map[x1][y1];
  map[x1][y1] = map[x2][y2];
  map[x2][y2] = temp;
  k++;
 }
}

Where MAX_X and MAX_X are macro definitions, it should be noted that there is no image in the outer layer of the map array, and the elimination algorithm later will be more convenient.

Here's what the map shows:


void CGameDlg::ShowMap()
{
 int i, j;
 CPoint p; //  Button position 
 CString str = _T("");
 // Clear original button 
 for (i = 0; i<m_btnGroup.GetSize(); i++)
  delete (CLLKButton *)m_btnGroup.GetAt(i);
 m_btnGroup.RemoveAll();
 // Add a new button 
 for (i = 1; i <= MAX_X - 2; i++)
  for (j = 1; j <= MAX_Y - 2; j++)
  {
   p.x = i;
   p.y = j;
   //arr[map[i][j] - 1]++;
   // Put the button in m_btnGroup Pointer array 
   m_btnGroup.Add(new CLLKButton(map[i][j], p));
  }
 // According to the button 
 for (i = 0; i<(MAX_X - 2)*(MAX_Y - 2); i++)
 {
  CLLKButton *btn = (CLLKButton *)m_btnGroup.GetAt(i);
  if (btn->ID > 0)
  {
   str.Format(_T("res\\%d.png"), btn->ID);

   CImage image;
   image.Load(str);

   btn->Create(str, WS_CHILD | BS_BITMAP | WS_VISIBLE,
    CRect(70 + (i % (MAX_Y - 2)) * 50, 70 + (i / (MAX_Y - 2)) * 50,
    120 + (i % (MAX_Y - 2)) * 50, 120 + (i / (MAX_Y - 2)) * 50), this,
    IDC_BLOCK + i);
   btn->SetBitmap(image);
   btn->ShowWindow(SW_SHOW);
  }
 }

}

In the process of generating the map, I did not put the image together and used the mask to eliminate the background color. Instead, I simply loaded the image onto the button, but since the.png image can achieve transparency (.bmp image cannot achieve transparency, it will have a white background), so it is a transparent background.
In addition, I overwrote a new CLLKButton class to inherit from CButton class by adding two attributes:


int ID ;  //  Types of pictures 
CPoint p; //  Button position 

The complete source code has been uploaded to my GitHub


Related articles: