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

📄 zllkview.cpp

📁 用VC写的连连看小游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		break;
	case VK_F7:
        OnMenuStop();
		break;
	default:
		break;
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CZLLKView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int  iTop,iLeft,iRight,iBottom;
	CRect  rect;
	iTop=DRAW_TOP+BMP_HEIGHT;   iBottom=iTop+BMP_HEIGHT*ROW_NUMBER;
    iLeft=DRAW_LEFT+BMP_WIDTH;  iRight=iLeft+BMP_WIDTH*LINE_NUMBER;
	//进入了游戏,并且鼠标在这个范围内
	if(bingame&&point.x>iLeft&&point.x<iRight&&point.y>iTop&&point.y<iBottom)
	{
	   	if(bsoundeffect)
	    	PlaySound(MAKEINTRESOURCE(IDR_WAVE_DOWN),AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC);
     	int tempx,tempy;  // 地图中的行列值
		int xtempindex,ytempindex;
	  	tempy=point.x/BMP_WIDTH;
	  	tempx=point.y/BMP_HEIGHT;
		xtempindex=tempx-1;
        ytempindex=tempy-2;
        CString str;
		//所点击图形编号大于0 就是此位置有图像
		if(llk_map[xtempindex][ytempindex].PicIndex>0)
		{
			if(bSecondClicked) //是第二次点击
			{

		        //str.Format("%d,%d,%d,%d,%d,%d",xtempindex,ytempindex,xindex,yindex,llk_map[xtempindex][ytempindex].PicIndex,llk_map[xindex][yindex].PicIndex);
		    	//AfxMessageBox(str);
		    	//判断点击的是不是上次点击的图片
		    	if((xtempindex==xindex)&&(ytempindex==yindex))//如果是
				{
		     		llk_map[xtempindex][ytempindex].state=0;		
					rect.top=tiley(xtempindex); rect.bottom=rect.top+PIC_HEIGHT;
			    	rect.left=tilex(ytempindex); rect.right=rect.left+PIC_WIDTH;
			    	InvalidateRect(&rect,TRUE);
					//DrawFace(xtempindex,ytempindex,llk_map[xtempindex][ytempindex].PicIndex,0);
				}
		     	else//如果不是上次点击的图片 判断图片类型是否相同 
				{

			     	if(llk_map[xtempindex][ytempindex].PicIndex==llk_map[xindex][yindex].PicIndex)
					{         
			      		//如果图片类型相同:判断是否连通(xindex yindex)   (xtempindex  ytempindex)
				    	bsuccess=CheckConnection(xindex,yindex,xtempindex,ytempindex);
				    	if(bsuccess)  //如果连通 则消除 并且更新图片位置列表
						{	
							//判断连击次数  
							newtime=timeGetTime();
							if((newtime-oldtime)>2000)
								m_dotNumber=0;
							else m_dotNumber++;	
							oldtime=newtime;						
							//如果连击次数大于0
				           	//更新用户分数
					       	g_userscore+=m_dotNumber*50;
							g_userscore+=100;
	                    	DrawInfo2(560,40,g_userscore);
							// 更新连机次数
							CString  str;
							if(m_dotNumber>0)
							{
		    					str.Format("连击%d次",m_dotNumber);
	        					DrawInfo3(140,70,str);
							}
							//更新滚动条
							m_nUpper+=5;
					    	//更新图片位置列表
					    	ReleasePicPlace(llk_map[xindex][yindex].PicIndex,xindex,yindex);
                         	ReleasePicPlace(llk_map[xtempindex][ytempindex].PicIndex,xtempindex,ytempindex);
					   	    if(bsoundeffect)
	                        	PlaySound(MAKEINTRESOURCE(IDR_WAVE_OK),AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC);
					    	//把该位置图片号设为0 
					    	llk_map[xtempindex][ytempindex].PicIndex=0;
                            llk_map[xindex][yindex].PicIndex=0;	
			          		rect.top=tiley(xtempindex); rect.bottom=rect.top+PIC_HEIGHT;
			            	rect.left=tilex(ytempindex); rect.right=rect.left+PIC_WIDTH;
			            	InvalidateRect(&rect,TRUE);
                            //DrawFaceBlank(xtempindex,ytempindex);
							rect.top=tiley(xindex); rect.bottom=rect.top+PIC_HEIGHT;
			            	rect.left=tilex(yindex); rect.right=rect.left+PIC_WIDTH;
			                InvalidateRect(&rect,TRUE);
                            //DrawFaceBlank(xindex,yindex);
							//判断位置事件
							if(llk_map[xtempindex][ytempindex].event==1||llk_map[xindex][yindex].event==1)
							{
								m_tishinumber++;
								DrawInfo3(140,70,"将奖励提示1次");
							}
							else if (llk_map[xtempindex][ytempindex].event==2||llk_map[xindex][yindex].event==2) 
							{
								m_sortnumber++;
								DrawInfo3(140,70,"将奖重列1次");
							}
							else if(llk_map[xtempindex][ytempindex].event==3||llk_map[xindex][yindex].event==3) 
							{
								g_userscore+=100;
								DrawInfo3(140,70,"将奖分数100");								
							}
						}
				    	else  //  不连通 
						{
					       	if(bsoundeffect)
	                        	PlaySound(MAKEINTRESOURCE(IDR_WAVE_NO),AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC);
						    llk_map[xindex][yindex].state=0;
							rect.top=tiley(xindex); rect.bottom=rect.top+PIC_HEIGHT;
			            	rect.left=tilex(yindex); rect.right=rect.left+PIC_WIDTH;
			            	InvalidateRect(&rect,TRUE);
						}
					}
				    else  //图片类型不同
					{
			            if(bsoundeffect)
	                    	PlaySound(MAKEINTRESOURCE(IDR_WAVE_NO),AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC);
				    	llk_map[xindex][yindex].state=0;
			        	rect.top=tiley(xindex); rect.bottom=rect.top+PIC_HEIGHT;
			            rect.left=tilex(yindex); rect.right=rect.left+PIC_WIDTH;
			           	InvalidateRect(&rect,TRUE);
						//DrawFace(xindex,yindex,llk_map[xindex][yindex].PicIndex,0);
					}
				}
		    	xindex=-1;
		    	yindex=-1;
			}
		    else  //是第一次点击
			{
		    	llk_map[xtempindex][ytempindex].state=1;
				rect.top=tiley(xtempindex); rect.bottom=rect.top+PIC_HEIGHT;
				rect.left=tilex(ytempindex); rect.right=rect.left+PIC_WIDTH;
				InvalidateRect(&rect,TRUE);
				//DrawFace(xtempindex,ytempindex,llk_map[xtempindex][ytempindex].PicIndex,1);
		    	xindex=xtempindex;
		    	yindex=ytempindex;
			}
		}		
	
		//判断是否消完,弹出对话框 纪录分数
		if(IsPicEmpty())  //已经消完
		{
			//PlaySound(MAKEINTRESOURCE(IDR_WAVE_WIN),AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC);
			bingame=false;
			KillTimer(1);
			//对话框
	    	CAddDlg adddlg;
	    	if(adddlg.DoModal()==IDOK)
			{
				OnMenuBack();
			}
		}
		else    //(!IsPicEmpty()) //没有消完
		{
			//判断是否无解
            if(IsNoConnection())
			{
		    	AfxMessageBox("已经无法消除",MB_OK);
                OnMenuBack();
			}
		}

	    bSecondClicked=!bSecondClicked;
	}
	CView::OnLButtonDown(nFlags, point);
}
bool CZLLKView::CheckConnection(int x1, int y1, int x2, int y2)
{
	int i,temp;
	//分为八种
	if((x1==x2))//同一行
	{		
		if(IsLineConnection(x1,y1,x2,y2))  	return true;
		else
		{
			for(i=1;i<=x1;i++)
			{	
				if(IsLinePicEmpty(x1,y1,(x1-i),y1))
				    if(IsNotLineConnection((x1-i),y1,x2,y2))
						return true;
			}
			temp=VIRTUAL_ROW-x1;
			for(i=1;i<=temp;i++)
			{
				if(IsLinePicEmpty(x1,y1,(x1+i),y1))
		           if(IsNotLineConnection((x1+i),y1,x2,y2))
						return true;
			}
		}
	}
	if((y1==y2))//同一列
	{		
		if(IsLineConnection(x1,y1,x2,y2))  	return true;
		else
		{
			for(i=1;i<=y1;i++)
			{	
				if(IsLinePicEmpty(x1,y1,x1,(y1-i)))
				    if(IsNotLineConnection(x1,(y1-i),x2,y2))
						return true;
			}
			temp=VIRTUAL_LINE-y1;
			for(i=1;i<=temp;i++)
			{
				if(IsLinePicEmpty(x1,y1,x1,(y1+i)))
		           if(IsNotLineConnection(x1,(y1+i),x2,y2))
						return true;
			}
		}
	}

	if((x2>x1)&&(y2>y1)) //(x2,y2)在(x1,y1)右下方
	{
		if(IsNotLineConnection(x1,y1,x2,y2))//先判断只有一个折点 * 代表两个点
			                                //  *----------|  
			return true;                    //  |          |
	                                        //  |__________*
		//先往下判断
		temp=VIRTUAL_ROW-x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1+i),y1))
			{
				if(IsNotLineConnection((x1+i),y1,x2,y2))
					return true;
			}
		}
		//往右判断
		temp=VIRTUAL_LINE-y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1+i)))
			{
				if(IsNotLineConnection(x1,(y1+i),x2,y2))
					return true;
			}
		}
		//往上判断
		for(i=1;i<=x1;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1-i),y1))
			{
				if(IsNotLineConnection((x1-i),y1,x2,y2))
					return true;
			}
		}
		//往左判断
		for(i=1;i<=y1;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1-1)))
			{
				if(IsNotLineConnection(x1,(y1-1),x2,y2))
					return true;
			}
		}
	}
	if((x2>x1)&&(y2<y1)) //(x2,y2)在(x1,y1)左下方
	{
		if(IsNotLineConnection(x1,y1,x2,y2))//先判断只有一个折点 * 代表两个点
			                                //  |----------*  
			return true;                    //  |          |
	                                        //  *__________|
		//先往下判断
		temp=VIRTUAL_ROW-x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1+i),y1))
			{
				if(IsNotLineConnection((x1+i),y1,x2,y2))
					return true;
			}
		}
		//往左判断
		temp=y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1-i)))
			{
				if(IsNotLineConnection(x1,(y1-i),x2,y2))
					return true;
			}
		}

		//往上判断
		temp=x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1-i),y1))
			{
				if(IsNotLineConnection((x1-i),y1,x2,y2))
					return true;
			}
		}
		//往右判断
		temp=VIRTUAL_LINE-y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1+i)))
			{
				if(IsNotLineConnection(x1,(y1+i),x2,y2))
					return true;
			}
		}	
	}
	if((x2<x1)&&(y2<y1)) //(x2,y2)在(x1,y1) 左上方
	{
		if(IsNotLineConnection(x1,y1,x2,y2))//先判断只有一个折点 * 代表两个点
			                                //  *----------|  
			return true;                    //  |          |
	                                        //  |__________*
		//先往上判断
		temp=x1;                                      
     	for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1-i),y1))
			{
				if(IsNotLineConnection((x1-i),y1,x2,y2))
					return true;
			}
		}
		//往左判断
		temp=y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1-i)))
			{
				if(IsNotLineConnection(x1,(y1-i),x2,y2))
					return true;
			}
		}
		//先往下判断
		temp=VIRTUAL_ROW-x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1+i),y1))
			{
				if(IsNotLineConnection((x1+i),y1,x2,y2))
					return true;
			}
		}
		//往右判断
		temp=VIRTUAL_LINE-y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1+i)))
			{
				if(IsNotLineConnection(x1,(y1+i),x2,y2))
					return true;
			}
		}
	}
	if((x2<x1)&&(y2>y1)) //(x2,y2)在(x1,y1)右上方
	{
		if(IsNotLineConnection(x1,y1,x2,y2))//先判断只有一个折点 * 代表两个点
			                                //  |----------*  
			return true;                    //  |          |
	                                        //  *__________|
		//先往上判断
		temp=x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1-i),y1))
			{
				if(IsNotLineConnection((x1-i),y1,x2,y2))
					return true;
			}
		}
		//往右判断
		temp=VIRTUAL_LINE-y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1+i)))
			{
				if(IsNotLineConnection(x1,(y1+i),x2,y2))
					return true;
			}
		}	
		//先往下判断
		temp=VIRTUAL_ROW-x1;                                      
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,(x1+i),y1))
			{
				if(IsNotLineConnection((x1+i),y1,x2,y2))
					return true;
			}
		}
		//往左判断
		temp=y1;
		for(i=1;i<=temp;i++)
		{
			if(IsLinePicEmpty(x1,y1,x1,(y1-i)))
			{
				if(IsNotLineConnection(x1,(y1-i),x2,y2))
					return true;
			}
		}
	}
	return false;	
}

bool CZLLKView::IsLineConnection(int x1, int y1, int x2, int y2) //判断直线两点是不是可连通的
{
   int i,temp;
   if(x1==x2)   //同一行
   {   
	   temp=abs(y2-y1);
	   if(y2>y1)
	   {
	 
	     for(i=1;i<temp;i++)
		     if(llk_map[x1][y1+i].PicIndex!=0)
				 return false;
	   }
	   else
	   {
		  for(i=1;i<temp;i++)
		     if(llk_map[x1][y1-i].PicIndex!=0)
				 return false;
	   }
   }
   if(y1==y2) //同一列
   {
	   temp=abs(x2-x1);
	   if(x2>x1)
	   {

⌨️ 快捷键说明

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