MFC learning 2048 small game program source code

The development principle of the 2048 game is relatively simple. It is based on a 4x4 grid. By controlling the movement of the digital cubes, the same digital cubes are synthesized and new digital cubes are generated.

The specific implementation process is as follows:

Determining requirements: First, you need to clarify the functional requirements of the game, such as generating random digital cubes, controlling the movement of digital cubes, and detecting collisions.
Interface design: Design the interface of the game, including digital cubes, moving directions, scores and other elements.
Logic implementation: Realize the logic part of the game, including the generation of digital cubes, movement and collision detection, and score recording and updating.

insert image description here

Here it is necessary to manually add a "PreTranslateMessage" message processing function to identify keyboard keystrokes.

In Windows programming, the Message Handling Function (Message Handling Function) is a function used to process messages sent to applications by the Windows operating system. Among them, "PreTranslateMessage" is a message processing function, which is used to preprocess the message before passing the message to the window procedure.

To add a "PreTranslateMessage" message handler, the following steps are required:

Open your window class definition file (usually a file named "window_class_name.rc") in the resource editor.

In the resource editor, find the window, right-click and select "Class Wizard", and then find "PreTranslateMessage" from the virtual function.

In the new message handler function, write your code to process the message. The prototype of this function is as follows:

BOOL PreTranslateMessage(MSG* pMsg)

Among them, pMsg is a pointer to the MSG structure, which contains information about the message, such as the type and parameters of the message.

In the "PreTranslateMessage" function, you can write code to process the message as needed. For example, you can intercept or modify specific messages by checking their type.
Save and close the definition file for the window class.

The main function of the "PreTranslateMessage" message processing function is to allow you to modify or intercept the message before the window procedure (Window Procedure). Through this function, you can perform some preprocessing operations on the message, such as modifying the parameters of the message, or passing the message to other functions for processing as needed.

insert image description here

Main function source code:

void CMy2048MFCDlg::Show()
{
    
    
	//所有方块的种类,封装到一个Node数组中
	const static Node color[]={
    
    
			0,		RGB(204,192,179),	RGB(204,192,179),
			2,		RGB(238,230,210),	RGB(119,110,100),
			4,		RGB(237,224,200),	RGB(119,110,100),
			8,		RGB(242,177,121),	RGB(249,242,242),
			16,		RGB(245,148,100),	RGB(249,242,242),
			32,		RGB(246,124,95),	RGB(249,242,242),
			64,		RGB(246,94,59),		RGB(249,242,242),
			128,	RGB(237,207,114),	RGB(249,242,242),
			256,	RGB(237,204,97),	RGB(249,242,242),
			512,	RGB(237,200,80),	RGB(249,242,242),
			1024,	RGB(237,197,63),	RGB(249,242,242),
			2048,	RGB(43,132,98),		RGB(249,242,242),
			4096,	RGB(250,56,108),	RGB(249,242,242),
			8192,	RGB(129,148,200),	RGB(249,242,242),
			16384,	RGB(255,0,0),		RGB(249,242,242),
			32768,	RGB(0,255,0),		RGB(249,242,242),
			65536,	RGB(128,128,0),		RGB(249,242,242),
	};
	static const int n_block_size = 120;			//方块大小
	static const int n_pos_x = 14;		            //显示位置
	static const int n_pos_y = 14;
	static const int n_gap_size = 10;		     	//间隙大小

	RECT rect;
	GetClientRect(&rect);
	m_dc.FillSolidRect(&rect,RGB(255,255,255));    //把整个客户区填充为白色背景

	m_dc.SelectObject(&m_font);
	CBrush bkBrush(RGB(187,173,160));
	m_dc.SelectObject(&bkBrush);
	//qrt:4*4的棋盘背景矩形大小
	RECT back_square;
	back_square.top = n_pos_x-n_gap_size;
	back_square.left = n_pos_y-n_gap_size;
	back_square.bottom = n_pos_x+4*n_gap_size+4*n_block_size;
	back_square.right = n_pos_y+4*n_gap_size+4*n_block_size;
	//使用当前笔绘制圆角矩形,用当前画刷填充 ,8*8的point是圆角的宽度
	m_dc.RoundRect(&back_square,CPoint(8,8));
	//重绘整个棋盘中16个块的矩形
	for(int i=0;i<4;i++)
	{
    
    
		for(int k=0;k<4;k++)
		{
    
    
			//取出棋盘中的数
			int num = m_nChessBoard[i][k];
			//得到棋盘数字对应的Node位置
			int n = GetNodePos(num);
			//定义数字所对应的画刷的颜色
			CBrush brush(color[n].m_BackColor);
			m_dc.SelectObject(&brush);
			//显示字体
			m_dc.SetTextColor(color[n].m_FontColor);
			//每个方块rt的矩形大小
			RECT rt;
			rt.left = n_pos_x+i*(n_block_size+n_gap_size);
			rt.top = n_pos_y+k*(n_block_size+n_gap_size);
			rt.right = rt.left+n_block_size;
			rt.bottom = rt.top+n_block_size;
			//放大步骤
			if(i*4+k == m_nNewPos)
			{
    
    
				const static int b[]={
    
    
					-24,-20,-16,-12,-8,-4,0,
				};
				rt.left  -= b[m_nCount];
				rt.right += b[m_nCount];
				rt.top   -= b[m_nCount];
				rt.bottom+= b[m_nCount];
				m_nCount++;
				if(m_nCount > sizeof(b)/sizeof(int))
				{
    
    
					m_nNewPos=-1;
				}
			}

			m_dc.RoundRect(&rt,CPoint(16,16));
			if(num>0)
			{
    
    
				//1.将棋盘中的数字转换为字符串
				//2.存入到str中,并画在矩形框rt中
				CString str;
				char  temp[10] = {
    
    0};
				_itoa_s(num,temp,10);
				str = temp;
				//单行 - 居中显示 - 垂直居中显示
				m_dc.DrawText(str,&rt,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
			}
		}
	}
	//分数
	CBrush brush(RGB(187,173,160));
	m_dc.SelectObject(&brush);
	m_dc.SetTextColor(RGB(238,235,232));
	RECT rt={
    
    580,170,760,300};
	m_dc.RoundRect(rt.left,rt.top,rt.right,rt.bottom,8,8);
	CString str;
	str="score";
	rt.bottom = (rt.bottom - rt.top)/2 + rt.top;
	rt.top += 10;
	rt.bottom += 10;
	//显示“score”
	m_dc.DrawText(str,&rt,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	char  tempScore[10] = {
    
    0};
	_itoa_s(m_nScore,tempScore,10);
	str = tempScore;
	rt.top += 50;
	rt.bottom += 50;
	//显示分数
	m_dc.DrawText(str,&rt,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	//判断游戏是否结束
	if(m_nOver)
	{
    
    
		CFont Over;
		//初始化字体:字体高度,字体宽度,夹角,夹角,字体磅数200,斜体,下划线,突出,字体的字符集
		//输出精度,剪贴精度,输出质量,字体的间距,字体类型
		Over.CreateFont(80,32,0,0,FW_EXTRABOLD,false,false,false,ANSI_CHARSET,
			OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY,FF_MODERN,L"Arial");
		m_dc.SelectObject(&Over);
		m_dc.SetTextColor(RGB(250,0,0));
		str="游戏结束";
		m_dc.DrawText(str,&back_square,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	CDC *dc=GetDC();
	dc->BitBlt(0,0,rect.right,rect.bottom,&m_dc,0,0,SRCCOPY);
	ReleaseDC(dc);
}

Complete program code: MFC/C++ small game source code 2048 small game

Guess you like

Origin blog.csdn.net/u014740628/article/details/131724099