⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renzhe2view.cpp

📁 贪吃蛇游戏源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	t_rect=CRect(rect3.right-80,15,rect3.right,40);
	for(int i=pDoc->m_long;i>=2;i--)
	{
		pDoc->rect[i]=pDoc->rect[i-1];
	}
	if(pDoc->m_long>=1)
	    pDoc->rect[1]=pDoc->m_rect;
	if(pDoc->l_yundong==1)
	{
		if(pDoc->rect[0].left>rect3.left)
		{
		    pDoc->L_point.x-=10;
		    pDoc->R_point.x-=10;
		}
		else
		{
			pDoc->game_zhongzhi=1;
			KillTimer(1);
		}
	}
	else if(pDoc->r_yundong==1)
	{
		if(pDoc->rect[0].right<rect3.right)
		{
		    pDoc->L_point.x+=10;
		    pDoc->R_point.x+=10;
		}
		else
		{
			pDoc->game_zhongzhi=1;
			KillTimer(1);
		}
	}
	else if(pDoc->u_yundong==1)
	{
		if(pDoc->rect[0].top>rect3.top+50)
		{
		   pDoc->L_point.y-=10;
		   pDoc->R_point.y-=10;
		}
		else
		{
			pDoc->game_zhongzhi=1;
			KillTimer(1);
		}
	}
	else if(pDoc->d_yundong==1)
	{
		if(pDoc->rect[0].bottom<rect3.bottom)
		{
		   pDoc->L_point.y+=10;
		   pDoc->R_point.y+=10;
		}
		else
		{
			pDoc->game_zhongzhi=1;
			KillTimer(1);
		}
	}
	/*for(int p=1;p<=pDoc->m_long;p++)                //如果蛇撞倒自己则游戏终止
		if(pDoc->rect[0]==pDoc->rect[p])
		{
           pDoc->game_zhongzhi=1;
           KillTimer(1);
		}*/
	pDoc->m_rect=CRect(pDoc->L_point,pDoc->R_point);
	 //pDoc->m_rect1=CRect(pDoc->rect[pDoc->m_long].left,pDoc->rect[pDoc->m_long].top,pDoc->m_rect.right,pDoc->m_rect.bottom);//注意要把范围扩大,这样可保证重绘的范围
	 //pDoc->m_rect1.left-=50;pDoc->m_rect1.top-=50;pDoc->m_rect1.right+=50;pDoc->m_rect1.bottom+=50;    //使重绘区扩大,确保重绘效果
	pDoc->rect[0]=pDoc->m_rect;
	
	InvalidateRect(rect1);
	pDoc->L_point2=CPoint(pDoc->L_point.x,pDoc->L_point.y+10);
    pDoc->R_point2=CPoint(pDoc->R_point.x,pDoc->R_point.y-10);
	for(int p=1;p<=pDoc->m_long;p++)                //如果蛇撞倒自己则游戏终止
		if(pDoc->rect[0]==pDoc->rect[p])
		{
           pDoc->game_zhongzhi=1;
           KillTimer(1);
		}
		if(pDoc->m_doushu>0)                                //作用是在豆数为0时,不进行下面的语句(否则!(pDoc->douzi[pDoc->m_doushu-1].dou_cunzai)无意义)
		{
	         while(!(pDoc->douzi[pDoc->m_doushu-1].dou_cunzai))                           //循环的目的是如果豆子的位置与蛇重合,则重新产生豆子
			 {
                 if(pDoc->zhongzhi==0&&pDoc->douzi[pDoc->m_doushu].dou_cunzai==1)
				 {//int x=rand()%(500);
		             int x=rand()%(rect3.right-10)+10;
		             int y=(rand()%(rect3.bottom-70))+70;                   //让豆子产生在说明栏以下
		             pDoc->douzi[pDoc->m_doushu].dou_rect=CRect(x-10,y-10,x,y);
		             //pDoc->m_doushu++;
		             pDoc->douzi[pDoc->m_doushu].dou_cunzai=0;
				 }
	             for(int j=0;j<=pDoc->m_long;j++)
		            if(pDoc->douzi[pDoc->m_doushu-1].dou_rect==pDoc->rect[j])
			           pDoc->dou_weizhi=1;
		            else
                       pDoc->dou_weizhi=0;
		         if(pDoc->dou_weizhi==0)
			        break;
			 }
		}	
		//InvalidateRect(pDoc->douzi[pDoc->m_doushu-1].dou_rect);
	if(pDoc->douzi[pDoc->m_doushu].dou_rect.PtInRect(pDoc->L_point)||pDoc->douzi[pDoc->m_doushu].dou_rect.PtInRect(pDoc->L_point2)||pDoc->douzi[pDoc->m_doushu].dou_rect.PtInRect(pDoc->R_point)||pDoc->douzi[pDoc->m_doushu].dou_rect.PtInRect(pDoc->R_point2))
	{
		
		pDoc->m_long++;
		pDoc->m_doushu++;
		pDoc->douzi[pDoc->m_doushu].dou_cunzai=1;//InvalidateRect(pDoc->douzi[pDoc->m_doushu-1].dou_rect);
		pDoc->douzi[pDoc->m_doushu-1].dou_cunzai=0;
		pDoc->m_score=((pDoc->m_long)*10);
		InvalidateRect(s_rect);                      //对得分进行重绘
		
	}
	pDoc->time+=0.1;
	//InvalidateRect(s_rect);
	InvalidateRect(t_rect);                           //对时间进行重绘
	CView::OnTimer(nIDEvent);
}

