使用MFC做贪吃蛇小游戏

1、先启动vs2013 -- >新建项目 -->选择一个MFC应用程序 -->填上名称以及项目的位置 -->确定

使用MFC做贪吃蛇小游戏_第1张图片

2、在应用程序向导中选择基于对话框的应用程序 -->完成。

使用MFC做贪吃蛇小游戏_第2张图片

3、在资源视图中,把向导为我们生成的控件删掉。

使用MFC做贪吃蛇小游戏_第3张图片

4、然后按F5运行一下程序。运行结果如下。一个空白的对话框。

使用MFC做贪吃蛇小游戏_第4张图片

5、下面就要开始在这个对话框上面制作我们的游戏界面了!

首先,我们需要做一下游戏界面的规划,在这里代码部分讲解


初始化贪吃蛇起初有3个节点,长度为3,起始坐标;
     食物默认为 1无0有 当然我更加喜欢0无1有*/

void  CMFC_SNAKEView::OnInitialUpdate()
{
	CView::OnInitialUpdate();
	Snake[0].x = 10;
	Snake[0].y = 10;
	Snake[1].x = 11;
	Snake[1].y = 10;
	Snake[2].x = 12;
	Snake[2].y = 10;
	Snake[0].direct = 3;
	Snake[0].len = 3;
	Food.isfood = 1;
	// TODO: Add your specialized code here and/or call the base class


}

/*对OnKeyDown()具体添加代码*/
void CMFC_SNAKEView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	switch (nChar)
	{
	case VK_UP:if (Snake[0].direct != 2)Snake[0].direct = 1; break;
	case VK_DOWN:if (Snake[0].direct != 1)Snake[0].direct = 2; break;
	case VK_LEFT:if (Snake[0].direct != 4)Snake[0].direct = 3; break;
	case VK_RIGHT:if (Snake[0].direct != 3)Snake[0].direct = 4; break;
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
/*Step 3*/
void CMFC_SNAKEView::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	// TODO: Add your message handler code here and/or call default
	CString str;
	
	str.Format(_T("%d,%d"), point.x, point.y);
	AfxMessageBox(str);
	CView::OnRButtonDown(nFlags, point);
	CView::OnRButtonDown(nFlags, point);
	CView::OnRButtonDown(nFlags, point);
}


/*Step 4*/
int CMFC_SNAKEView::oninit()
{
	CDC *pDC = GetDC();
	CBrush DrawBrush = (RGB(100, 100, 100));
	CBrush *Drawbrush = pDC->SelectObject(&DrawBrush);
	for (int i = 0; i <= Snake[0].len - 1; i++)
		pDC->Rectangle(Snake[i].x * 20, Snake[i].y * 20, (Snake[i].x + 1) * 20, (Snake[i].y + 1) * 20);
	pDC->SelectObject(DrawBrush);
	return 0;/*此句在编译时出现必须有返回值 自己加的return 0,需要注意*/

}

/*Step 5 控件添加代码*/
//添加计时器
void CMFC_SNAKEView::OnStart()
{
	// TODO: Add your command handler code here
	SetTimer(1, 3000, NULL);
	AfxMessageBox(_T("3秒后开始游戏!"));
}
//添加暂停按钮
void CMFC_SNAKEView::OnPause()
{
	// TODO: Add your command handler code here
	KillTimer(1);
	AfxMessageBox(_T("暂停游戏..."));
}
//添加退出按钮
void CMFC_SNAKEView::OnExit()
{
	// TODO: Add your command handler code here
	AfxMessageBox(_T("退出游戏..."));
	exit ;
}
void CMFC_SNAKEView::OnContinue()
{
	// TODO: Add your command handler code here
	SetTimer(1, 10, NULL);
	//第一个参数用于表示要设置的Timer事件的ID,用于区分存在多个Timer的情况
	//第二个参数表示时间间隔,多长时间会触发一次这个事件,以毫秒为单位
	//第三个参数表示回调函数,当事件发生的时候会调用这个函数,如果这个参数为空,当事件发生的时候,将向应用程序的消息队列发送WM_TIMER消息,一般都不写回调函数,直接处理CWnd的WM_TIMER消息就可以了
}

/*Step 6 对OnDraw()的添加代码*/
//画蛇身选颜色
void CMFC_SNAKEView::OnDraw(CDC* pDC)
{
	CMFC_SNAKEDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CBrush backBrush(RGB(100, 100, 0));
	CBrush* pOldBrush = pDC->SelectObject(&backBrush);
	CRect rect;
	pDC->GetClipBox(&rect);
	pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
	pDC->SelectObject(pOldBrush);

	pDC->Rectangle(19, 19, 501, 501);
	oninit();
}
//游戏进行后不同阶段游戏难度不同(吃食物后增加蛇移动速度)
/*Step 7 对OnTime()的添加代码*/
void CMFC_SNAKEView::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CDC *pDC = GetDC();
	CString soure;

	if (Snake[0].len == 3)
	{
		SetTimer(1, 370, NULL);
	}
	
	if (Snake[0].len == 6)
	{
		SetTimer(1, 270, NULL);
	}
	
	if (Snake[0].len == 9)
	{
		SetTimer(1, 200, NULL);
	}
	
	if (Snake[0].len == 12)
	{
		SetTimer(1, 100, NULL);
	}
	
	soure.Format(_T("得分:%d!", (Snake[0].len - 3) * 10));
	//撞界判断
	if (Snake[0].x * 20 <= 37 || Snake[0].y * 20 <= 37 || Snake[0].x * 20 >= 462 || Snake[0].y * 20 >= 462)
	{
		KillTimer(1);
		AfxMessageBox(soure);
		// s=0;
	}
	//蛇身相撞判断
	if (Snake[0].len>3)
		for (int sn = Snake[0].len - 1; sn>0; sn--)
		{
			if (Snake[0].x * 20 == Snake[sn].x * 20 && Snake[0].y * 20 == Snake[sn].y * 20)
			{
				KillTimer(1);
				AfxMessageBox(soure);
				//  s=0;
			}
		}
	///////////////////////////////////////////////////////////////////////////
	pDC->SelectStockObject(WHITE_PEN);
	pDC->Rectangle(Snake[Snake[0].len - 1].x * 20, Snake[Snake[0].len - 1].y * 20, (Snake[Snake[0].len - 1].x + 1) * 20, (Snake[Snake[0].len - 1].y + 1) * 20);
	for (int i = Snake[0].len - 1; i>0; i--)
	{
		Snake[i].x = Snake[i - 1].x;
		Snake[i].y = Snake[i - 1].y;
	}
	//行走方向判断
	if (Snake[0].direct == 1)Snake[0].y--;
	if (Snake[0].direct == 2)Snake[0].y++;
	if (Snake[0].direct == 3)Snake[0].x--;
	if (Snake[0].direct == 4)Snake[0].x++;
	pDC->SelectStockObject(BLACK_PEN);

	CBrush DrawBrush = (RGB(100, 100, 100));
	CBrush *Drawbrush = pDC->SelectObject(&DrawBrush);
	pDC->Rectangle(Snake[0].x * 20, Snake[0].y * 20, (Snake[0].x + 1) * 20, (Snake[0].y + 1) * 20);
	pDC->SelectObject(DrawBrush);

	//判断吃豆的条件,撞到就吃
	if (Snake[0].x * 20 == Food.x * 20 && Snake[0].y * 20 == Food.y * 20)
	{
		Snake[0].len++;
		Food.isfood = 1;
		Snake[Snake[0].len - 1].x = Snake[Snake[0].len - 2].x;
		Snake[Snake[0].len - 1].y = Snake[Snake[0].len - 2].y;
	}
	//如果食物被吃了 就生成
	if (Food.isfood == 1)
	{
		srand((unsigned)time(NULL));
		do
		{
			for (int isfo = Snake[0].len - 1; isfo >= 0; isfo--)
				if (Snake[0].x * 20 == Snake[isfo].x * 20 && Snake[0].y * 20 == Snake[isfo].y * 20)
				{
					Food.x = rand() % 25 ;
					Food.y = rand() % 25;
				}
		} while (Food.x * 20<70 || Food.y * 20<70 || Food.x * 20>430 || Food.y * 20>430);
		pDC->Rectangle(Food.x * 20, Food.y * 20, (Food.x + 1) * 20, (Food.y + 1) * 20);
		Food.isfood = 0;
	}

	CView::OnTimer(nIDEvent);
}
使用MFC做贪吃蛇小游戏_第5张图片


使用MFC做贪吃蛇小游戏_第6张图片 使用MFC做贪吃蛇小游戏_第7张图片

你可能感兴趣的:(小游戏编写)