/*void CRenzhe2View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
		
			
			
			    SetTimer(1,50,0);
				//pDoc->zhongzhi=1;
			
			else
			{
				KillTimer(1);
				pDoc->zhongzhi=0;
			}
			
		}
	CView::OnLButtonDown(nFlags, point);
}*/



void CRenzhe2View::OnMENUzhuce() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->m_register==0)
	{
	   pDoc->m_dialog=1;
	   Invalidate();
	   /*CRect rect1;
	   GetClientRect(&rect1);
	   CRect rect2(0,0,rect1.right,50);
	   InvalidateRect(rect2);*/
	   pDoc->m_register=1;
	}
	else
		MessageBox("你已注册");

	
}

void CRenzhe2View::OnPrimary() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=90;
	
}

void CRenzhe2View::OnSenior() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=20;
}

void CRenzhe2View::OnJunior() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=40;
}

void CRenzhe2View::OnGameWudi() 
{
	// TODO: Add your command handler code here
     CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=10;
	
}

void CRenzhe2View::OnGameScore100() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_totlescore=100;
}

void CRenzhe2View::OnGameScore200() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_totlescore=200;
}

void CRenzhe2View::OnGameScore500() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_totlescore=500;
}

void CRenzhe2View::OnInstructions() 
{
	// TODO: Add your command handler code here
	MessageBox("游戏开始前可进行注册,游戏选关,分数选择。如果不进行操作,则默认游戏等级:初级,分数:200.游戏中可改变,但要暂停游戏一次才有效");
	
}

void CRenzhe2View::OnContextMenu(CWnd*, CPoint point)
{
	// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1){			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);			point = rect.TopLeft();			point.Offset(5, 5);		}		CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_RENZHE2_VIEW));		CMenu* pPopup = menu.GetSubMenu(0);		ASSERT(pPopup != NULL);		CWnd* pWndPopupOwner = this;		while (pWndPopupOwner->GetStyle() & WS_CHILD)			pWndPopupOwner = pWndPopupOwner->GetParent();		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,			pWndPopupOwner);	}
}

void CRenzhe2View::OnGJunior() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=40;
	
}

void CRenzhe2View::OnGPrimer() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=90;
	
	
}

void CRenzhe2View::OnGSenior() 
{
	// TODO: Add your command handler code here
	CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=20;
	
}

void CRenzhe2View::OnGtianxiawudi() 
{
	// TODO: Add your command handler code here
	 CRenzhe2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_gamelevel=10;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -